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
{{ message }}
Repository navigation
Support type aliases in formats 'list[int]' and 'int | float' in argument conversion #4711
Our argument conversion typically uses actual types like int, list[int] and int | float, but we also support type aliases as strings like 'int' or 'integer'. The motivation for type aliases is to support types returned, for example, by dynamic libraries wrapping code using other languages. Such libraries can simply return type names as strings instead of mapping them to actual Python types.
There are two limitations with type aliases, though:
It isn't possible to represent types with nested types like 'list[int]'. Aliases always map to a single concrete type, not to nested types.
Unions cannot be represented using "Python syntax" like 'int | float'. It is possible to use a tuple like ('int', 'float'), though, so this is mainly an inconvenience.
Implementing this enhancement requires two things:
Enhance type conversion to work with TypeInfo. Currently these objects are only used by Libdoc.
In addition to helping with libraries wrapping non-Python code, this enhancement would allow us to create argument converters based on Libdoc spec files. That would probably be useful for external tools such as editor plugins.
This enhancement would also help with normal Python libraries when using Python versions that do not support the modern list[int] (requires Python 3.9) and int | float (requires Python 3.10) syntax. Type checkers support stringifying types like
defexample(a: 'list[int]', b: 'int | float'):
...
but the type hints we get are also strings. If this enhancement is implemented, we'd support type hints like above with all Python versions in our argument conversion.
Parsing strings like 'list[int]' and 'int | float' isn't exactly trivial, because we also need to support nested usages like 'list[ list[int] | tuple[ float | int | dict[str, int|float]]]'. I created a parser that can do that as an experiment and include it below. It would need some cleanup still and, more importantly, proper unit test. It also would need to be integrated with the existing TypeInfo class.
Error reporting has been improved and using something like list[int now causes an error taking the keyword into use. In practice characters [, ], , and | that have a special meaning with types cause problems (unless used as intended). Using non-type strings is otherwise still supported. For example, arg: Hello, world! will now cause an error due to the comma, but arg: Hello world! is still ok. In the future, possibly still as part of this issue, we may want to restrict type names to alpha numeric characters. Regardless is that done, already this change makes this issue backwards incompatible.
Changes to TypeConverter done as part of this issue turned out to cause backwards incompatibility issues (#4942). TypeConverter wasn't officially part of the public API so we most likely won't revert the changes, but instead add a new public API for that purpose. Anyway, also this is a backwards incompatible change that should be mentioned in the release notes.
Activity
pekkaklarck commentedon Mar 30, 2023
This enhancement would also help with normal Python libraries when using Python versions that do not support the modern
list[int](requires Python 3.9) andint | float(requires Python 3.10) syntax. Type checkers support stringifying types likebut the type hints we get are also strings. If this enhancement is implemented, we'd support type hints like above with all Python versions in our argument conversion.
pekkaklarck commentedon Mar 30, 2023
Parsing strings like
'list[int]'and'int | float'isn't exactly trivial, because we also need to support nested usages like'list[ list[int] | tuple[ float | int | dict[str, int|float]]]'. I created a parser that can do that as an experiment and include it below. It would need some cleanup still and, more importantly, proper unit test. It also would need to be integrated with the existingTypeInfoclass.Move `TypeInfo` to its own module.
Refactor type conversion.
Argument conversion with `'list[int]'` and `'int | float'`
pekkaklarck commentedon Sep 22, 2023
Basic functionality is now done and the feature is also documented. This is what we still need to do:
'list[int'.TypeInfoParserand possibly also forTypeInfoTokenizer, including error situations.TypeInfo.Enhance TypeInfoParser. Part of #4711.
Argument conversion: Make using invalid string type info an error.
pekkaklarck commentedon Sep 28, 2023
Error reporting has been improved and using something like
list[intnow causes an error taking the keyword into use. In practice characters[,],,and|that have a special meaning with types cause problems (unless used as intended). Using non-type strings is otherwise still supported. For example,arg: Hello, world!will now cause an error due to the comma, butarg: Hello world!is still ok. In the future, possibly still as part of this issue, we may want to restrict type names to alpha numeric characters. Regardless is that done, already this change makes this issue backwards incompatible.Refactor type conversion.
Libdoc: Store types parsed from specs as TypeInfo.
pekkaklarck commentedon Oct 3, 2023
After some more refactoring and enhancements this issue ought to now be done.
pekkaklarck commentedon Nov 13, 2023
Changes to
TypeConverterdone as part of this issue turned out to cause backwards incompatibility issues (#4942).TypeConverterwasn't officially part of the public API so we most likely won't revert the changes, but instead add a new public API for that purpose. Anyway, also this is a backwards incompatible change that should be mentioned in the release notes.Support brackets in paramets with TypeInfoParser.
Enhance TypeInfo.