diff --git a/mypy/checker.py b/mypy/checker.py index a425e4730604..41d0b863caa8 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -1328,9 +1328,9 @@ def check_protocol_variance(self, defn: ClassDef) -> None: tvars = info.defn.type_vars for i, tvar in enumerate(tvars): up_args = [object_type if i == j else AnyType(TypeOfAny.special_form) - for j, _ in enumerate(tvars)] + for j, _ in enumerate(tvars)] # type: List[Type] down_args = [UninhabitedType() if i == j else AnyType(TypeOfAny.special_form) - for j, _ in enumerate(tvars)] + for j, _ in enumerate(tvars)] # type: List[Type] up, down = Instance(info, up_args), Instance(info, down_args) # TODO: add advanced variance checks for recursive protocols if is_subtype(down, up, ignore_declared_variance=True): diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index d2b640016c67..a2ed8e84159a 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -2342,7 +2342,7 @@ def visit_conditional_expr(self, e: ConditionalExpr) -> Type: # branch's type. else_type = self.analyze_cond_branch(else_map, e.else_expr, context=if_type) - res = join.join_types(if_type, else_type) + res = UnionType.make_simplified_union([if_type, else_type]) return res diff --git a/test-data/unit/check-expressions.test b/test-data/unit/check-expressions.test index 94ec7dd437e5..70fd81673a82 100644 --- a/test-data/unit/check-expressions.test +++ b/test-data/unit/check-expressions.test @@ -1448,6 +1448,28 @@ x = [1] x = ['x'] # E: List item 0 has incompatible type "str"; expected "int" [builtins fixtures/list.pyi] +[case testConditionalExpressionUnion] +reveal_type(1 if bool() else 2) # E: Revealed type is 'builtins.int' +reveal_type(1 if bool() else '') # E: Revealed type is 'Union[builtins.int, builtins.str]' +class A: + pass +class B(A): + pass +class C: + pass +class D(A): + pass +a = A() +b = B() +c = C() +d = D() +reveal_type(a if bool() else b) # E: Revealed type is '__main__.A' +reveal_type(b if bool() else c) # E: Revealed type is 'Union[__main__.B, __main__.C]' +reveal_type(c if bool() else b) # E: Revealed type is 'Union[__main__.C, __main__.B]' +reveal_type(c if bool() else a) # E: Revealed type is 'Union[__main__.C, __main__.A]' +reveal_type(d if bool() else b) # E: Revealed type is 'Union[__main__.D, __main__.B]' +[builtins fixtures/bool.pyi] + -- Special cases -- ------------- diff --git a/test-data/unit/check-functions.test b/test-data/unit/check-functions.test index 0b8310c668eb..3989a6ed26cb 100644 --- a/test-data/unit/check-functions.test +++ b/test-data/unit/check-functions.test @@ -2064,8 +2064,8 @@ def f() -> None: def g(x: int) -> None: pass h = f if bool() else g -reveal_type(h) # E: Revealed type is 'builtins.function' -h(7) # E: Cannot call function of unknown type +reveal_type(h) # E: Revealed type is 'Union[def (), def (x: builtins.int)]' +h(7) # E: Too many arguments for "f" [builtins fixtures/bool.pyi] -- Positional-only arguments diff --git a/test-data/unit/check-optional.test b/test-data/unit/check-optional.test index d2f699dd0cdb..ea347a1b865d 100644 --- a/test-data/unit/check-optional.test +++ b/test-data/unit/check-optional.test @@ -385,7 +385,7 @@ def lookup_field(name, obj): attr = None [case testTernaryWithNone] -reveal_type(None if bool() else 0) # E: Revealed type is 'Union[builtins.int, builtins.None]' +reveal_type(None if bool() else 0) # E: Revealed type is 'Union[builtins.None, builtins.int]' [builtins fixtures/bool.pyi] [case testListWithNone]