Commit 6cb8e79
committed
Fix type narrowing for @Final classes with identity checks
Fixes #20590
When using 'is' operator or match/case patterns to check type objects
of @Final classes, mypy now properly narrows the types since @Final
classes cannot have subclasses at runtime.
Changes:
- typeops.py: Updated is_singleton_identity_type() to recognize
CallableType and TypeType representing @Final class constructors
as singleton types for identity-based narrowing
- checker.py: Modified narrowing logic to:
1. Allow @Final class type objects in narrowable_operand check
2. Preserve else-branch narrowing for @Final class identity checks
since no subclasses can exist
This enables exhaustiveness checking with assert_never() when all
@Final class cases are handled in if/elif or match/case branches.1 parent 639fcde commit 6cb8e79
2 files changed
+17
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6567 | 6567 | | |
6568 | 6568 | | |
6569 | 6569 | | |
| 6570 | + | |
6570 | 6571 | | |
6571 | 6572 | | |
6572 | 6573 | | |
| |||
6803 | 6804 | | |
6804 | 6805 | | |
6805 | 6806 | | |
6806 | | - | |
| 6807 | + | |
| 6808 | + | |
| 6809 | + | |
| 6810 | + | |
| 6811 | + | |
| 6812 | + | |
6807 | 6813 | | |
6808 | 6814 | | |
6809 | 6815 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1004 | 1004 | | |
1005 | 1005 | | |
1006 | 1006 | | |
| 1007 | + | |
| 1008 | + | |
| 1009 | + | |
| 1010 | + | |
| 1011 | + | |
| 1012 | + | |
| 1013 | + | |
| 1014 | + | |
| 1015 | + | |
| 1016 | + | |
1007 | 1017 | | |
1008 | 1018 | | |
1009 | 1019 | | |
| |||
0 commit comments