You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In python 3, functions and methods can have an __annotations__ attribute, which is automatically initialized with type hints:
>>> def foo(a: str, b: int) -> str:
... return a * b
>>> help(foo)
Help on function foo in module __main__:
foo(a:str, b:int) -> str
>>> foo.__annotations__
{'a': <class 'str'>, 'b': <class 'int'>, 'return': <class 'str'>}
Those annotations are used for ide type hints and can be used as basis for generating .pyi files. (iirc there is even a tool that can do that automatically)
Implementation
In the proc macros, we get the function argument types, but only as ast, not as resolved typed.
So first, implement a function we can call on all possible rust types for arguments (rust native, python native and custom) that returns the corresponding type object. Then generate code that builds a dict with the annotations using that function. During runtime, add that dict with PyFunction_SetAnnotations. Make sure to take of tyke possible cyclic dependencies (afaik this is done by using the string name instead the type object when there would otherwise be a cycle).
This could possibly even be extended to modules and class, s. datamodel.
The text was updated successfully, but these errors were encountered:
In python 3, functions and methods can have an
__annotations__
attribute, which is automatically initialized with type hints:Those annotations are used for ide type hints and can be used as basis for generating
.pyi
files. (iirc there is even a tool that can do that automatically)Implementation
In the proc macros, we get the function argument types, but only as ast, not as resolved typed.
So first, implement a function we can call on all possible rust types for arguments (rust native, python native and custom) that returns the corresponding type object. Then generate code that builds a dict with the annotations using that function. During runtime, add that dict with PyFunction_SetAnnotations. Make sure to take of tyke possible cyclic dependencies (afaik this is done by using the string name instead the type object when there would otherwise be a cycle).
This could possibly even be extended to modules and class, s. datamodel.
The text was updated successfully, but these errors were encountered: