You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Always use .enum_members to find enum members (#18675)
Closes#18565
This fixes the problem with `nonmember` and `member` special cases,
however, it required me to change one test case.
See `testEnumReachabilityPEP484ExampleSingletonWithMethod` change,
because in runtime `token` is not a member by default, at least in
recent python versions. Proof:
```python
# 3.14
>>> from enum import Enum
>>> class Empty(Enum):
... token = lambda x: x
...
>>> Empty.token
<function Empty.<lambda> at 0x101251250>
>>> Empty.token.value
```
and
```python
# 3.11
>>> from enum import Enum
>>> class Empty(Enum):
... token = lambda x: x
...
>>> Empty.token
<function Empty.<lambda> at 0x104757600>
>>> Empty.token.value
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'function' object has no attribute 'value'
```
So, I had to add `member()` there to make the test pass.
from enum import Enum, IntEnum, Flag, IntFlag, member
1625
+
1626
+
class NonEmptyEnum(Enum):
1627
+
@member
1628
+
def call(self) -> None: ...
1629
+
class NonEmptyIntEnum(IntEnum):
1630
+
@member
1631
+
def call(self) -> None: ...
1632
+
class NonEmptyFlag(Flag):
1633
+
@member
1634
+
def call(self) -> None: ...
1635
+
class NonEmptyIntFlag(IntFlag):
1636
+
@member
1637
+
def call(self) -> None: ...
1638
+
1639
+
class ErrorEnumWithoutValue(NonEmptyEnum): # E: Cannot extend enum with existing members: "NonEmptyEnum"
1640
+
pass
1641
+
class ErrorIntEnumWithoutValue(NonEmptyIntEnum): # E: Cannot extend enum with existing members: "NonEmptyIntEnum"
1642
+
pass
1643
+
class ErrorFlagWithoutValue(NonEmptyFlag): # E: Cannot extend enum with existing members: "NonEmptyFlag"
1644
+
pass
1645
+
class ErrorIntFlagWithoutValue(NonEmptyIntFlag): # E: Cannot extend enum with existing members: "NonEmptyIntFlag"
1646
+
pass
1647
+
[builtins fixtures/bool.pyi]
1648
+
1649
+
[case testEnumCanExtendEnumsWithNonMembers]
1650
+
# flags: --python-version 3.11
1651
+
from enum import Enum, IntEnum, Flag, IntFlag, nonmember
1652
+
1653
+
class NonEmptyEnum(Enum):
1654
+
x = nonmember(1)
1655
+
class NonEmptyIntEnum(IntEnum):
1656
+
x = nonmember(1)
1657
+
class NonEmptyFlag(Flag):
1658
+
x = nonmember(1)
1659
+
class NonEmptyIntFlag(IntFlag):
1660
+
x = nonmember(1)
1661
+
1662
+
class ErrorEnumWithoutValue(NonEmptyEnum):
1663
+
pass
1664
+
class ErrorIntEnumWithoutValue(NonEmptyIntEnum):
1665
+
pass
1666
+
class ErrorFlagWithoutValue(NonEmptyFlag):
1667
+
pass
1668
+
class ErrorIntFlagWithoutValue(NonEmptyIntFlag):
1669
+
pass
1670
+
[builtins fixtures/bool.pyi]
1671
+
1672
+
[case testLambdaIsNotEnumMember]
1673
+
from enum import Enum
1674
+
1675
+
class My(Enum):
1676
+
x = lambda a: a
1677
+
1678
+
class Other(My): ...
1679
+
[builtins fixtures/bool.pyi]
1680
+
1618
1681
[case testSubclassingNonFinalEnums]
1619
1682
from enum import Enum, IntEnum, Flag, IntFlag, EnumMeta
1620
1683
@@ -1839,6 +1902,10 @@ from enum import Enum
1839
1902
class A(Enum):
1840
1903
class Inner: pass
1841
1904
class B(A): pass # E: Cannot extend enum with existing members: "A"
1905
+
1906
+
class A1(Enum):
1907
+
class __Inner: pass
1908
+
class B1(A1): pass
1842
1909
[builtins fixtures/bool.pyi]
1843
1910
1844
1911
[case testEnumFinalSpecialProps]
@@ -1922,7 +1989,7 @@ from enum import Enum
1922
1989
class A(Enum): # E: Detected enum "lib.A" in a type stub with zero members. There is a chance this is due to a recent change in the semantics of enum membership. If so, use `member = value` to mark an enum member, instead of `member: type` \
1923
1990
# N: See https://typing.readthedocs.io/en/latest/spec/enums.html#defining-members
1924
1991
x: int
1925
-
class B(A): # E: Cannot extend enum with existing members: "A"
1992
+
class B(A):
1926
1993
x = 1 # E: Cannot override writable attribute "x" with a final one
0 commit comments