8000 Fix make_simplified_union for instances with last_known_value by freundTech · Pull Request #10373 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Fix make_simplified_union for instances with last_known_value #10373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 13 additions & 1 deletion mypy/typeops.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,8 @@ def make_simplified_union(items: Sequence[Type],
# Keep track of the truishness info for deleted subtypes which can be relevant
cbt = cbf = False
for j, tj in enumerate(items):
if i != j and is_proper_subtype(tj, ti, keep_erased_types=keep_erased):
if i != j and is_proper_subtype(tj, ti, keep_erased_types=keep_erased) and \
is_redundant_literal_instance(ti, tj):
# We found a redundant item in the union.
removed.add(j)
cbt = cbt or tj.can_be_true
Expand Down Expand Up @@ -805,3 +806,14 @@ def custom_special_method(typ: Type, name: str, check_all: bool = False) -> bool
return True
# TODO: support other types (see ExpressionChecker.has_member())?
return False


def is_redundant_literal_instance(general: ProperType, specific: ProperType) -> bool:
if not isinstance(general, Instance) or general.last_known_value is None:
return True
if isinstance(specific, Instance) and specific.last_known_value == general.last_known_value:
return True
if isinstance(specific, UninhabitedType):
return True

return False
2 changes: 1 addition & 1 deletion test-data/unit/check-python38.test
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def f(x: int = (c := 4)) -> int:
f(x=(y7 := 3))
reveal_type(y7) # N: Revealed type is "builtins.int"

reveal_type((lambda: (y8 := 3) and y8)()) # N: Revealed type is "Literal[3]?"
reveal_type((lambda: (y8 := 3) and y8)()) # N: Revealed type is "builtins.int"
y8 # E: Name "y8" is not defined

y7 = 1.0 # E: Incompatible types in assignment (expression has type "float", variable has type "int")
Expand Down
0