Fix false positive checking a class against type[Protocol] with instance attributes - #562
Merged
agronholm merged 4 commits intoJul 19, 2026
Conversation
Owner
|
Thanks! |
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.
Fixes #499
Checking a class (not an instance) against
type[SomeProtocol]raised a false-positiveTypeCheckErrorwhen the protocol declared a non-ClassVarattribute that the class sets in__init__:Per PEP 544, an attribute annotation without
ClassVardescribes an instance attribute, so a class object is not expected to carry it. This implements the approach you outlined in the issue: when the subject is a class,check_protocolnow ignores non-ClassVarattribute annotations and only requires theClassVarones to be present on the class. Instance checks are unchanged, and method/callable checks are untouched.Added two regression tests: a class satisfying a protocol via an instance attribute no longer raises, and a class missing a required
ClassVarmember still does. The fulltests/test_checkers.pysuite passes.