[New-bugs-announce] [issue43817] Add typing.eval_annotations()
Larry Hastings
report at bugs.python.org
Mon Apr 12 13:39:44 EDT 2021
New submission from Larry Hastings <larry at hastings.org>:
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?
----------
components: Library (Lib)
messages: 390874
nosy: eric.smith, gvanrossum, larry
priority: normal
severity: normal
stage: needs patch
status: open
title: Add typing.eval_annotations()
type: enhancement
versions: Python 3.10
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue43817>
_______________________________________
More information about the New-bugs-announce
mailing list