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

[typing] __origin__ invariant broken #77601

Closed
apaszke mannequin opened this issue May 3, 2018 · 4 comments
Closed

[typing] __origin__ invariant broken #77601

apaszke mannequin opened this issue May 3, 2018 · 4 comments
Labels
3.7 (EOL) end of life 3.8 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@apaszke
Copy link
Mannequin

apaszke mannequin commented May 3, 2018

BPO 33420
Nosy @gvanrossum, @ilevkivskyi, @apaszke

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2018-05-09.01:25:47.599>
created_at = <Date 2018-05-03.18:31:41.527>
labels = ['3.7', '3.8', 'type-bug', 'library']
title = '[typing] __origin__ invariant broken'
updated_at = <Date 2018-05-09.01:25:47.598>
user = 'https://github.com/apaszke'

bugs.python.org fields:

activity = <Date 2018-05-09.01:25:47.598>
actor = 'levkivskyi'
assignee = 'none'
closed = True
closed_date = <Date 2018-05-09.01:25:47.599>
closer = 'levkivskyi'
components = ['Library (Lib)']
creation = <Date 2018-05-03.18:31:41.527>
creator = 'apaszke'
dependencies = []
files = []
hgrepos = []
issue_num = 33420
keywords = []
message_count = 4.0
messages = ['316127', '316130', '316131', '316302']
nosy_count = 3.0
nosy_names = ['gvanrossum', 'levkivskyi', 'apaszke']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue33420'
versions = ['Python 3.7', 'Python 3.8']

@apaszke
Copy link
Mannequin Author

apaszke mannequin commented May 3, 2018

Hi everyone,

I have a module that needs to inspect type annotations on a few functions. One of the types I need to special case on is typing.Tuple, and I used code like this to detect it:

if getattr(annotation, '__origin__', None) == typing.Tuple:
    ... 
else:
    ...

This was based on the comment from the typing module (Lib/typing.py:609) that specified this particular invariant on the __origin__ attribute:

__origin__ keeps a reference to a type that was subscripted,
e.g., Union[T, int].__origin__ == Union;

Everything worked just fine until I checked it on the alpha release of Python 3.7 in my CI. Turns out, that in that release we have

typing.Tuple[str, int].__origin__ == tuple

and not (which is the case in e.g. 3.6)

typing.Tuple[str, int].__origin__ == typing.Tuple

I know this is not a documented attribute, so it can change, but I wanted to highlight that it's either a regression, or the comment will need to be updated, so people won't try to depend on that.

@apaszke apaszke mannequin added 3.7 (EOL) end of life 3.8 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels May 3, 2018
@ilevkivskyi
Copy link
Member

Yes, the comment needs to be updated, but as you said, no guaranties about undocumented dunder attribute. We tried to preserve as much of the API as possible in 3.7 after PEP-560, but something needs to be sacrificed (especially in the purely internal API you used).

You may instead use typing_inspect library on PyPI, I am not 100% sure it will help in your case, but it has is_tuple_type function that should work across several versions of typing.

@apaszke
Copy link
Mannequin Author

apaszke mannequin commented May 3, 2018

Of course, I'm not expecting this to be 100% reliable, and so I'm ok with the answer that the comment is now outdated.

I'd like to avoid adding extra dependencies for so simple things, so I guess I'll just special case that in my code for now. It would be great if the typing module had some tools for introspection built in!

@brettcannon brettcannon changed the title __origin__ invariant broken [typing] __origin__ invariant broken May 4, 2018
@ilevkivskyi
Copy link
Member

This is now fixed on master by 43d12a6 (the comment is updated).

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.7 (EOL) end of life 3.8 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant