Skip to content

Commit

Permalink
Properly account for member and nonmember in `TypeInfo.enum_membe…
Browse files Browse the repository at this point in the history
…rs` (#18559)

Closes #18557

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
sobolevn and pre-commit-ci[bot] authored Feb 14, 2025
1 parent 763185d commit f404b16
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
24 changes: 20 additions & 4 deletions mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3247,10 +3247,26 @@ def enum_members(self) -> list[str]:
name
for name, sym in self.names.items()
if (
isinstance(sym.node, Var)
and name not in EXCLUDED_ENUM_ATTRIBUTES
and not name.startswith("__")
and sym.node.has_explicit_value
(
isinstance(sym.node, Var)
and name not in EXCLUDED_ENUM_ATTRIBUTES
and not name.startswith("__")
and sym.node.has_explicit_value
and not (
isinstance(
typ := mypy.types.get_proper_type(sym.node.type), mypy.types.Instance
)
and typ.type.fullname == "enum.nonmember"
)
)
or (
isinstance(sym.node, Decorator)
and any(
dec.fullname == "enum.member"
for dec in sym.node.decorators
if isinstance(dec, RefExpr)
)
)
)
]

Expand Down
2 changes: 1 addition & 1 deletion mypy/typeops.py
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,7 @@ class Status(Enum):
FAILURE = 2
UNKNOWN = 3
...and if we call `try_expanding_enum_to_union(Union[Color, Status], 'module.Color')`,
...and if we call `try_expanding_sum_type_to_union(Union[Color, Status], 'module.Color')`,
this function will return Literal[Color.RED, Color.BLUE, Color.YELLOW, Status].
"""
typ = get_proper_type(typ)
Expand Down
34 changes: 34 additions & 0 deletions test-data/unit/check-enum.test
Original file line number Diff line number Diff line change
Expand Up @@ -1933,6 +1933,18 @@ class D(C): # E: Cannot extend enum with existing members: "C" \
x: int # E: Cannot assign to final name "x"
[builtins fixtures/bool.pyi]

[case testEnumNotFinalWithMethodsAndUninitializedValuesStubMember]
# flags: --python-version 3.11
# This was added in 3.11
import lib

[file lib.pyi]
from enum import Enum, member
class A(Enum):
@member
def x(self) -> None: ...
[builtins fixtures/bool.pyi]

[case testEnumLiteralValues]
from enum import Enum

Expand Down Expand Up @@ -2325,6 +2337,28 @@ def some_a(a: A):
[builtins fixtures/dict.pyi]


[case testEnumMemberAndNonMemberSupport]
# flags: --python-version 3.11 --warn-unreachable
# This was added in 3.11
from enum import Enum, member, nonmember

class A(Enum):
x = 1
y = member(2)
z = nonmember(3)

def some_a(a: A):
if a is not A.x and a is not A.z:
reveal_type(a) # N: Revealed type is "Literal[__main__.A.y]"
if a is not A.y and a is not A.z:
reveal_type(a) # N: Revealed type is "Literal[__main__.A.x]"
if a is not A.x:
reveal_type(a) # N: Revealed type is "Literal[__main__.A.y]"
if a is not A.y:
reveal_type(a) # N: Revealed type is "Literal[__main__.A.x]"
[builtins fixtures/dict.pyi]


[case testErrorOnAnnotatedMember]
from enum import Enum

Expand Down

0 comments on commit f404b16

Please sign in to comment.