Skip to content

Commit

Permalink
Change tuple[] repr to tuple[()] (#15783)
Browse files Browse the repository at this point in the history
Closes #15782
  • Loading branch information
sobolevn committed Jul 30, 2023
1 parent 002502a commit d71afbf
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 27 deletions.
5 changes: 3 additions & 2 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -2507,10 +2507,11 @@ def format_literal_value(typ: LiteralType) -> str:
# Prefer the name of the fallback class (if not tuple), as it's more informative.
if typ.partial_fallback.type.fullname != "builtins.tuple":
return format(typ.partial_fallback)
type_items = format_list(typ.items) or "()"
if options.use_lowercase_names():
s = f"tuple[{format_list(typ.items)}]"
s = f"tuple[{type_items}]"
else:
s = f"Tuple[{format_list(typ.items)}]"
s = f"Tuple[{type_items}]"
return s
elif isinstance(typ, TypedDictType):
# If the TypedDictType is named, return the name
Expand Down
4 changes: 2 additions & 2 deletions mypy/test/testtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ def test_callable_type_with_var_args(self) -> None:
)
assert_equal(str(c3), "def (X? =, *Y?) -> Any")

def test_tuple_type(self) -> None:
def test_tuple_type_upper(self) -> None:
options = Options()
options.force_uppercase_builtins = True
assert_equal(TupleType([], self.fx.std_tuple).str_with_options(options), "Tuple[]")
assert_equal(TupleType([], self.fx.std_tuple).str_with_options(options), "Tuple[()]")
assert_equal(TupleType([self.x], self.fx.std_tuple).str_with_options(options), "Tuple[X?]")
assert_equal(
TupleType(
Expand Down
2 changes: 1 addition & 1 deletion mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3197,7 +3197,7 @@ def visit_overloaded(self, t: Overloaded) -> str:
return f"Overload({', '.join(a)})"

def visit_tuple_type(self, t: TupleType) -> str:
s = self.list_str(t.items)
s = self.list_str(t.items) or "()"
tuple_name = "tuple" if self.options.use_lowercase_names() else "Tuple"
if t.partial_fallback and t.partial_fallback.type:
fallback_name = t.partial_fallback.type.fullname
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-async-await.test
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ async def gen() -> AsyncGenerator[int, str]:

async def h() -> None:
g = gen()
await g.asend(()) # E: Argument 1 to "asend" of "AsyncGenerator" has incompatible type "Tuple[]"; expected "str"
await g.asend(()) # E: Argument 1 to "asend" of "AsyncGenerator" has incompatible type "Tuple[()]"; expected "str"
reveal_type(await g.asend('hello')) # N: Revealed type is "builtins.int"

[builtins fixtures/dict.pyi]
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-dataclasses.test
Original file line number Diff line number Diff line change
Expand Up @@ -1896,7 +1896,7 @@ reveal_type(t.__match_args__) # N: Revealed type is "Tuple[Literal['bar']]"
class Empty:
...
e: Empty
reveal_type(e.__match_args__) # N: Revealed type is "Tuple[]"
reveal_type(e.__match_args__) # N: Revealed type is "Tuple[()]"
[builtins fixtures/dataclasses.pyi]

[case testDataclassWithoutMatchArgs]
Expand Down
12 changes: 11 additions & 1 deletion test-data/unit/check-namedtuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,16 @@ reveal_type(A().b) # N: Revealed type is "typing.NamedTuple"
[builtins fixtures/tuple.pyi]
[typing fixtures/typing-namedtuple.pyi]


[case testEmptyNamedTupleTypeRepr]
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]"
[builtins fixtures/tuple.pyi]

[case testNamedTupleWrongfile]
from typing import NamedTuple
from b import Type1
Expand Down Expand Up @@ -1036,7 +1046,7 @@ def good6() -> NamedTuple:
def bad1() -> NamedTuple:
return 1 # E: Incompatible return value type (got "int", expected "NamedTuple")
def bad2() -> NamedTuple:
return () # E: Incompatible return value type (got "Tuple[]", expected "NamedTuple")
return () # E: Incompatible return value type (got "Tuple[()]", expected "NamedTuple")
def bad3() -> NamedTuple:
return (1, 2) # E: Incompatible return value type (got "Tuple[int, int]", expected "NamedTuple")

Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-overloading.test
Original file line number Diff line number Diff line change
Expand Up @@ -1145,7 +1145,7 @@ def f(x: str) -> None: pass
f(1.1)
f('')
f(1)
f(()) # E: No overload variant of "f" matches argument type "Tuple[]" \
f(()) # E: No overload variant of "f" matches argument type "Tuple[()]" \
# N: Possible overload variants: \
# N: def f(x: float) -> None \
# N: def f(x: str) -> None
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-python310.test
Original file line number Diff line number Diff line change
Expand Up @@ -1568,8 +1568,8 @@ class AnnAssign(stmt):
value: str
simple: int

reveal_type(AST.__match_args__) # N: Revealed type is "Tuple[]"
reveal_type(stmt.__match_args__) # N: Revealed type is "Tuple[]"
reveal_type(AST.__match_args__) # N: Revealed type is "Tuple[()]"
reveal_type(stmt.__match_args__) # N: Revealed type is "Tuple[()]"
reveal_type(AnnAssign.__match_args__) # N: Revealed type is "Tuple[Literal['target']?, Literal['annotation']?, Literal['value']?, Literal['simple']?]"

AnnAssign.__match_args__ = ('a', 'b', 'c', 'd') # E: Cannot assign to "__match_args__"
Expand Down
21 changes: 15 additions & 6 deletions test-data/unit/check-tuples.test
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ t3 = None # type: Tuple[A, B]
a, b, c = None, None, None # type: (A, B, C)

if int():
t2 = () # E: Incompatible types in assignment (expression has type "Tuple[]", variable has type "Tuple[A]")
t2 = () # E: Incompatible types in assignment (expression has type "Tuple[()]", variable has type "Tuple[A]")
if int():
t2 = (a, a) # E: Incompatible types in assignment (expression has type "Tuple[A, A]", variable has type "Tuple[A]")
if int():
Expand Down Expand Up @@ -1244,9 +1244,9 @@ f(0) # E: Argument 1 to "f" has incompatible type "int"; expected "Tuple[Any, .
from typing import Tuple
def f(a: Tuple[()]) -> None: pass
f(())
f((1,)) # E: Argument 1 to "f" has incompatible type "Tuple[int]"; expected "Tuple[]"
f(('', '')) # E: Argument 1 to "f" has incompatible type "Tuple[str, str]"; expected "Tuple[]"
f(0) # E: Argument 1 to "f" has incompatible type "int"; expected "Tuple[]"
f((1,)) # E: Argument 1 to "f" has incompatible type "Tuple[int]"; expected "Tuple[()]"
f(('', '')) # E: Argument 1 to "f" has incompatible type "Tuple[str, str]"; expected "Tuple[()]"
f(0) # E: Argument 1 to "f" has incompatible type "int"; expected "Tuple[()]"
[builtins fixtures/tuple.pyi]

[case testNonliteralTupleIndex]
Expand Down Expand Up @@ -1467,20 +1467,29 @@ from typing import Tuple
t = ('',) * 2
reveal_type(t) # N: Revealed type is "Tuple[builtins.str, builtins.str]"
t2 = ('',) * -1
reveal_type(t2) # N: Revealed type is "Tuple[]"
reveal_type(t2) # N: Revealed type is "Tuple[()]"
t3 = ('', 1) * 2
reveal_type(t3) # N: Revealed type is "Tuple[builtins.str, builtins.int, builtins.str, builtins.int]"
def f() -> Tuple[str, ...]:
return ('', )
reveal_type(f() * 2) # N: Revealed type is "builtins.tuple[builtins.str, ...]"
[builtins fixtures/tuple.pyi]

[case testEmptyTupleTypeRepr]
from typing import Tuple

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

reveal_type(f) # N: Revealed type is "def () -> Tuple[()]"
reveal_type(f()) # N: Revealed type is "Tuple[()]"
[builtins fixtures/tuple.pyi]

[case testMultiplyTupleByIntegerLiteralReverse]
from typing import Tuple
t = 2 * ('',)
reveal_type(t) # N: Revealed type is "Tuple[builtins.str, builtins.str]"
t2 = -1 * ('',)
reveal_type(t2) # N: Revealed type is "Tuple[]"
reveal_type(t2) # N: Revealed type is "Tuple[()]"
t3 = 2 * ('', 1)
reveal_type(t3) # N: Revealed type is "Tuple[builtins.str, builtins.int, builtins.str, builtins.int]"
def f() -> Tuple[str, ...]:
Expand Down
8 changes: 4 additions & 4 deletions test-data/unit/check-type-aliases.test
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ U = Union[int, str]
def f(x: U) -> None: pass
f(1)
f('')
f(()) # E: Argument 1 to "f" has incompatible type "Tuple[]"; expected "Union[int, str]"
f(()) # E: Argument 1 to "f" has incompatible type "Tuple[()]"; expected "Union[int, str]"
[targets __main__, __main__.f]
[builtins fixtures/tuple.pyi]

Expand Down Expand Up @@ -64,7 +64,7 @@ from _m import U
def f(x: U) -> None: pass
f(1)
f('x')
f(()) # E: Argument 1 to "f" has incompatible type "Tuple[]"; expected "Union[int, str]"
f(()) # E: Argument 1 to "f" has incompatible type "Tuple[()]"; expected "Union[int, str]"
[file _m.py]
from typing import Union
U = Union[int, str]
Expand Down Expand Up @@ -170,11 +170,11 @@ f(1) # E: Argument 1 to "f" has incompatible type "int"; expected "str"
from typing import Tuple, Callable
EmptyTuple = Tuple[()]
x: EmptyTuple
reveal_type(x) # N: Revealed type is "Tuple[]"
reveal_type(x) # N: Revealed type is "Tuple[()]"

EmptyTupleCallable = Callable[[Tuple[()]], None]
f: EmptyTupleCallable
reveal_type(f) # N: Revealed type is "def (Tuple[])"
reveal_type(f) # N: Revealed type is "def (Tuple[()])"
[builtins fixtures/list.pyi]

[case testForwardTypeAlias]
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-typevar-tuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ f_args3: Tuple[int, str, bool]
reveal_type(f(f_args)) # N: Revealed type is "Tuple[builtins.str, builtins.str]"
reveal_type(f(f_args2)) # N: Revealed type is "Tuple[builtins.str]"
reveal_type(f(f_args3)) # N: Revealed type is "Tuple[builtins.str, builtins.str, builtins.bool]"
f(empty) # E: Argument 1 to "f" has incompatible type "Tuple[]"; expected "Tuple[int]"
f(empty) # E: Argument 1 to "f" has incompatible type "Tuple[()]"; expected "Tuple[int]"
f(bad_args) # E: Argument 1 to "f" has incompatible type "Tuple[str, str]"; expected "Tuple[int, str]"
# TODO: This hits a crash where we assert len(templates.items) == 1. See visit_tuple_type
# in mypy/constraints.py.
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/fine-grained.test
Original file line number Diff line number Diff line change
Expand Up @@ -8026,7 +8026,7 @@ A = NamedTuple('A', F) # type: ignore
[builtins fixtures/list.pyi]
[out]
==
b.py:3: note: Revealed type is "Tuple[, fallback=a.A]"
b.py:3: note: Revealed type is "Tuple[(), fallback=a.A]"

[case testImportOnTopOfAlias1]
from a import A
Expand Down
8 changes: 4 additions & 4 deletions test-data/unit/typexport-basic.test
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,8 @@ import typing
x = ()
[builtins fixtures/primitives.pyi]
[out]
NameExpr(2) : Tuple[]
TupleExpr(2) : Tuple[]
NameExpr(2) : Tuple[()]
TupleExpr(2) : Tuple[()]

[case testInferTwoTypes]
## NameExpr
Expand All @@ -313,8 +313,8 @@ def f() -> None:
x = ()
[builtins fixtures/primitives.pyi]
[out]
NameExpr(3) : Tuple[]
TupleExpr(3) : Tuple[]
NameExpr(3) : Tuple[()]
TupleExpr(3) : Tuple[()]


-- Basic generics
Expand Down

0 comments on commit d71afbf

Please sign in to comment.