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 type repr of empty tuples and namedtuples #15782

Closed
sobolevn opened this issue Jul 30, 2023 · 0 comments · Fixed by #15783
Closed

Incorrect type repr of empty tuples and namedtuples #15782

sobolevn opened this issue Jul 30, 2023 · 0 comments · Fixed by #15783
Assignees
Labels
bug mypy got something wrong

Comments

@sobolevn
Copy link
Member

Right now this code will produce:

from typing import NamedTuple

N = NamedTuple('N', [])
n: N
reveal_type(N)  # N: Revealed type is "def () -> Tuple[, fallback=__main__.N]"
reveal_type(n)  # N: Revealed type is "Tuple[, fallback=__main__.N]"

and:

from typing import Tuple

def f() -> Tuple[()]: ...

reveal_type(f)    # N: Revealed type is "def () -> Tuple[]"
reveal_type(f())  # N: Revealed type is "Tuple[]"

This does not look right, because Tuple[] is not a thing in Python:

>>> from typing import Tuple
>>> Tuple[]
  File "<stdin>", line 1
    Tuple[]
          ^
SyntaxError: invalid syntax

I propose to change it to tuple[()] or Tuple[()] with optional fallback= in case of NamedTuple.

This can affect people who assert output (like pytest-mypy-plugins users), but I think that /s/Tuple[]/Tuple[()] is simple enough to fix all output based checks.

But, we will be more correct in terms of typing.

@sobolevn sobolevn added the bug mypy got something wrong label Jul 30, 2023
@sobolevn sobolevn self-assigned this Jul 30, 2023
sobolevn added a commit that referenced this issue Jul 30, 2023
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

Successfully merging a pull request may close this issue.

1 participant