Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-29262: Add get_origin() and get_args() introspection helpers to typing #13685

Merged
merged 4 commits into from
May 30, 2019

Conversation

ilevkivskyi
Copy link
Member

@ilevkivskyi ilevkivskyi commented May 30, 2019

This is an old feature request that appears from time to time. After a year of experimenting with various introspection capabilities in typing_inspect on PyPI, I propose to add these two most commonly used functions: get_origin() and get_args(). These are essentially thin public wrappers around private APIs: __origin__ and __args__.

As discussed in the issue and on the typing tracker, exposing some public helpers instead of __origin__ and __args__ directly will give us more flexibility if we will decide to update the internal representation, while still maintaining backwards compatibility.

The implementation is very simple an is essentially a copy from typing_inspect with one exception: ClassVar was special-cased in typing_inspect, but I think this special-casing doesn't really help and only makes things more complicated.

https://bugs.python.org/issue29262

Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix the typo and land it!

Doc/library/typing.rst Outdated Show resolved Hide resolved
@ilevkivskyi ilevkivskyi merged commit 4c23aff into python:master May 30, 2019
@ilevkivskyi ilevkivskyi deleted the add-typing-introspection branch May 30, 2019 23:10
@saulshanabrook
Copy link
Contributor

Could you go into the rationale for normalization a bit more? I would prefer that get_origin(typing.List[int]) is typing.List. xref ilevkivskyi/typing_inspect#36 which lays out the same issue in typing_inspect.

DinoV pushed a commit to DinoV/cpython that referenced this pull request Jan 14, 2020
…yping (pythonGH-13685)

This is an old feature request that appears from time to time. After a year of experimenting with various introspection capabilities in `typing_inspect` on PyPI, I propose to add these two most commonly used functions: `get_origin()` and `get_args()`. These are essentially thin public wrappers around private APIs: `__origin__` and `__args__`.

As discussed in the issue and on the typing tracker, exposing some public helpers instead of `__origin__` and `__args__` directly will give us more flexibility if we will decide to update the internal representation, while still maintaining backwards compatibility.

The implementation is very simple an is essentially a copy from `typing_inspect` with one exception: `ClassVar` was special-cased in `typing_inspect`, but I think this special-casing doesn't really help and only makes things more complicated.
jstasiak added a commit to jstasiak/typing that referenced this pull request Feb 6, 2020
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
gvanrossum pushed a commit to python/typing that referenced this pull request Feb 7, 2020
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.
JelleZijlstra pushed a commit to python/typing_extensions that referenced this pull request May 19, 2022
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.
JelleZijlstra pushed a commit to python/typing_extensions that referenced this pull request May 19, 2022
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants