Fix false positive when runtime_checkable() is used as a function#20831
Open
Fridayai700 wants to merge 2 commits intopython:masterfrom
Open
Fix false positive when runtime_checkable() is used as a function#20831Fridayai700 wants to merge 2 commits intopython:masterfrom
Fridayai700 wants to merge 2 commits intopython:masterfrom
Conversation
When `runtime_checkable(SomeProtocol)` is called as a function (rather than used as a `@runtime_checkable` decorator), mypy incorrectly reports "Only @runtime_checkable protocols can be used with instance and class checks" for `isinstance(x, runtime_checkable(SomeProtocol))`. This is a valid pattern — for example, when you want to use `isinstance()` checks in tests without marking the protocol as runtime-checkable in its definition. The fix adds a check in `check_runtime_protocol_test()` to recognize when the second argument to `isinstance()`/`issubclass()` is a direct call to `runtime_checkable()`. Fixes python#20787
for more information, see https://pre-commit.ci
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When
runtime_checkable(SomeProtocol)is called as a function (rather than used as a@runtime_checkabledecorator), mypy incorrectly reports:for
isinstance(x, runtime_checkable(SomeProtocol)).This is a valid pattern — for example, when you want to use
isinstance()checks in tests without permanently marking the protocol as runtime-checkable in its definition.Before:
After: No error.
Fix
check_runtime_protocol_test()now checks whether the expression passed toisinstance()/issubclass()is a direct call toruntime_checkable()(ortyping_extensions.runtime_checkable/typing_extensions.runtime). If so, the protocol is treated as runtime-checkable at the point of use.Test plan
testRuntimeCheckableCalledAsFunctionInIsinstancecovering bothisinstance()andissubclass()withruntime_checkable()as a function callpython -m pytest mypy/test/testcheck.py -k Runtime -o "addopts="— all passFixes #20787