Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
adjust test case and fix enums without members being collapsed to nev…
…er which is also the base of all types
  • Loading branch information
terencehonles committed Oct 28, 2024
commit d68b5d1a8c6476d690be4569b51997be574e44be
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