Skip to content

Incorrect type repr of empty tuples and namedtuples #15782

@sobolevn

Description

@sobolevn

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.

Metadata

Metadata

Assignees

Labels

bugmypy got something wrong

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions