Skip to main content

Petit Type System

This library is meant to be used as a backend to implement type exporter from python to another language.

In order to use it, you are supposed to implement the different handlers for the types you want to export.

Petit-ts is based on it, and is able to export any of current python types.

Petit-java is currently under development and will make the export of python types to Java available

The next step will be to build an GraphQL exporter

Where to start ?

The libs works by using a store which has handlers for each type of data. To create a new type of store, you have to use create_store_class, this will return a factory, which can later be used to create an actual store, which will hold all of the informations of your different types.

Example:

ts_raw_default_types: List[Tuple[Any, str]] = [
    (bool, "boolean"),
    (None, "void"),
    (NoneType, "undefined"),

    (null, "null"),
    (undefined, "undefined"),

    (int, "number /*int*/"),
    (float, "number /*float*/"),

    (str, "string"),
    (dict, "object"),
    (list, "any[]"),

    (List[Any], "any[]"),
    (List, "any[]"),
    (List[int], "number[]"),
    (List[str], "string[]"),

    # any's
    (object, "any"),
    (Any, "any"),
]


# TS-specifics
ts_export_token = 'export'

ts_class_handlers: List[Type[ClassHandler]] = [
    TSEnumHandler,
    TSDataclassHandler,
]

ts_basic_handlers: List[Type[BasicHandler]] = [
    TSUnionHandler,
    TSLiteralHandler,
    TSArrayHandler,
    TSMappingHandler,
    TSTupleHandler,
]

TSTypeStore = create_store_class(
    ts_export_token,
    struct_handler=TSStructHandler,
    basic_handlers=ts_basic_handlers,
    class_handlers=ts_class_handlers,
    basic_types=ts_raw_default_types,
)

This is an example taken from petit-ts

We have 3 types of handlers:

  • BasicHandler
  • ClassHandler
  • StructHandler

BasicHandler

A BasicHandler implements two methods:

should_handle(cls: Any) -> bool

This functions returns wether this Handler should actually handle the supplied object

build(cls: Any) -> Tuple[Optional[str], str]

This function builds the representation of your type in the target language, most of the time it will use the store.get_repr(value) expression.

It returns a Tuple[Optional[str], str] where the first value, is the name the exported type. In some langague you can return None and the type will be inlined.

For example Typescript supports it but Java doesn't.

The second value is the actual representation.

ClassHandler

A ClassHandler implements 3 methods:

is_mapping() -> bool

This function tells the library if it should use the StructRenderer on the returned data or just use the result a the representation directly

For example, the DataclassHandler and the PydanticHandler use it in order to reduce the amount of code.

should_handle

Like for the BaseHandler this method returns wether the handler should handle the supplied object.

build

This function returns, a string representation or a dict depending on if it's a mapping or not

StructHandler

This handler implements the process of making structs for the target language.

A StructHandler implements 2 methods:

make_struct

This methods returns a string representation of a given dict, or dict-like object.

make_struct_inline

This does the same as the previous function but it does it inline.

For example, Typescript supports it but Java doesn't.

If you don't want to support this option or if the target language doesn't support it, you don't have to do anything.

BasicTypes

We have the basic types and their string representation.

Export token

For now we use an export token, it is used in order to export a type from a given class/module.

Todo

Update the part where the generic classes are handled

Metadata

Release files for petit-type-system 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for petit-type-system 0.3.0
File Size Uploaded Provenance Actions
petit_type_system-0.3.0.tar.gz 14.1 kB Without provenance Details for petit_type_system-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for petit-type-system 0.3.0
File Interpreter ABI Platform Actions
petit_type_system-0.3.0-py2.py3-none-any.whl Python 2, Python 3 none any Details for petit_type_system-0.3.0-py2.py3-none-any.whl

Total release size: 29.9 kB

Release files / petit_type_system-0.3.0.tar.gz

Download URL petit_type_system-0.3.0.tar.gz
Size 14.1 kB
Tags Source
SHA-256 checksum
How to use checksums
1ccca254b59871cb2fab7fbda606da720aa97b204601ca80b72ddcd6af1bc6f9
BLAKE2b-256 checksum
How to use checksums
eaaf4f56cbd3d5796e06621b6223274eaceb9960a91d5be03287706db90cb61b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.6

Release files / petit_type_system-0.3.0-py2.py3-none-any.whl

Download URL petit_type_system-0.3.0-py2.py3-none-any.whl
Size 15.8 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
beeaaa72c603dd7b5815c26f5feba22f07174c64229726f9060086115bab0acd
BLAKE2b-256 checksum
How to use checksums
215872c42245e73d653ad693e70a8aefae926a15695299b52620f96658bffd57
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.6

Security for petit-type-system 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page