diff --git a/mypy/typeops.py b/mypy/typeops.py index 3b5ca73f8713..1760e9c00503 100644 --- a/mypy/typeops.py +++ b/mypy/typeops.py @@ -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 @@ -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 diff --git a/test-data/unit/check-python38.test b/test-data/unit/check-python38.test index 1e9b6ea66f28..ee2f0721ab62 100644 --- a/test-data/unit/check-python38.test +++ b/test-data/unit/check-python38.test @@ -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")