avanov / typeit

Infer Python types from JSON data, use them for auto serialisation and parsing

Home Page:https://typeit.readthedocs.io/en/develop/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

https://github.com/avanov/typeit/workflows/CI/badge.svg?branch=develop https://coveralls.io/repos/github/avanov/typeit/badge.svg?branch=develop Requirements Status Documentation Status Latest PyPI Release

Typeit

typeit infers Python types from a sample JSON/YAML data, and provides you with the tools for serialising and parsing it. It also provides you with smart constructors for arbitrarily nested data structures. The library works superb on Python 3.7 and above.

Start using it by generating types for your JSON payloads:

$ echo '{"first-name": "Hello", "initial": null, "last_name": "World"}' | typeit gen

The snipped above produces output similar to this:

from typing import Any, NamedTuple, Optional, Sequence
from typeit import TypeConstructor


class Main(NamedTuple):
    first_name: str
    initial: Optional[Any]
    last_name: str


overrides = {
    Main.first_name: 'first-name',
}


mk_main, serialize_main = TypeConstructor & overrides ^ Main

Use these functions to construct and serialize your payloads:

payload = {"first-name": "Hello", "initial": None, "last_name": "World"}

data = mk_main(payload)
assert isinstance(data, Main)
assert serialize_main(data) == payload

Documentation

Documentation is hosted on ReadTheDocs: https://typeit.readthedocs.io/en/develop/

Test framework

Run existing test suite with

$ make test

Changelog

See CHANGELOG.

About

Infer Python types from JSON data, use them for auto serialisation and parsing

https://typeit.readthedocs.io/en/develop/

License:MIT License


Languages

Language:Python 97.9%Language:Nix 1.4%Language:Makefile 0.7%