Skip to content

Commit

Permalink
adjust test case and fix enums without members being collapsed to nev…
Browse files Browse the repository at this point in the history
…er which is also the base of all types
  • Loading branch information
terencehonles committed Oct 28, 2024
1 parent 4e3efbc commit d68b5d1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
14 changes: 6 additions & 8 deletions mypy/typeops.py
Original file line number Diff line number Diff line change
Expand Up @@ -897,18 +897,16 @@ class Status(Enum):
items = [
try_expanding_sum_type_to_union(item, target_fullname) for item in typ.relevant_items()
]
return make_simplified_union(items, contract_literals=False)
elif isinstance(typ, Instance) and typ.type.fullname == target_fullname:
if typ.type.is_enum:
return make_simplified_union(
[LiteralType(name, typ) for name in typ.get_enum_values()], contract_literals=False
)
items = [LiteralType(name, typ) for name in typ.get_enum_values()]
elif typ.type.fullname == "builtins.bool":
return make_simplified_union(
[LiteralType(True, typ), LiteralType(False, typ)], contract_literals=False
)
items = [LiteralType(True, typ), LiteralType(False, typ)]
else:
return typ

return typ
# if the expanded union would be `Never` leave the type as is
return typ if not items else make_simplified_union(items, contract_literals=False)


def try_contracting_literals_in_union(types: Sequence[Type]) -> list[ProperType]:
Expand Down
10 changes: 4 additions & 6 deletions test-data/unit/check-enum.test
Original file line number Diff line number Diff line change
Expand Up @@ -2230,24 +2230,22 @@ reveal_type(Foo.Baz.value) # N: Revealed type is "Any"
[typing fixtures/typing-full.pyi]


[case testEnumWithAnnotationOnly]
[case testEnumWithImplicitMembersUsingAnnotationOnly]
# flags: --warn-unreachable
import enum


class E(enum.IntEnum):
A: int # E: Enum members must be left unannotated \
# N: See https://typing.readthedocs.io/en/latest/spec/enums.html#defining-members
B: int # E: Enum members must be left unannotated \
# N: See https://typing.readthedocs.io/en/latest/spec/enums.html#defining-members
A: int
B: int


def do_check(value: E) -> None:
reveal_type(value) # N: Revealed type is "__main__.E"
if value is E.A:
return

reveal_type(value) # N: Revealed type is "Literal[__main__.E.B]"
reveal_type(value) # N: Revealed type is "__main__.E"
"should be reachable"

[builtins fixtures/primitives.pyi]
Expand Down

0 comments on commit d68b5d1

Please sign in to comment.