Skip to content

Commit e7a19c8

Browse files
authored
Make X[()] count as a type application (#20568)
Fixes #20563
1 parent 952f159 commit e7a19c8

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

mypy/typeanal.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2533,7 +2533,9 @@ def validate_instance(t: Instance, fail: MsgCallback, empty_tuple_index: bool) -
25332533
arg_count = len(t.args)
25342534
min_tv_count = sum(not tv.has_default() for tv in t.type.defn.type_vars)
25352535
max_tv_count = len(t.type.type_vars)
2536-
if arg_count and (arg_count < min_tv_count or arg_count > max_tv_count):
2536+
if (arg_count or empty_tuple_index) and (
2537+
arg_count < min_tv_count or arg_count > max_tv_count
2538+
):
25372539
fail(
25382540
wrong_type_arg_count(min_tv_count, max_tv_count, str(arg_count), t.type.name),
25392541
t,

test-data/unit/check-generics.test

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3622,3 +3622,33 @@ t2.foo = [B()]
36223622
t2.foo = [C()]
36233623
t2.foo = [1] # E: Value of type variable "T" of "foo" of "Test" cannot be "int"
36243624
[builtins fixtures/property.pyi]
3625+
3626+
[case testMissingTypeArgsInApplication]
3627+
from typing import Generic
3628+
from typing_extensions import Unpack, TypeVarTuple
3629+
3630+
Ts = TypeVarTuple("Ts")
3631+
3632+
class CanHaveZero(Generic[Unpack[Ts]]): ...
3633+
3634+
ok1: CanHaveZero[()]
3635+
ok2: tuple[()]
3636+
3637+
bad1: list[()] = [] # E: "list" expects 1 type argument, but none given
3638+
[builtins fixtures/tuple.pyi]
3639+
3640+
[case testMissingTypeArgsInApplicationStrict]
3641+
# flags: --strict
3642+
from typing import Generic
3643+
from typing_extensions import Unpack, TypeVarTuple
3644+
3645+
Ts = TypeVarTuple("Ts")
3646+
3647+
class CanHaveZero(Generic[Unpack[Ts]]): ...
3648+
3649+
ok1: CanHaveZero[()]
3650+
ok2: tuple[()]
3651+
3652+
bad1: list[()] = [] # E: "list" expects 1 type argument, but none given \
3653+
# E: Missing type arguments for generic type "list"
3654+
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)