Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.3k
bpo-39491: Merge PEP 593 (typing.Annotated) support#18260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
PEP 593 has been accepted some time ago. I got a green light for mergingthis from Till, so I went ahead and combined the code contributed totyping_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 usedmy best judgment, let me know if something needs changing in thisregard.[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
I also modified |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
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.
Doc/library/typing.rst Outdated
| more information). For example:: | ||
| class Student(NamedTuple): | ||
| name: Annotated[str, struct.ctype("<10s")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
The builtin modulestruct doesn't have an attributectype. Maybe pick a different example?
| ..versionadded::3.5.2 | ||
| ..data::Annotated |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Can I ask that you hint at the possibility of passing multiple extras (Annotated[T, x, y, z]) early in the description?
Doc/library/typing.rst Outdated
| 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Reflow this paragraph.
Doc/library/typing.rst Outdated
| 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
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).
Doc/library/typing.rst Outdated
| * ``Annotated`` can be used with nested and generic aliases:: | ||
| Typevar T = ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Please use correct syntax for type variables.
Doc/whatsnew/3.9.rst Outdated
| :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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Also mention the new argument to get_type_hints() here.
Lib/test/test_typing.py Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Only one blank line 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Thanks! I'll land this once the tests pass.
bedevere-bot commentedFeb 5, 2020
@gvanrossum: Please replace |
Woo! I'm wondering now – what should happen in the typing repository? Should the |
I'm thinking |
I can't speak for@ilevkivskyi (who seems to be too busy to follow this, alas) but I think the dependency clearly goes from typing_extension to typing -- typing doesn't use anything from typing_extensions, while typing_extensions already depends on typing. So to make typing_extensions work with Python 3.9, typing_extensions should try to import Annotated from typing before defining its own versions (and refraining from defining its own versions if it can import Annotated from typing). I don't think it's worth your while to copy Annotated and the improved get_type_info into typing -- that would only benefit Python versions so old that they don't have typing in the stdlib at all. Probably the only relevant version would be 2.7, whose end of life has come. Let those users continue to import this feature from typing_extensions. There's also something similar to be done for typeshed, otherwise 3.9 users who import Annotated from typing will get complaints. Here I think the definition should strictly check on sys.version_info: in 3.9 and later, the definition is in typing, and typing_extensions imports from there; in earlier versions, the definition is only in typing_extensions. |
The implementations come from CPython commit 427c84f13f77 with one smallchange – the get_origin's docstring mentions Annotated as it's alsosupported.get_origin() and get_args() introduced in [1] and modified in [2] tosupport Annotated.[1]python/cpython#13685[2]python/cpython#18260
Following [1] this prevents multiple runtime implementations of Annotatedand get_type_hints from existing on Python 3.9 (which has recentlymerged PEP 593 changes[2]). Reexporting allows code targetting bothPython pre-3.9 and 3.9+ to be able to import from typing_extensions andto keep working without changes.[1]python/cpython#18260 (comment)[2]python/cpython#18260
I forgot to do it inpython#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 smallchange – the get_origin's docstring mentions Annotated as it's alsosupported.get_origin() and get_args() introduced in [1] and modified in [2] tosupport Annotated.[1]python/cpython#13685[2]python/cpython#18260* Define our own get_origin()/get_args() in typing_extensions on Python 3.8Otherwise typing_extensions.get_origin() would not recognizetyping_extensions.Annotated on 3.8.
…699)Following [1] this prevents multiple runtime implementations of Annotatedand get_type_hints from existing on Python 3.9 (which has recentlymerged PEP 593 changes[2]). Reexporting allows code targetting bothPython pre-3.9 and 3.9+ to be able to import from typing_extensions andto keep working without changes.[1]python/cpython#18260 (comment)[2]python/cpython#18260
The implementations come from CPython commit 427c84f13f77 with one smallchange – the get_origin's docstring mentions Annotated as it's alsosupported.get_origin() and get_args() introduced in [1] and modified in [2] tosupport Annotated.[1]python/cpython#13685[2]python/cpython#18260* Define our own get_origin()/get_args() in typing_extensions on Python 3.8Otherwise typing_extensions.get_origin() would not recognizetyping_extensions.Annotated on 3.8.
…#699)Following [1] this prevents multiple runtime implementations of Annotatedand get_type_hints from existing on Python 3.9 (which has recentlymerged PEP 593 changes[2]). Reexporting allows code targetting bothPython pre-3.9 and 3.9+ to be able to import from typing_extensions andto keep working without changes.[1]python/cpython#18260 (comment)[2]python/cpython#18260
The implementations come from CPython commit 427c84f13f77 with one smallchange – the get_origin's docstring mentions Annotated as it's alsosupported.get_origin() and get_args() introduced in [1] and modified in [2] tosupport Annotated.[1]python/cpython#13685[2]python/cpython#18260* Define our own get_origin()/get_args() in typing_extensions on Python 3.8Otherwise typing_extensions.get_origin() would not recognizetyping_extensions.Annotated on 3.8.
…#699)Following [1] this prevents multiple runtime implementations of Annotatedand get_type_hints from existing on Python 3.9 (which has recentlymerged PEP 593 changes[2]). Reexporting allows code targetting bothPython pre-3.9 and 3.9+ to be able to import from typing_extensions andto keep working without changes.[1]python/cpython#18260 (comment)[2]python/cpython#18260
Uh oh!
There was an error while loading.Please reload this page.
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