-
Notifications
You must be signed in to change notification settings - Fork 886
Description
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 the
pytype!` 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]