Skip to content

Validate Python types #5238

@Tpt

Description

@Tpt

The new introspection code requires to write Python type names as strings:

const INPUT_TYPE: &'static str = "int";

It can quickly become cumbersome when dealing with collections. For example:

const INPUT_TYPE: &'static str = const_concat!("typing.Collection[", Foo::INPUT_TYPE, "]");

there is also no compile-time validation.

An approach might be to introduce a PythonType(&'static str) type (name TBD) that would wrap the Python types stored using static strings and a pytype! procedural macro to validate the types and allow filling easily parameters.

For example:

const INPUT_TYPE: PyType = pytype!(int);
const INPUT_TYPE: PyType = pytype!(typing.Collection[FOO::INPUT_TYPE])

The base type is considered as a string but arguments are Rust expressions returning PyType. To get a list[int] explicitely:

const INPUT_TYPE: PyType = pytype!(list[pytype!(int)])

Side note: it's likely that the Python int is extracted into a Rust type like u32, hence the nicer syntax for the expected usecase:

const INPUT_TYPE: PyType = pytype!(list[u32::INPUT_TYPE])

The possible grammar for the ìnputargument of thepytype!` macro might be:

ident := Rust ident
bracketed := '[' bracketed_item (',' bracketed_item)* ']'
bracketed_item := bracketed | Rust expr
input := ident ('.' ident)* (bracketed)?

Note that Rust bracketed is recursive to support Callable[[int], int]

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions