Skip to content

Commit d71afbf

Browse files
authored
Change tuple[] repr to tuple[()] (#15783)
Closes #15782
1 parent 002502a commit d71afbf

13 files changed

+47
-27
lines changed

mypy/messages.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2507,10 +2507,11 @@ def format_literal_value(typ: LiteralType) -> str:
25072507
# Prefer the name of the fallback class (if not tuple), as it's more informative.
25082508
if typ.partial_fallback.type.fullname != "builtins.tuple":
25092509
return format(typ.partial_fallback)
2510+
type_items = format_list(typ.items) or "()"
25102511
if options.use_lowercase_names():
2511-
s = f"tuple[{format_list(typ.items)}]"
2512+
s = f"tuple[{type_items}]"
25122513
else:
2513-
s = f"Tuple[{format_list(typ.items)}]"
2514+
s = f"Tuple[{type_items}]"
25142515
return s
25152516
elif isinstance(typ, TypedDictType):
25162517
# If the TypedDictType is named, return the name

mypy/test/testtypes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,10 @@ def test_callable_type_with_var_args(self) -> None:
129129
)
130130
assert_equal(str(c3), "def (X? =, *Y?) -> Any")
131131

132-
def test_tuple_type(self) -> None:
132+
def test_tuple_type_upper(self) -> None:
133133
options = Options()
134134
options.force_uppercase_builtins = True
135-
assert_equal(TupleType([], self.fx.std_tuple).str_with_options(options), "Tuple[]")
135+
assert_equal(TupleType([], self.fx.std_tuple).str_with_options(options), "Tuple[()]")
136136
assert_equal(TupleType([self.x], self.fx.std_tuple).str_with_options(options), "Tuple[X?]")
137137
assert_equal(
138138
TupleType(

mypy/types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3197,7 +3197,7 @@ def visit_overloaded(self, t: Overloaded) -> str:
31973197
return f"Overload({', '.join(a)})"
31983198

31993199
def visit_tuple_type(self, t: TupleType) -> str:
3200-
s = self.list_str(t.items)
3200+
s = self.list_str(t.items) or "()"
32013201
tuple_name = "tuple" if self.options.use_lowercase_names() else "Tuple"
32023202
if t.partial_fallback and t.partial_fallback.type:
32033203
fallback_name = t.partial_fallback.type.fullname

test-data/unit/check-async-await.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ async def gen() -> AsyncGenerator[int, str]:
475475

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

481481
[builtins fixtures/dict.pyi]

test-data/unit/check-dataclasses.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1896,7 +1896,7 @@ reveal_type(t.__match_args__) # N: Revealed type is "Tuple[Literal['bar']]"
18961896
class Empty:
18971897
...
18981898
e: Empty
1899-
reveal_type(e.__match_args__) # N: Revealed type is "Tuple[]"
1899+
reveal_type(e.__match_args__) # N: Revealed type is "Tuple[()]"
19001900
[builtins fixtures/dataclasses.pyi]
19011901

19021902
[case testDataclassWithoutMatchArgs]

test-data/unit/check-namedtuple.test

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,16 @@ reveal_type(A().b) # N: Revealed type is "typing.NamedTuple"
931931
[builtins fixtures/tuple.pyi]
932932
[typing fixtures/typing-namedtuple.pyi]
933933

934+
935+
[case testEmptyNamedTupleTypeRepr]
936+
from typing import NamedTuple
937+
938+
N = NamedTuple('N', [])
939+
n: N
940+
reveal_type(N) # N: Revealed type is "def () -> Tuple[(), fallback=__main__.N]"
941+
reveal_type(n) # N: Revealed type is "Tuple[(), fallback=__main__.N]"
942+
[builtins fixtures/tuple.pyi]
943+
934944
[case testNamedTupleWrongfile]
935945
from typing import NamedTuple
936946
from b import Type1
@@ -1036,7 +1046,7 @@ def good6() -> NamedTuple:
10361046
def bad1() -> NamedTuple:
10371047
return 1 # E: Incompatible return value type (got "int", expected "NamedTuple")
10381048
def bad2() -> NamedTuple:
1039-
return () # E: Incompatible return value type (got "Tuple[]", expected "NamedTuple")
1049+
return () # E: Incompatible return value type (got "Tuple[()]", expected "NamedTuple")
10401050
def bad3() -> NamedTuple:
10411051
return (1, 2) # E: Incompatible return value type (got "Tuple[int, int]", expected "NamedTuple")
10421052

test-data/unit/check-overloading.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1145,7 +1145,7 @@ def f(x: str) -> None: pass
11451145
f(1.1)
11461146
f('')
11471147
f(1)
1148-
f(()) # E: No overload variant of "f" matches argument type "Tuple[]" \
1148+
f(()) # E: No overload variant of "f" matches argument type "Tuple[()]" \
11491149
# N: Possible overload variants: \
11501150
# N: def f(x: float) -> None \
11511151
# N: def f(x: str) -> None

test-data/unit/check-python310.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,8 +1568,8 @@ class AnnAssign(stmt):
15681568
value: str
15691569
simple: int
15701570

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

15751575
AnnAssign.__match_args__ = ('a', 'b', 'c', 'd') # E: Cannot assign to "__match_args__"

test-data/unit/check-tuples.test

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ t3 = None # type: Tuple[A, B]
143143
a, b, c = None, None, None # type: (A, B, C)
144144

145145
if int():
146-
t2 = () # E: Incompatible types in assignment (expression has type "Tuple[]", variable has type "Tuple[A]")
146+
t2 = () # E: Incompatible types in assignment (expression has type "Tuple[()]", variable has type "Tuple[A]")
147147
if int():
148148
t2 = (a, a) # E: Incompatible types in assignment (expression has type "Tuple[A, A]", variable has type "Tuple[A]")
149149
if int():
@@ -1244,9 +1244,9 @@ f(0) # E: Argument 1 to "f" has incompatible type "int"; expected "Tuple[Any, .
12441244
from typing import Tuple
12451245
def f(a: Tuple[()]) -> None: pass
12461246
f(())
1247-
f((1,)) # E: Argument 1 to "f" has incompatible type "Tuple[int]"; expected "Tuple[]"
1248-
f(('', '')) # E: Argument 1 to "f" has incompatible type "Tuple[str, str]"; expected "Tuple[]"
1249-
f(0) # E: Argument 1 to "f" has incompatible type "int"; expected "Tuple[]"
1247+
f((1,)) # E: Argument 1 to "f" has incompatible type "Tuple[int]"; expected "Tuple[()]"
1248+
f(('', '')) # E: Argument 1 to "f" has incompatible type "Tuple[str, str]"; expected "Tuple[()]"
1249+
f(0) # E: Argument 1 to "f" has incompatible type "int"; expected "Tuple[()]"
12501250
[builtins fixtures/tuple.pyi]
12511251

12521252
[case testNonliteralTupleIndex]
@@ -1467,20 +1467,29 @@ from typing import Tuple
14671467
t = ('',) * 2
14681468
reveal_type(t) # N: Revealed type is "Tuple[builtins.str, builtins.str]"
14691469
t2 = ('',) * -1
1470-
reveal_type(t2) # N: Revealed type is "Tuple[]"
1470+
reveal_type(t2) # N: Revealed type is "Tuple[()]"
14711471
t3 = ('', 1) * 2
14721472
reveal_type(t3) # N: Revealed type is "Tuple[builtins.str, builtins.int, builtins.str, builtins.int]"
14731473
def f() -> Tuple[str, ...]:
14741474
return ('', )
14751475
reveal_type(f() * 2) # N: Revealed type is "builtins.tuple[builtins.str, ...]"
14761476
[builtins fixtures/tuple.pyi]
14771477

1478+
[case testEmptyTupleTypeRepr]
1479+
from typing import Tuple
1480+
1481+
def f() -> Tuple[()]: ...
1482+
1483+
reveal_type(f) # N: Revealed type is "def () -> Tuple[()]"
1484+
reveal_type(f()) # N: Revealed type is "Tuple[()]"
1485+
[builtins fixtures/tuple.pyi]
1486+
14781487
[case testMultiplyTupleByIntegerLiteralReverse]
14791488
from typing import Tuple
14801489
t = 2 * ('',)
14811490
reveal_type(t) # N: Revealed type is "Tuple[builtins.str, builtins.str]"
14821491
t2 = -1 * ('',)
1483-
reveal_type(t2) # N: Revealed type is "Tuple[]"
1492+
reveal_type(t2) # N: Revealed type is "Tuple[()]"
14841493
t3 = 2 * ('', 1)
14851494
reveal_type(t3) # N: Revealed type is "Tuple[builtins.str, builtins.int, builtins.str, builtins.int]"
14861495
def f() -> Tuple[str, ...]:

test-data/unit/check-type-aliases.test

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ U = Union[int, str]
1212
def f(x: U) -> None: pass
1313
f(1)
1414
f('')
15-
f(()) # E: Argument 1 to "f" has incompatible type "Tuple[]"; expected "Union[int, str]"
15+
f(()) # E: Argument 1 to "f" has incompatible type "Tuple[()]"; expected "Union[int, str]"
1616
[targets __main__, __main__.f]
1717
[builtins fixtures/tuple.pyi]
1818

@@ -64,7 +64,7 @@ from _m import U
6464
def f(x: U) -> None: pass
6565
f(1)
6666
f('x')
67-
f(()) # E: Argument 1 to "f" has incompatible type "Tuple[]"; expected "Union[int, str]"
67+
f(()) # E: Argument 1 to "f" has incompatible type "Tuple[()]"; expected "Union[int, str]"
6868
[file _m.py]
6969
from typing import Union
7070
U = Union[int, str]
@@ -170,11 +170,11 @@ f(1) # E: Argument 1 to "f" has incompatible type "int"; expected "str"
170170
from typing import Tuple, Callable
171171
EmptyTuple = Tuple[()]
172172
x: EmptyTuple
173-
reveal_type(x) # N: Revealed type is "Tuple[]"
173+
reveal_type(x) # N: Revealed type is "Tuple[()]"
174174

175175
EmptyTupleCallable = Callable[[Tuple[()]], None]
176176
f: EmptyTupleCallable
177-
reveal_type(f) # N: Revealed type is "def (Tuple[])"
177+
reveal_type(f) # N: Revealed type is "def (Tuple[()])"
178178
[builtins fixtures/list.pyi]
179179

180180
[case testForwardTypeAlias]

test-data/unit/check-typevar-tuple.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ f_args3: Tuple[int, str, bool]
5858
reveal_type(f(f_args)) # N: Revealed type is "Tuple[builtins.str, builtins.str]"
5959
reveal_type(f(f_args2)) # N: Revealed type is "Tuple[builtins.str]"
6060
reveal_type(f(f_args3)) # N: Revealed type is "Tuple[builtins.str, builtins.str, builtins.bool]"
61-
f(empty) # E: Argument 1 to "f" has incompatible type "Tuple[]"; expected "Tuple[int]"
61+
f(empty) # E: Argument 1 to "f" has incompatible type "Tuple[()]"; expected "Tuple[int]"
6262
f(bad_args) # E: Argument 1 to "f" has incompatible type "Tuple[str, str]"; expected "Tuple[int, str]"
6363
# TODO: This hits a crash where we assert len(templates.items) == 1. See visit_tuple_type
6464
# in mypy/constraints.py.

test-data/unit/fine-grained.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8026,7 +8026,7 @@ A = NamedTuple('A', F) # type: ignore
80268026
[builtins fixtures/list.pyi]
80278027
[out]
80288028
==
8029-
b.py:3: note: Revealed type is "Tuple[, fallback=a.A]"
8029+
b.py:3: note: Revealed type is "Tuple[(), fallback=a.A]"
80308030

80318031
[case testImportOnTopOfAlias1]
80328032
from a import A

test-data/unit/typexport-basic.test

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,8 @@ import typing
294294
x = ()
295295
[builtins fixtures/primitives.pyi]
296296
[out]
297-
NameExpr(2) : Tuple[]
298-
TupleExpr(2) : Tuple[]
297+
NameExpr(2) : Tuple[()]
298+
TupleExpr(2) : Tuple[()]
299299

300300
[case testInferTwoTypes]
301301
## NameExpr
@@ -313,8 +313,8 @@ def f() -> None:
313313
x = ()
314314
[builtins fixtures/primitives.pyi]
315315
[out]
316-
NameExpr(3) : Tuple[]
317-
TupleExpr(3) : Tuple[]
316+
NameExpr(3) : Tuple[()]
317+
TupleExpr(3) : Tuple[()]
318318

319319

320320
-- Basic generics

0 commit comments

Comments
 (0)