Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect TypeIs narrowing for tuples #17599

Open
erictraut opened this issue Jul 26, 2024 · 1 comment
Open

Incorrect TypeIs narrowing for tuples #17599

erictraut opened this issue Jul 26, 2024 · 1 comment
Labels
bug mypy got something wrong

Comments

@erictraut
Copy link

I noticed this behavior when investigating a similar issue in pyright.

Mypy isn't correctly narrowing tuples in some cases.

from typing_extensions import TypeIs


def is_tuple_of_strings(v: tuple[int | str, ...]) -> TypeIs[tuple[str, ...]]:
    return all(isinstance(x, str) for x in v)


def test1(t: tuple[int]) -> None:
    if is_tuple_of_strings(t):
        reveal_type(t)  # Should be Never ✅ 
    else:
        reveal_type(t)  # Should be tuple[int] ✅ 

def test2(t: tuple[str, int]) -> None:
    if is_tuple_of_strings(t):
        reveal_type(t)  # Should be Never ✅ 
    else:
        reveal_type(t)  # Should be tuple[str, int] ✅ 

def test3(t: tuple[int | str]) -> None:
    if is_tuple_of_strings(t):
        reveal_type(t)  # Should be tuple[str] ✅ 
    else:
        reveal_type(t)  # Should be tuple[int] or tuple[int | str] ❌ (mypy: Never)

def test4(t: tuple[int | str, int | str]) -> None:
    if is_tuple_of_strings(t):
        reveal_type(t)  # Should be tuple[str, str] ✅ 
    else:
        reveal_type(t)  # Should be tuple[int | str, int | str] or tuple[int, int | str] | tuple[str, int] ❌ (mypy: Never)

def test5(t: tuple[int | str, ...]) -> None:
    if is_tuple_of_strings(t):
        reveal_type(t)  # Should be tuple[str, ...] ✅ 
    else:
        reveal_type(t)  # Should be tuple[int | str, ...] ❌ (mypy: Never)

def test6(t: tuple[str, *tuple[int | str, ...], str]) -> None:
    if is_tuple_of_strings(t):
        reveal_type(t)  # Should be tuple[str, *tuple[str, ...], str] ❌ (mypy: tuple[str, Never, str])
    else:
        reveal_type(t)  # Should be tuple[str, *tuple[int | str, ...], str] ❌ (mypy: Never)
@kreathon
Copy link
Contributor

I just checked the outcome of the tests with: #17232:

  • 3,4,5 would also be fixed
  • 6 does not change (at least the if-branch; else branch looks correct)

Output:

../xx.py:12: note: Revealed type is "tuple[builtins.int]"
../xx.py:18: note: Revealed type is "tuple[builtins.str, builtins.int]"
../xx.py:22: note: Revealed type is "tuple[builtins.str]"
../xx.py:24: note: Revealed type is "tuple[Union[builtins.int, builtins.str]]"
../xx.py:28: note: Revealed type is "tuple[builtins.str, builtins.str]"
../xx.py:30: note: Revealed type is "tuple[Union[builtins.int, builtins.str], Union[builtins.int, builtins.str]]"
../xx.py:34: note: Revealed type is "builtins.tuple[builtins.str, ...]"
../xx.py:36: note: Revealed type is "builtins.tuple[Union[builtins.int, builtins.str], ...]"
../xx.py:40: note: Revealed type is "tuple[builtins.str, Never, builtins.str]"
../xx.py:42: note: Revealed type is "tuple[builtins.str, Unpack[builtins.tuple[Union[builtins.int, builtins.str], ...]], builtins.str]"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants