Skip to content

Python Typeshare improvements #217

@hculea

Description

@hculea

In #169 support for Python is being added in Typeshare. This enables the majority of Rust -> Python type translation use-cases leveraging Pydantic, a data validation library for Python.

This issue captures follow-up improvements for the Python typeshare implementation, that have been left out of scope for the first iteration.

  • Tighter type validation: conint from Pydantic (and other constrained types) can be used for tighter validation. For example, if the Rust type is u8, then we can leverage conint to bound the number from 0 to 255. Currently, this validation relies on the Rust deserializer.

  • Generics are not yet supported: struct and enum with generic parameters are not supported. For example, the test case can_generate_generic_enum is not enabled for Python as it is not supported. This can be achieved using TypeVar of the typing library.

  • Only support for adjacently tagged enums: Rust enums must be adjacently tagged, like #[serde(tag = "t", content = "c")], other enum representations, such as internally, externally or untagged are not supported. This is a limitation for the entire Typeshare framework, which already throws parsing errors in the absence of adjacent enum tags.

  • Type overriding is not supported: Type overriding with the #[typeshare] macro is not supported. See the test can_override_types for details.

  • Multi-file support: Currently, all the Python types are written to a single file. @darrell-roberts has shared a potential solution to this here.

    • part of this will be to remove the overriden generate_types function. See suggestion

Originally posted by @CheatCod in #169 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions