-
-
Notifications
You must be signed in to change notification settings - Fork 31.3k
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
Troubles with @runtime_checkable protocols #83089
Comments
The PEP-544 specifies that: A protocol can be used as a second argument in isinstance() and issubclass() only if it is explicitly opt-in by @runtime_checkable decorator. It is not specified exactly whether this should be enforced by static type checkers or at runtime. Currently it is enforced in both cases: mypy flags this as error, and a TypeError is raised at runtime. There is however a problem with current runtime implementation: abc and functools modules may call issubclass() on non-runtime checkable protocols if they appear as explicit superclasses. This is currently solved by a sys._getframe() hack in typing module. The problem is that current solution is incomplete. For example, the TypeError is not raised in the case of non-method protocols: >>> class P(Protocol):
... x: int
...
>>>
>>> class C: ...
...
>>> isinstance(C(), P)
False # should be TypeError I tried to fix it this morning but after an hour of attempts to tweak the existing hack I gave up. It looks like there are only two reasonable solutions:
Any thoughts/preferences? |
If the "little helper in abc" solves it, let's do that. The sys._getframe() hack has always been a bit smelly -- I assume we can get rid of that then? |
It seems like the straightforward, minimal fix would be to just add if (getattr(cls, '_is_protocol', False) and to _ProtocolMeta.__instancecheck__. Does that fail on some edge case (that the current implementation works on)? It's a little weird that _ProtocolMeta.__instancecheck__ doesn't explicitly check that the protocol is runtime-checkable. |
Hi Kevin, If you want to work on this, could you come up with some test cases that --Guido |
Hi Guido, I decided to help with this issue as far as Kevin did not respond. |
I checked and this bug is not present at 3.11 version. So I simply added test to cover case mentioned by Ivan. |
Please forget my previous msg, this bug still present at 3.11 version. PR contains fix proposed by Kevin. Sorry for making to much noise. |
Yurii and Kevin, thanks for pushing the patch forward! Thanks for the review too Guido. I'm closing this issue as all bugfix PRs have landed to bugfix branches. This will make its way into the next versions of Python for those respective branches. One point of contention is whether to introduce this in 3.9.6. This will cause code previously working in 3.9.5 to throw an error if people were using it incorrectly. So it might be better to only enforce this in 3.10 onwards. I created #70265 just in case the decision is made to revert. Sorry for any inconvenience caused everyone! |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: