Fix enum Self type in match statements and circular dependency#2675
Fix enum Self type in match statements and circular dependency#2675yangdanny97 wants to merge 1 commit intofacebook:mainfrom
Conversation
Summary: Fix two related issues with enum pattern matching: 1. Issues facebook#2080 and facebook#2604: `Type::SelfType` was not handled alongside `Type::ClassType` in narrowing, subset checking, and overload expansion. This caused false-positive "non-exhaustive match" errors when matching on `self` inside enum methods, and incorrect `Literal[A.p]` return types. 2. **Circular dependency bug**: `get_enum_members` resolved ALL class fields (including methods), creating a cycle when called during method body analysis of enum methods using `match self`. Added a `could_be_enum_member` flag to `ClassFieldProperties` so that `get_enum_members` can skip methods and nested classes without resolving their types. Fixes facebook#2080 Fixes facebook#2604 Differential Revision: D95437784
|
@yangdanny97 has exported this pull request. If you are a Meta employee, you can view the originating Diff in D95437784. |
|
Diff from mypy_primer, showing the effect of this PR on open source code: archinstall (https://github.com/archlinux/archinstall)
- ERROR archinstall/lib/hardware.py:25:11-23: Returned type `Literal[CpuVendor._Unknown]` is not assignable to declared return type `Self@CpuVendor` [bad-return]
- ERROR archinstall/lib/models/bootloader.py:34:11-28: Returned type `Literal[Bootloader.NO_BOOTLOADER]` is not assignable to declared return type `Self@Bootloader` [bad-return]
- ERROR archinstall/lib/models/bootloader.py:36:11-22: Returned type `Literal[Bootloader.Systemd]` is not assignable to declared return type `Self@Bootloader` [bad-return]
- ERROR archinstall/lib/models/bootloader.py:38:11-19: Returned type `Literal[Bootloader.Grub]` is not assignable to declared return type `Self@Bootloader` [bad-return]
- ERROR archinstall/lib/models/device.py:728:11-22: Returned type `Literal[PartitionType.Primary]` is not assignable to declared return type `Self@PartitionType` [bad-return]
- ERROR archinstall/lib/models/device.py:731:11-23: Returned type `Literal[PartitionType._Unknown]` is not assignable to declared return type `Self@PartitionType` [bad-return]
- ERROR archinstall/lib/models/users.py:61:13-23: Returned type `Literal[PasswordStrength.STRONG]` is not assignable to declared return type `Self@PasswordStrength` [bad-return]
- ERROR archinstall/lib/models/users.py:63:13-25: Returned type `Literal[PasswordStrength.MODERATE]` is not assignable to declared return type `Self@PasswordStrength` [bad-return]
- ERROR archinstall/lib/models/users.py:65:13-21: Returned type `Literal[PasswordStrength.WEAK]` is not assignable to declared return type `Self@PasswordStrength` [bad-return]
- ERROR archinstall/lib/models/users.py:67:13-26: Returned type `Literal[PasswordStrength.VERY_WEAK]` is not assignable to declared return type `Self@PasswordStrength` [bad-return]
- ERROR archinstall/lib/models/users.py:71:13-23: Returned type `Literal[PasswordStrength.STRONG]` is not assignable to declared return type `Self@PasswordStrength` [bad-return]
- ERROR archinstall/lib/models/users.py:73:13-25: Returned type `Literal[PasswordStrength.MODERATE]` is not assignable to declared return type `Self@PasswordStrength` [bad-return]
- ERROR archinstall/lib/models/users.py:75:13-21: Returned type `Literal[PasswordStrength.WEAK]` is not assignable to declared return type `Self@PasswordStrength` [bad-return]
- ERROR archinstall/lib/models/users.py:77:13-26: Returned type `Literal[PasswordStrength.VERY_WEAK]` is not assignable to declared return type `Self@PasswordStrength` [bad-return]
- ERROR archinstall/lib/models/users.py:81:13-23: Returned type `Literal[PasswordStrength.STRONG]` is not assignable to declared return type `Self@PasswordStrength` [bad-return]
- ERROR archinstall/lib/models/users.py:83:13-25: Returned type `Literal[PasswordStrength.MODERATE]` is not assignable to declared return type `Self@PasswordStrength` [bad-return]
- ERROR archinstall/lib/models/users.py:85:13-21: Returned type `Literal[PasswordStrength.WEAK]` is not assignable to declared return type `Self@PasswordStrength` [bad-return]
- ERROR archinstall/lib/models/users.py:87:13-26: Returned type `Literal[PasswordStrength.VERY_WEAK]` is not assignable to declared return type `Self@PasswordStrength` [bad-return]
- ERROR archinstall/lib/models/users.py:91:13-23: Returned type `Literal[PasswordStrength.STRONG]` is not assignable to declared return type `Self@PasswordStrength` [bad-return]
- ERROR archinstall/lib/models/users.py:93:13-25: Returned type `Literal[PasswordStrength.MODERATE]` is not assignable to declared return type `Self@PasswordStrength` [bad-return]
- ERROR archinstall/lib/models/users.py:95:13-21: Returned type `Literal[PasswordStrength.WEAK]` is not assignable to declared return type `Self@PasswordStrength` [bad-return]
- ERROR archinstall/lib/models/users.py:97:13-26: Returned type `Literal[PasswordStrength.VERY_WEAK]` is not assignable to declared return type `Self@PasswordStrength` [bad-return]
- ERROR archinstall/lib/models/users.py:99:10-23: Returned type `Literal[PasswordStrength.VERY_WEAK]` is not assignable to declared return type `Self@PasswordStrength` [bad-return]
mitmproxy (https://github.com/mitmproxy/mitmproxy)
- ERROR mitmproxy/proxy/layers/http/_events.py:127:37-42: Argument `Self@ErrorCode` is not assignable to parameter `arg` with type `Never` in function `typing.assert_never` [bad-argument-type]
jax (https://github.com/google/jax)
+ WARN jax/_src/pallas/mosaic/tpu_info.py:92:16-63: Redundant cast: `int` is the same type as `int` [redundant-cast]
|
Primer Diff Classification✅ 3 improvement(s) | 3 project(s) total 3 improvement(s) across archinstall, mitmproxy, jax.
Detailed analysis✅ Improvement (3)archinstall (-23)
The PR description explains this was issue #2604 where
mitmproxy (-1)
jax (+1)
Was this helpful? React with 👍 or 👎 Classification by primer-classifier (3 LLM) |
Summary:
Fix two related issues with enum pattern matching:
Issues non-exhaustive-match false positive for utility / "self" functions on the enum. #2080 and Fake enum literal type #2604:
Type::SelfTypewas not handled alongsideType::ClassTypein narrowing, subset checking, and overload expansion.This caused false-positive "non-exhaustive match" errors when matching
on
selfinside enum methods, and incorrectLiteral[A.p]returntypes.
Circular dependency bug:
get_enum_membersresolved ALL classfields (including methods), creating a cycle when called during
method body analysis of enum methods using
match self. Added acould_be_enum_memberflag toClassFieldPropertiesso thatget_enum_memberscan skip methods and nested classes withoutresolving their types.
Fixes #2080
Fixes #2604
Differential Revision: D95437784