Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 18 additions & 46 deletions test-data/unit/check-literal.test
Original file line number Diff line number Diff line change
Expand Up @@ -2949,48 +2949,20 @@ reveal_type(C().collection) # N: Revealed type is "builtins.list[Literal['word'
reveal_type(C().word) # N: Revealed type is "Literal['word']"
[builtins fixtures/tuple.pyi]

[case testStringLiteralTernary]
# https://github.com/python/mypy/issues/19534
def test(b: bool) -> None:
l = "foo" if b else "bar"
reveal_type(l) # N: Revealed type is "builtins.str"
[builtins fixtures/tuple.pyi]

[case testintLiteralTernary]
# https://github.com/python/mypy/issues/19534
def test(b: bool) -> None:
l = 0 if b else 1
reveal_type(l) # N: Revealed type is "builtins.int"
[builtins fixtures/tuple.pyi]

[case testStringIntUnionTernary]
# https://github.com/python/mypy/issues/19534
def test(b: bool) -> None:
l = 1 if b else "a"
reveal_type(l) # N: Revealed type is "Union[builtins.int, builtins.str]"
reveal_type(l) # N: Revealed type is "builtins.int | builtins.str"
[builtins fixtures/tuple.pyi]

[case testListComprehensionTernary]
# https://github.com/python/mypy/issues/19534
def test(b: bool) -> None:
l = [1] if b else ["a"]
reveal_type(l) # N: Revealed type is "Union[builtins.list[builtins.int], builtins.list[builtins.str]]"
reveal_type(l) # N: Revealed type is "builtins.list[builtins.int] | builtins.list[builtins.str]"
[builtins fixtures/list.pyi]

[case testSetComprehensionTernary]
# https://github.com/python/mypy/issues/19534
def test(b: bool) -> None:
s = {1} if b else {"a"}
reveal_type(s) # N: Revealed type is "Union[builtins.set[builtins.int], builtins.set[builtins.str]]"
[builtins fixtures/set.pyi]

[case testDictComprehensionTernary]
# https://github.com/python/mypy/issues/19534
def test(b: bool) -> None:
d = {1:1} if "" else {"a": "a"}
reveal_type(d) # N: Revealed type is "Union[builtins.dict[builtins.int, builtins.int], builtins.dict[builtins.str, builtins.str]]"
[builtins fixtures/dict.pyi]

[case testLambdaTernary]
from typing import TypeVar, Union, Callable, reveal_type

Expand Down Expand Up @@ -3034,23 +3006,23 @@ class B:
def test_static_with_attr(x: B) -> None:
def foo(t: A) -> None: ...

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"
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"
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"
r2 = NOOP if x.attr is None else (lambda: foo(x.attr)) # E: Argument 1 to "foo" has incompatible type "Optional[A]"; expected "A"
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"
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"
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"
r2 = NOOP if x.attr is None else (lambda: foo(x.attr)) # E: Argument 1 to "foo" has incompatible type "A | None"; expected "A"
reveal_type(l2) # N: Revealed type is "def ()"
reveal_type(r2) # N: Revealed type is "def ()"

def test_generic_with_attr(x: B) -> None:
T = TypeVar("T")
def bar(t: T) -> T: return t

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]")
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]")
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]")
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]")
l2 = (lambda: bar(x.attr)) if x.attr is None else NOOP
r2 = NOOP if x.attr is not None else (lambda: bar(x.attr))
reveal_type(l2) # N: Revealed type is "def () -> Union[__main__.A, None]"
reveal_type(r2) # N: Revealed type is "def () -> Union[__main__.A, None]"
reveal_type(l2) # N: Revealed type is "def () -> __main__.A | None"
reveal_type(r2) # N: Revealed type is "def () -> __main__.A | None"

[case testLambdaTernaryDoubleIndirectAttribute]
# fails due to binder issue inside `check_func_def`
Expand All @@ -3067,23 +3039,23 @@ class C:
def test_static_with_attr(x: C) -> None:
def foo(t: A) -> None: ...

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"
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"
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"
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"
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"
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"
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"
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"
reveal_type(l2) # N: Revealed type is "def ()"
reveal_type(r2) # N: Revealed type is "def ()"

def test_generic_with_attr(x: C) -> None:
T = TypeVar("T")
def bar(t: T) -> T: return t

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]")
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]")
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]")
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]")
l2 = (lambda: bar(x.attr.attr)) if x.attr.attr is None else NOOP
r2 = NOOP if x.attr.attr is not None else (lambda: bar(x.attr.attr))
reveal_type(l2) # N: Revealed type is "def () -> Union[__main__.A, None]"
reveal_type(r2) # N: Revealed type is "def () -> Union[__main__.A, None]"
reveal_type(l2) # N: Revealed type is "def () -> __main__.A | None"
reveal_type(r2) # N: Revealed type is "def () -> __main__.A | None"


[case testLiteralTernaryUnionNarrowing]
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-optional.test
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ reveal_type(l) # N: Revealed type is "builtins.list[typing.Generator[builtins.s
[case testNoneListTernary]
# https://github.com/python/mypy/issues/19534
x = [None] if "" else [1]
reveal_type(x) # N: Revealed type is "Union[builtins.list[None], builtins.list[builtins.int]]"
reveal_type(x) # N: Revealed type is "builtins.list[None] | builtins.list[builtins.int]"
[builtins fixtures/list.pyi]

[case testListIncompatibleErrorMessage]
Expand Down