Repository navigation
Check class object assignability and RHS __eq__ for overlap - #11931
crypt0lith wants to merge 4 commits into
Conversation
When the left operand is a class object, isTypeComparable checks assignability only against a class object or type instance on the right, so a class object is never comparable to an object or protocol instance. Check assignability whenever the right operand is a class or class instance.
🔴 Pyright CLI QA —
|
4982666 to
d4acb4c
Compare
1101ed2 to
d995710
Compare
1ccec55 to
2baca80
Compare
|
Local review: The older instance-field QA case no longer reproduces. Both operand orders still report |
2baca80 to
fe2f1bb
Compare
|
I dropped 2baca80. Main has the same instance-member defect in the left operand's
|
Summary
x in (...)narrows the subject toNeverwhen the container holds class objects and the subject is of typeobjector a protocol type, or when only the subject's class overrides__eq__. The branch is then unreachable, andreportUnnecessaryContainsreports it.reportUnnecessaryComparisonreports the same operand pairs in==, andiswith a class object on the left against anobjector protocol subject.Root cause
When the left operand is a class object,
isTypeComparablechecks assignability in either direction only if the right operand is also a class object or atypeinstance. With the operands swapped, the instance branch checks assignability against any class, sox == intis comparable whileint == xisn't.When assignability fails, both branches look for an
__eq__overload only on the left operand: on the metaclass of a class object, or on the class of an instance. Python falls back to the right operand's reflected__eq__when the left one returnsNotImplemented.narrowTypeForContainerElementTypepasses the container element as the left operand.Fix
Check assignability in either direction whenever the right operand is a class or class instance, as the instance branch does. Treat the operands as comparable when the right operand's class or metaclass overrides
__eq__, except forisandis not, which never call__eq__, and except for an__eq__declared on a built-in class, which returnsNotImplementedfor other types.Test
Cases added to
comparison2.pyfunc14objectinstancefunc15==unrelated class instancefunc16__eq__func17iswith a right operand whose class overrides__eq__func18__eq__is assigned throughselffunc19__eq__is inherited from a built-in classCases added to
unnecessaryContains1.pyfunc6objector protocol subject, class object elementsfunc7func8__eq__, unrelated class elementCases added to
typeNarrowingIn1.pyfunc35objectsubject narrows to the class objects in the container