@@ -2946,6 +2946,140 @@ reveal_type(C().collection) # N: Revealed type is "builtins.list[Literal['word'
29462946reveal_type(C().word) # N: Revealed type is "Literal['word']"
29472947[builtins fixtures/tuple.pyi]
29482948
2949+ [case testStringLiteralTernary]
2950+ def test(b: bool) -> None:
2951+ l = "foo" if b else "bar"
2952+ reveal_type(l) # N: Revealed type is "builtins.str"
2953+ [builtins fixtures/tuple.pyi]
2954+
2955+ [case testintLiteralTernary]
2956+ def test(b: bool) -> None:
2957+ l = 0 if b else 1
2958+ reveal_type(l) # N: Revealed type is "builtins.int"
2959+ [builtins fixtures/tuple.pyi]
2960+
2961+ [case testStringIntUnionTernary]
2962+ def test(b: bool) -> None:
2963+ l = 1 if b else "a"
2964+ reveal_type(l) # N: Revealed type is "Union[builtins.int, builtins.str]"
2965+ [builtins fixtures/tuple.pyi]
2966+
2967+ [case testListComprehensionTernary]
2968+ # gh-19534
2969+ def test(b: bool) -> None:
2970+ l = [1] if b else ["a"]
2971+ reveal_type(l) # N: Revealed type is "Union[builtins.list[builtins.int], builtins.list[builtins.str]]"
2972+ [builtins fixtures/list.pyi]
2973+
2974+ [case testSetComprehensionTernary]
2975+ # gh-19534
2976+ def test(b: bool) -> None:
2977+ s = {1} if b else {"a"}
2978+ reveal_type(s) # N: Revealed type is "Union[builtins.set[builtins.int], builtins.set[builtins.str]]"
2979+ [builtins fixtures/set.pyi]
2980+
2981+ [case testDictComprehensionTernary]
2982+ # gh-19534
2983+ def test(b: bool) -> None:
2984+ d = {1:1} if "" else {"a": "a"}
2985+ reveal_type(d) # N: Revealed type is "Union[builtins.dict[builtins.int, builtins.int], builtins.dict[builtins.str, builtins.str]]"
2986+ [builtins fixtures/dict.pyi]
2987+
2988+ [case testLambdaTernary]
2989+ from typing import TypeVar, Union, Callable, reveal_type
2990+
2991+ NOOP = lambda: None
2992+ class A: pass
2993+ class B:
2994+ attr: Union[A, None]
2995+
2996+ def test_static(x: Union[A, None]) -> None:
2997+ def foo(t: A) -> None: ...
2998+
2999+ l1: Callable[[], object] = (lambda: foo(x)) if x is not None else NOOP
3000+ r1: Callable[[], object] = NOOP if x is None else (lambda: foo(x))
3001+ l2 = (lambda: foo(x)) if x is not None else NOOP
3002+ r2 = NOOP if x is None else (lambda: foo(x))
3003+ reveal_type(l2) # N: Revealed type is "def ()"
3004+ reveal_type(r2) # N: Revealed type is "def ()"
3005+
3006+ def test_generic(x: Union[A, None]) -> None:
3007+ T = TypeVar("T")
3008+ def bar(t: T) -> T: return t
3009+
3010+ l1: Callable[[], None] = (lambda: bar(x)) if x is None else NOOP
3011+ r1: Callable[[], None] = NOOP if x is not None else (lambda: bar(x))
3012+ l2 = (lambda: bar(x)) if x is None else NOOP
3013+ r2 = NOOP if x is not None else (lambda: bar(x))
3014+ reveal_type(l2) # N: Revealed type is "def ()"
3015+ reveal_type(r2) # N: Revealed type is "def ()"
3016+
3017+
3018+ [case testLambdaTernaryIndirectAttribute]
3019+ # fails due to binder issue inside `check_func_def`
3020+ # gh-19561
3021+ from typing import TypeVar, Union, Callable, reveal_type
3022+
3023+ NOOP = lambda: None
3024+ class A: pass
3025+ class B:
3026+ attr: Union[A, None]
3027+
3028+ def test_static_with_attr(x: B) -> None:
3029+ def foo(t: A) -> None: ...
3030+
3031+ l1: Callable[[], None] = (lambda: foo(x.attr)) if x.attr is not None else NOOP
3032+ r1: Callable[[], None] = NOOP if x.attr is None else (lambda: foo(x.attr))
3033+ l2 = (lambda: foo(x.attr)) if x.attr is not None else NOOP
3034+ r2 = NOOP if x.attr is None else (lambda: foo(x.attr))
3035+ reveal_type(l2) # N: Revealed type is "def ()"
3036+ reveal_type(r2) # N: Revealed type is "def ()"
3037+
3038+ def test_generic_with_attr(x: B) -> None:
3039+ T = TypeVar("T")
3040+ def bar(t: T) -> T: return t
3041+
3042+ l1: Callable[[], None] = (lambda: bar(x.attr)) if x.attr is None else NOOP
3043+ r1: Callable[[], None] = NOOP if x.attr is not None else (lambda: bar(x.attr))
3044+ l2 = (lambda: bar(x.attr)) if x.attr is None else NOOP
3045+ r2 = NOOP if x.attr is not None else (lambda: bar(x.attr))
3046+ reveal_type(l2) # N: Revealed type is "def ()"
3047+ reveal_type(r2) # N: Revealed type is "def ()"
3048+
3049+ [case testLambdaTernaryDoubleIndirectAttribute]
3050+ # fails due to binder issue inside `check_func_def`
3051+ # gh-19561
3052+ from typing import TypeVar, Union, Callable, reveal_type
3053+
3054+ NOOP = lambda: None
3055+ class A: pass
3056+ class B:
3057+ attr: Union[A, None]
3058+ class C:
3059+ attr: B
3060+
3061+ def test_static_with_attr(x: C) -> None:
3062+ def foo(t: A) -> None: ...
3063+
3064+ l1: Callable[[], None] = (lambda: foo(x.attr.attr)) if x.attr.attr is not None else NOOP
3065+ r1: Callable[[], None] = NOOP if x.attr.attr is None else (lambda: foo(x.attr.attr))
3066+ l2 = (lambda: foo(x.attr.attr)) if x.attr.attr is not None else NOOP
3067+ r2 = NOOP if x.attr.attr is None else (lambda: foo(x.attr.attr))
3068+ reveal_type(l2) # N: Revealed type is "def ()"
3069+ reveal_type(r2) # N: Revealed type is "def ()"
3070+
3071+ def test_generic_with_attr(x: C) -> None:
3072+ T = TypeVar("T")
3073+ def bar(t: T) -> T: return t
3074+
3075+ l1: Callable[[], None] = (lambda: bar(x.attr.attr)) if x.attr.attr is None else NOOP
3076+ r1: Callable[[], None] = NOOP if x.attr.attr is not None else (lambda: bar(x.attr.attr))
3077+ l2 = (lambda: bar(x.attr.attr)) if x.attr.attr is None else NOOP
3078+ r2 = NOOP if x.attr.attr is not None else (lambda: bar(x.attr.attr))
3079+ reveal_type(l2) # N: Revealed type is "def ()"
3080+ reveal_type(r2) # N: Revealed type is "def ()"
3081+
3082+
29493083[case testLiteralTernaryUnionNarrowing]
29503084from typing import Literal, Optional
29513085
0 commit comments