-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Fix exhaustive match checking for final class objects #11683
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
9f9dd61
c5499be
460963d
ad68a05
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 |
|---|---|---|
|
|
@@ -1334,6 +1334,29 @@ function narrowTypeBasedOnValuePattern( | |
| (subjectSubtypeExpanded) => { | ||
| // If this is a negative test, see if it's an enum value. | ||
| if (!isPositiveTest) { | ||
| if ( | ||
| isInstantiableClass(subjectSubtypeExpanded) && | ||
| isInstantiableClass(valueSubtypeExpanded) && | ||
| isSameWithoutLiteralValue(subjectSubtypeExpanded, valueSubtypeExpanded) | ||
| ) { | ||
| // A value pattern compares with ==, not identity. Ensure that the | ||
| // class has standard equality semantics by checking if its metaclass | ||
| // is the standard type (or ABCMeta) and hasn't been overridden with a | ||
| // custom metaclass that might implement a custom __eq__. | ||
| const metaclass = subjectSubtypeExpanded.shared.effectiveMetaclass; | ||
| const isStandardEquality = | ||
| !metaclass || | ||
| (isClass(metaclass) && | ||
| ClassType.isBuiltIn(metaclass, ['type', 'ABCMeta', 'EnumMeta'])); | ||
|
|
||
| if ( | ||
|
Collaborator
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.
Could this guard account for final classes with metaclasses that override |
||
| isStandardEquality && | ||
| (ClassType.isFinal(subjectSubtypeExpanded) || | ||
| !subjectSubtypeExpanded.priv.includeSubclasses) | ||
| ) { | ||
| return undefined; | ||
| } | ||
| } | ||
|
Collaborator
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.
Add regression coverage showing that a final class-object match is exhaustive and an equivalent non-final class remains non-exhaustive. This analyzer behavior change currently has no test specification. |
||
| if ( | ||
| isClassInstance(subjectSubtypeExpanded) && | ||
| isClassInstance(valueSubtypeExpanded) && | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| # pyright: reportMatchNotExhaustive=true | ||
|
|
||
| from typing import final | ||
|
|
||
| class Base: | ||
| pass | ||
|
|
||
| @final | ||
| class A1(Base): | ||
| pass | ||
|
|
||
| @final | ||
| class B1(Base): | ||
| pass | ||
|
|
||
| class NS1: | ||
| A1 = A1 | ||
| B1 = B1 | ||
|
|
||
| def exhaustive_final(inst: A1 | B1): | ||
| match type(inst): | ||
| case NS1.A1: | ||
| pass | ||
| case NS1.B1: | ||
| pass | ||
|
|
||
| class A2(Base): | ||
| pass | ||
|
|
||
| class B2(Base): | ||
| pass | ||
|
|
||
| class NS2: | ||
| A2 = A2 | ||
| B2 = B2 | ||
|
|
||
| def non_exhaustive_non_final(inst: A2 | B2): | ||
| match type(inst): | ||
| case NS2.A2: | ||
| pass | ||
| case NS2.B2: | ||
| pass | ||
|
|
||
| class Meta(type): | ||
| def __eq__(self, other): | ||
| return False | ||
|
|
||
| @final | ||
| class C1(metaclass=Meta): | ||
| pass | ||
|
|
||
| @final | ||
| class D1(metaclass=Meta): | ||
| pass | ||
|
|
||
| class NS3: | ||
| C1 = C1 | ||
| D1 = D1 | ||
|
|
||
| def non_exhaustive_custom_meta(inst: C1 | D1): | ||
| match type(inst): | ||
| case NS3.C1: | ||
| pass | ||
| case NS3.D1: | ||
| pass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This allowlist also rejects
class Meta(type): pass, so final classes using the inheritedtype.__eq__remain incorrectly non-exhaustive. Resolve__eq__through the metaclass MRO and accept it when inherited unchanged fromtype; add a no-override custom-metaclass regression to confirm the behavior.