Message390874
Currently, with PEP 563 looming over us, people who use annotations for something besides type hints are kind of stuck. Converting stringized annotations back into useful values is a complicated problem, and there's only function in the standard library that will do it for you: typing.get_type_hints(). However, typing.get_type_hints() deals specifically with *type hints*. Type hints are a subset of annotations, and they have an additional set of rules they want to obey. As such typing.get_type_hints() is quite opinionated and makes additional changes to the annotations that someone who only wants to un-stringize their annotations likely does not want.
I therefore propose adding a new function to typing:
typing.get_annotations(o)
This would behave similarly to typing.get_type_hints(o), except it would be agnostic about the contents of the annotations. It would simply evaluate them, without further changing them as typing.get_type_hints() does. Specifically:
* It would ignore the "__no_type_check__" attribute.
* It would not wrap string annotations with ForwardRef().
* It would not wrap values with Optional[].
* It would not turn an annotation of None into type(None).
* It would not otherwise change the value of the annotation, in case I missed any.
Since the two functions would be so similar, hopefully either they would share a common implementation, or typing.get_type_hints() could be implemented on top of typing.get_annotations().
Guido, Eric, any thoughts? |
|
Date |
User |
Action |
Args |
2021-04-12 17:39:44 | larry | set | recipients:
+ larry, gvanrossum, eric.smith |
2021-04-12 17:39:44 | larry | set | messageid: <1618249184.39.0.525270769302.issue43817@roundup.psfhosted.org> |
2021-04-12 17:39:44 | larry | link | issue43817 messages |
2021-04-12 17:39:44 | larry | create | |
|