New submission from Larry Hastings <[email protected]>:
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 <[email protected]>
<https://bugs.python.org/issue43817>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com