Repository navigation
Fix subclassing builtin protocols on older Python versions #650
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1080,6 +1080,12 @@ def _next_in_mro(cls): | |
| return next_in_mro | ||
|
|
||
|
|
||
| _PROTO_WHITELIST = ['Callable', 'Awaitable', | ||
| 'Iterable', 'Iterator', 'AsyncIterable', 'AsyncIterator', | ||
| 'Hashable', 'Sized', 'Container', 'Collection', 'Reversible', | ||
| 'ContextManager', 'AsyncContextManager'] | ||
|
|
||
|
|
||
| def _get_protocol_attrs(cls): | ||
| attrs = set() | ||
| for base in cls.__mro__[:-1]: # without object | ||
|
|
@@ -1187,7 +1193,9 @@ def __init__(cls, *args, **kwargs): | |
| for b in cls.__bases__) | ||
| if cls._is_protocol: | ||
| for base in cls.__mro__[1:]: | ||
| if not (base in (object, Generic, Callable) or | ||
| if not (base in (object, Generic) or | ||
| base.__module__ == 'collections.abc' and | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe there's a bug here because In [24]: from typing import ContextManager
...: from typing_extensions import Protocol
...:
...: class Foobar(ContextManager, Protocol):
...: pass
...:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-24-fa1f28400733> in <module>
1 from typing import ContextManager
2
----> 3 class Foobar(ContextManager, Protocol):
4 pass
5
/mnt/xarfuse/uid-4861/0260e378-ns-4026531840/typing_extensions.py in __init__(cls, *args, **kwargs)
1201 base.__origin__ is Generic):
1202 raise TypeError('Protocols can only inherit from other'
-> 1203 ' protocols, got %r' % base)
1204
1205 def _no_init(self, *args, **kwargs):
TypeError: Protocols can only inherit from other protocols, got typing.ContextManagerIs there something I'm missing here, or is there genuinely an issue that needs to be fixed by maintaining a map of module -> class name for everything in the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this happen on all Python versions? I think it may be relevant only for 3.5, if not however it makes sense to fix this by making whitelisting more precise. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll see if I can repro in 2.7 as well, my error is from 3.6. If it is 3.5+ only, would you have an alternate suggestion apart from having a more explicit module -> class name whitelist? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can repro this in 2.7 as well:
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like there is a bigger problem. I can reproduce this also in Python 3.8 (beta) and There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, I'll do that right now. Thanks for following up @ilevkivskyi! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Created https://bugs.python.org/issue38008, I'll move the discussion there
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jivid Now that this is fixed upstream, would you like to work on a backport? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ilevkivskyi yes! Is it enough for me to make a similar change to the original PR in this repo, or do I need to do something else for backporting?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I don't remember for sure TBH, but most likely yes. Note however that you might need to add this change to more than one place. There are four places that might need to be updated: |
||
| base.__name__ in _PROTO_WHITELIST or | ||
| isinstance(base, TypingMeta) and base._is_protocol or | ||
| isinstance(base, GenericMeta) and | ||
| base.__origin__ is Generic): | ||
|
|
@@ -1513,7 +1521,9 @@ def _proto_hook(other): | |
|
|
||
| # Check consistency of bases. | ||
| for base in cls.__bases__: | ||
| if not (base in (object, Generic, Callable) or | ||
| if not (base in (object, Generic) or | ||
| base.__module__ == 'collections.abc' and | ||
| base.__name__ in _PROTO_WHITELIST or | ||
| isinstance(base, _ProtocolMeta) and base._is_protocol): | ||
| raise TypeError('Protocols can only inherit from other' | ||
| ' protocols, got %r' % base) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.