You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: test-data/unit/check-literal.test
+18-46Lines changed: 18 additions & 46 deletions
Original file line number
Diff line number
Diff line change
@@ -2949,48 +2949,20 @@ reveal_type(C().collection) # N: Revealed type is "builtins.list[Literal['word'
2949
2949
reveal_type(C().word) # N: Revealed type is "Literal['word']"
2950
2950
[builtins fixtures/tuple.pyi]
2951
2951
2952
-
[case testStringLiteralTernary]
2953
-
# https://github.com/python/mypy/issues/19534
2954
-
def test(b: bool) -> None:
2955
-
l = "foo" if b else "bar"
2956
-
reveal_type(l) # N: Revealed type is "builtins.str"
2957
-
[builtins fixtures/tuple.pyi]
2958
-
2959
-
[case testintLiteralTernary]
2960
-
# https://github.com/python/mypy/issues/19534
2961
-
def test(b: bool) -> None:
2962
-
l = 0 if b else 1
2963
-
reveal_type(l) # N: Revealed type is "builtins.int"
2964
-
[builtins fixtures/tuple.pyi]
2965
-
2966
2952
[case testStringIntUnionTernary]
2967
2953
# https://github.com/python/mypy/issues/19534
2968
2954
def test(b: bool) -> None:
2969
2955
l = 1 if b else "a"
2970
-
reveal_type(l) # N: Revealed type is "Union[builtins.int, builtins.str]"
2956
+
reveal_type(l) # N: Revealed type is "builtins.int | builtins.str"
2971
2957
[builtins fixtures/tuple.pyi]
2972
2958
2973
2959
[case testListComprehensionTernary]
2974
2960
# https://github.com/python/mypy/issues/19534
2975
2961
def test(b: bool) -> None:
2976
2962
l = [1] if b else ["a"]
2977
-
reveal_type(l) # N: Revealed type is "Union[builtins.list[builtins.int], builtins.list[builtins.str]]"
2963
+
reveal_type(l) # N: Revealed type is "builtins.list[builtins.int] | builtins.list[builtins.str]"
2978
2964
[builtins fixtures/list.pyi]
2979
2965
2980
-
[case testSetComprehensionTernary]
2981
-
# https://github.com/python/mypy/issues/19534
2982
-
def test(b: bool) -> None:
2983
-
s = {1} if b else {"a"}
2984
-
reveal_type(s) # N: Revealed type is "Union[builtins.set[builtins.int], builtins.set[builtins.str]]"
2985
-
[builtins fixtures/set.pyi]
2986
-
2987
-
[case testDictComprehensionTernary]
2988
-
# https://github.com/python/mypy/issues/19534
2989
-
def test(b: bool) -> None:
2990
-
d = {1:1} if "" else {"a": "a"}
2991
-
reveal_type(d) # N: Revealed type is "Union[builtins.dict[builtins.int, builtins.int], builtins.dict[builtins.str, builtins.str]]"
2992
-
[builtins fixtures/dict.pyi]
2993
-
2994
2966
[case testLambdaTernary]
2995
2967
from typing import TypeVar, Union, Callable, reveal_type
2996
2968
@@ -3034,23 +3006,23 @@ class B:
3034
3006
def test_static_with_attr(x: B) -> None:
3035
3007
def foo(t: A) -> None: ...
3036
3008
3037
-
l1: Callable[[], None] = (lambda: foo(x.attr)) if x.attr is not None else NOOP # E: Argument 1 to "foo" has incompatible type "Optional[A]"; expected "A"
3038
-
r1: Callable[[], None] = NOOP if x.attr is None else (lambda: foo(x.attr)) # E: Argument 1 to "foo" has incompatible type "Optional[A]"; expected "A"
3039
-
l2 = (lambda: foo(x.attr)) if x.attr is not None else NOOP # E: Argument 1 to "foo" has incompatible type "Optional[A]"; expected "A"
3040
-
r2 = NOOP if x.attr is None else (lambda: foo(x.attr)) # E: Argument 1 to "foo" has incompatible type "Optional[A]"; expected "A"
3009
+
l1: Callable[[], None] = (lambda: foo(x.attr)) if x.attr is not None else NOOP # E: Argument 1 to "foo" has incompatible type "A | None"; expected "A"
3010
+
r1: Callable[[], None] = NOOP if x.attr is None else (lambda: foo(x.attr)) # E: Argument 1 to "foo" has incompatible type "A | None"; expected "A"
3011
+
l2 = (lambda: foo(x.attr)) if x.attr is not None else NOOP # E: Argument 1 to "foo" has incompatible type "A | None"; expected "A"
3012
+
r2 = NOOP if x.attr is None else (lambda: foo(x.attr)) # E: Argument 1 to "foo" has incompatible type "A | None"; expected "A"
3041
3013
reveal_type(l2) # N: Revealed type is "def ()"
3042
3014
reveal_type(r2) # N: Revealed type is "def ()"
3043
3015
3044
3016
def test_generic_with_attr(x: B) -> None:
3045
3017
T = TypeVar("T")
3046
3018
def bar(t: T) -> T: return t
3047
3019
3048
-
l1: Callable[[], None] = (lambda: bar(x.attr)) if x.attr is None else NOOP # E: Incompatible types in assignment (expression has type "Callable[[], Optional[A]]", variable has type "Callable[[], None]")
3049
-
r1: Callable[[], None] = NOOP if x.attr is not None else (lambda: bar(x.attr)) # E: Incompatible types in assignment (expression has type "Callable[[], Optional[A]]", variable has type "Callable[[], None]")
3020
+
l1: Callable[[], None] = (lambda: bar(x.attr)) if x.attr is None else NOOP # E: Incompatible types in assignment (expression has type "Callable[[], A | None]", variable has type "Callable[[], None]")
3021
+
r1: Callable[[], None] = NOOP if x.attr is not None else (lambda: bar(x.attr)) # E: Incompatible types in assignment (expression has type "Callable[[], A | None]", variable has type "Callable[[], None]")
3050
3022
l2 = (lambda: bar(x.attr)) if x.attr is None else NOOP
3051
3023
r2 = NOOP if x.attr is not None else (lambda: bar(x.attr))
3052
-
reveal_type(l2) # N: Revealed type is "def () -> Union[__main__.A, None]"
3053
-
reveal_type(r2) # N: Revealed type is "def () -> Union[__main__.A, None]"
3024
+
reveal_type(l2) # N: Revealed type is "def () -> __main__.A | None"
3025
+
reveal_type(r2) # N: Revealed type is "def () -> __main__.A | None"
3054
3026
3055
3027
[case testLambdaTernaryDoubleIndirectAttribute]
3056
3028
# fails due to binder issue inside `check_func_def`
@@ -3067,23 +3039,23 @@ class C:
3067
3039
def test_static_with_attr(x: C) -> None:
3068
3040
def foo(t: A) -> None: ...
3069
3041
3070
-
l1: Callable[[], None] = (lambda: foo(x.attr.attr)) if x.attr.attr is not None else NOOP # E: Argument 1 to "foo" has incompatible type "Optional[A]"; expected "A"
3071
-
r1: Callable[[], None] = NOOP if x.attr.attr is None else (lambda: foo(x.attr.attr)) # E: Argument 1 to "foo" has incompatible type "Optional[A]"; expected "A"
3072
-
l2 = (lambda: foo(x.attr.attr)) if x.attr.attr is not None else NOOP # E: Argument 1 to "foo" has incompatible type "Optional[A]"; expected "A"
3073
-
r2 = NOOP if x.attr.attr is None else (lambda: foo(x.attr.attr)) # E: Argument 1 to "foo" has incompatible type "Optional[A]"; expected "A"
3042
+
l1: Callable[[], None] = (lambda: foo(x.attr.attr)) if x.attr.attr is not None else NOOP # E: Argument 1 to "foo" has incompatible type "A | None"; expected "A"
3043
+
r1: Callable[[], None] = NOOP if x.attr.attr is None else (lambda: foo(x.attr.attr)) # E: Argument 1 to "foo" has incompatible type "A | None"; expected "A"
3044
+
l2 = (lambda: foo(x.attr.attr)) if x.attr.attr is not None else NOOP # E: Argument 1 to "foo" has incompatible type "A | None"; expected "A"
3045
+
r2 = NOOP if x.attr.attr is None else (lambda: foo(x.attr.attr)) # E: Argument 1 to "foo" has incompatible type "A | None"; expected "A"
3074
3046
reveal_type(l2) # N: Revealed type is "def ()"
3075
3047
reveal_type(r2) # N: Revealed type is "def ()"
3076
3048
3077
3049
def test_generic_with_attr(x: C) -> None:
3078
3050
T = TypeVar("T")
3079
3051
def bar(t: T) -> T: return t
3080
3052
3081
-
l1: Callable[[], None] = (lambda: bar(x.attr.attr)) if x.attr.attr is None else NOOP # E: Incompatible types in assignment (expression has type "Callable[[], Optional[A]]", variable has type "Callable[[], None]")
3082
-
r1: Callable[[], None] = NOOP if x.attr.attr is not None else (lambda: bar(x.attr.attr)) # E: Incompatible types in assignment (expression has type "Callable[[], Optional[A]]", variable has type "Callable[[], None]")
3053
+
l1: Callable[[], None] = (lambda: bar(x.attr.attr)) if x.attr.attr is None else NOOP # E: Incompatible types in assignment (expression has type "Callable[[], A | None]", variable has type "Callable[[], None]")
3054
+
r1: Callable[[], None] = NOOP if x.attr.attr is not None else (lambda: bar(x.attr.attr)) # E: Incompatible types in assignment (expression has type "Callable[[], A | None]", variable has type "Callable[[], None]")
3083
3055
l2 = (lambda: bar(x.attr.attr)) if x.attr.attr is None else NOOP
3084
3056
r2 = NOOP if x.attr.attr is not None else (lambda: bar(x.attr.attr))
3085
-
reveal_type(l2) # N: Revealed type is "def () -> Union[__main__.A, None]"
3086
-
reveal_type(r2) # N: Revealed type is "def () -> Union[__main__.A, None]"
3057
+
reveal_type(l2) # N: Revealed type is "def () -> __main__.A | None"
3058
+
reveal_type(r2) # N: Revealed type is "def () -> __main__.A | None"
0 commit comments