bpo-39491: Merge PEP 593 (typing.Annotated) support - #18260
Conversation
PEP 593 has been accepted some time ago. I got a green light for merging this from Till, so I went ahead and combined the code contributed to typing_extensions[1] and the documentation from the PEP 593 text[2]. My changes were limited to: * removing code designed for typing_extensions to run on older Python versions * removing some irrelevant parts of the PEP text when copying it over as documentation and otherwise changing few small bits to better serve the purpose * changing the get_type_hints signature to match reality (parameter names) I wasn't entirely sure how to go about crediting the authors but I used my best judgment, let me know if something needs changing in this regard. [1] https://github.com/python/typing/blob/8280de241fd8c8afe727c7860254b753e383b360/typing_extensions/src_py3/typing_extensions.py [2] https://github.com/python/peps/blob/17710b879882454d55f82c2d44596e8e9f8e4bff/pep-0593.rst
gvanrossum
left a comment
There was a problem hiding this comment.
I didn't audit that the changes to typing.py and test_typing.py are exactly what's in the typing repo, I skimmed it and beyond that I trust you.
| more information). For example:: | ||
|
|
||
| class Student(NamedTuple): | ||
| name: Annotated[str, struct.ctype("<10s")] |
There was a problem hiding this comment.
The builtin module struct doesn't have an attribute ctype. Maybe pick a different example?
|
|
||
| .. versionadded:: 3.5.2 | ||
|
|
||
| .. data:: Annotated |
There was a problem hiding this comment.
Can I ask that you hint at the possibility of passing multiple extras (Annotated[T, x, y, z]) early in the description?
| It's up to the tool consuming the annotations | ||
| to decide whether the client is allowed to have several annotations on | ||
| one type and how to merge those annotations. |
| T1 = Annotated[int, ValueRange(-10, 5)] | ||
| T2 = Annotated[T1, ValueRange(-20, 3)] | ||
|
|
||
| A new ``include_extras`` parameter to :func:`get_type_hints` has been added, |
There was a problem hiding this comment.
It will stop to be "new" in the next version. You can just state that the functionality exists without describing that it's new and has been added (that would be text for "what's new" though).
|
|
||
| * ``Annotated`` can be used with nested and generic aliases:: | ||
|
|
||
| Typevar T = ... |
There was a problem hiding this comment.
Please use correct syntax for type variables.
| :pep:`593` introduced an :data:`typing.Annotated` type to decorate existing | ||
| types with context-specific metadata. (Contributed by Till Varoquaux and | ||
| Konstantin Kashin.) |
There was a problem hiding this comment.
Also mention the new argument to get_type_hints() here.
|
|
||
|
|
|
If there's a difference between this and what's in the typing repo other than I listed above it's a mistake, but I don't believe anything significant got changed when transferring the code here. As for your comments – they should be addressed now. |
gvanrossum
left a comment
There was a problem hiding this comment.
Thanks! I'll land this once the tests pass.
|
@gvanrossum: Please replace |
|
Woo! I'm wondering now – what should happen in the typing repository? Should the |
|
I'm thinking |
The implementations come from CPython commit 427c84f13f77 with one small change – the get_origin's docstring mentions Annotated as it's also supported. get_origin() and get_args() introduced in [1] and modified in [2] to support Annotated. [1] python/cpython#13685 [2] python/cpython#18260
Following [1] this prevents multiple runtime implementations of Annotated and get_type_hints from existing on Python 3.9 (which has recently merged PEP 593 changes[2]). Reexporting allows code targetting both Python pre-3.9 and 3.9+ to be able to import from typing_extensions and to keep working without changes. [1] python/cpython#18260 (comment) [2] python/cpython#18260
I forgot to do it in python#18260.
This is to handle PEP 593 support recently merged into CPython[1]. [1] python/cpython#18260
The implementations come from CPython commit 427c84f13f77 with one small change – the get_origin's docstring mentions Annotated as it's also supported. get_origin() and get_args() introduced in [1] and modified in [2] to support Annotated. [1] python/cpython#13685 [2] python/cpython#18260 * Define our own get_origin()/get_args() in typing_extensions on Python 3.8 Otherwise typing_extensions.get_origin() would not recognize typing_extensions.Annotated on 3.8.
…699) Following [1] this prevents multiple runtime implementations of Annotated and get_type_hints from existing on Python 3.9 (which has recently merged PEP 593 changes[2]). Reexporting allows code targetting both Python pre-3.9 and 3.9+ to be able to import from typing_extensions and to keep working without changes. [1] python/cpython#18260 (comment) [2] python/cpython#18260
The implementations come from CPython commit 427c84f13f77 with one small change – the get_origin's docstring mentions Annotated as it's also supported. get_origin() and get_args() introduced in [1] and modified in [2] to support Annotated. [1] python/cpython#13685 [2] python/cpython#18260 * Define our own get_origin()/get_args() in typing_extensions on Python 3.8 Otherwise typing_extensions.get_origin() would not recognize typing_extensions.Annotated on 3.8.
…699) Following [1] this prevents multiple runtime implementations of Annotated and get_type_hints from existing on Python 3.9 (which has recently merged PEP 593 changes[2]). Reexporting allows code targetting both Python pre-3.9 and 3.9+ to be able to import from typing_extensions and to keep working without changes. [1] python/cpython#18260 (comment) [2] python/cpython#18260
The implementations come from CPython commit 427c84f13f77 with one small change – the get_origin's docstring mentions Annotated as it's also supported. get_origin() and get_args() introduced in [1] and modified in [2] to support Annotated. [1] python/cpython#13685 [2] python/cpython#18260 * Define our own get_origin()/get_args() in typing_extensions on Python 3.8 Otherwise typing_extensions.get_origin() would not recognize typing_extensions.Annotated on 3.8.
…699) Following [1] this prevents multiple runtime implementations of Annotated and get_type_hints from existing on Python 3.9 (which has recently merged PEP 593 changes[2]). Reexporting allows code targetting both Python pre-3.9 and 3.9+ to be able to import from typing_extensions and to keep working without changes. [1] python/cpython#18260 (comment) [2] python/cpython#18260

PEP 593 has been accepted some time ago. I got a green light for merging
this from Till, so I went ahead and combined the code contributed to
typing_extensions[1] and the documentation from the PEP 593 text[2].
My changes were limited to:
versions
documentation and otherwise changing few small bits to better serve
the purpose
names)
I wasn't entirely sure how to go about crediting the authors but I used
my best judgment, let me know if something needs changing in this
regard.
[1] https://github.com/python/typing/blob/8280de241fd8c8afe727c7860254b753e383b360/typing_extensions/src_py3/typing_extensions.py
[2] https://github.com/python/peps/blob/17710b879882454d55f82c2d44596e8e9f8e4bff/pep-0593.rst
https://bugs.python.org/issue39491