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
+109Lines changed: 109 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -2949,6 +2949,115 @@ 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 testStringIntUnionTernary]
2953
+
# https://github.com/python/mypy/issues/19534
2954
+
def test(b: bool) -> None:
2955
+
l = 1 if b else "a"
2956
+
reveal_type(l) # N: Revealed type is "builtins.int | builtins.str"
2957
+
[builtins fixtures/tuple.pyi]
2958
+
2959
+
[case testListComprehensionTernary]
2960
+
# https://github.com/python/mypy/issues/19534
2961
+
def test(b: bool) -> None:
2962
+
l = [1] if b else ["a"]
2963
+
reveal_type(l) # N: Revealed type is "builtins.list[builtins.int] | builtins.list[builtins.str]"
2964
+
[builtins fixtures/list.pyi]
2965
+
2966
+
[case testLambdaTernary]
2967
+
from typing import TypeVar, Union, Callable, reveal_type
2968
+
2969
+
NOOP = lambda: None
2970
+
class A: pass
2971
+
class B:
2972
+
attr: Union[A, None]
2973
+
2974
+
def test_static(x: Union[A, None]) -> None:
2975
+
def foo(t: A) -> None: ...
2976
+
2977
+
l1: Callable[[], object] = (lambda: foo(x)) if x is not None else NOOP
2978
+
r1: Callable[[], object] = NOOP if x is None else (lambda: foo(x))
2979
+
l2 = (lambda: foo(x)) if x is not None else NOOP
2980
+
r2 = NOOP if x is None else (lambda: foo(x))
2981
+
reveal_type(l2) # N: Revealed type is "def ()"
2982
+
reveal_type(r2) # N: Revealed type is "def ()"
2983
+
2984
+
def test_generic(x: Union[A, None]) -> None:
2985
+
T = TypeVar("T")
2986
+
def bar(t: T) -> T: return t
2987
+
2988
+
l1: Callable[[], None] = (lambda: bar(x)) if x is None else NOOP
2989
+
r1: Callable[[], None] = NOOP if x is not None else (lambda: bar(x))
2990
+
l2 = (lambda: bar(x)) if x is None else NOOP
2991
+
r2 = NOOP if x is not None else (lambda: bar(x))
2992
+
reveal_type(l2) # N: Revealed type is "def ()"
2993
+
reveal_type(r2) # N: Revealed type is "def ()"
2994
+
2995
+
2996
+
[case testLambdaTernaryIndirectAttribute]
2997
+
# fails due to binder issue inside `check_func_def`
2998
+
# https://github.com/python/mypy/issues/19561
2999
+
from typing import TypeVar, Union, Callable, reveal_type
3000
+
3001
+
NOOP = lambda: None
3002
+
class A: pass
3003
+
class B:
3004
+
attr: Union[A, None]
3005
+
3006
+
def test_static_with_attr(x: B) -> None:
3007
+
def foo(t: A) -> None: ...
3008
+
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"
3013
+
reveal_type(l2) # N: Revealed type is "def ()"
3014
+
reveal_type(r2) # N: Revealed type is "def ()"
3015
+
3016
+
def test_generic_with_attr(x: B) -> None:
3017
+
T = TypeVar("T")
3018
+
def bar(t: T) -> T: return t
3019
+
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]")
3022
+
l2 = (lambda: bar(x.attr)) if x.attr is None else NOOP
3023
+
r2 = NOOP if x.attr is not None else (lambda: bar(x.attr))
3024
+
reveal_type(l2) # N: Revealed type is "def () -> __main__.A | None"
3025
+
reveal_type(r2) # N: Revealed type is "def () -> __main__.A | None"
3026
+
3027
+
[case testLambdaTernaryDoubleIndirectAttribute]
3028
+
# fails due to binder issue inside `check_func_def`
3029
+
# https://github.com/python/mypy/issues/19561
3030
+
from typing import TypeVar, Union, Callable, reveal_type
3031
+
3032
+
NOOP = lambda: None
3033
+
class A: pass
3034
+
class B:
3035
+
attr: Union[A, None]
3036
+
class C:
3037
+
attr: B
3038
+
3039
+
def test_static_with_attr(x: C) -> None:
3040
+
def foo(t: A) -> None: ...
3041
+
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"
3046
+
reveal_type(l2) # N: Revealed type is "def ()"
3047
+
reveal_type(r2) # N: Revealed type is "def ()"
3048
+
3049
+
def test_generic_with_attr(x: C) -> None:
3050
+
T = TypeVar("T")
3051
+
def bar(t: T) -> T: return t
3052
+
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]")
3055
+
l2 = (lambda: bar(x.attr.attr)) if x.attr.attr is None else NOOP
3056
+
r2 = NOOP if x.attr.attr is not None else (lambda: bar(x.attr.attr))
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