Skip to content

Commit c3cd83a

Browse files
authored
Fix disallow-any errors for Instance types (PEP 696) (#16832)
Similar to TypeAlias types `Missing type parameters for generic type` should not be emitted if too many arguments are given. There is a separate error message for that. Ref: #14851
1 parent 5bf7742 commit c3cd83a

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

Diff for: mypy/messages.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -2515,10 +2515,10 @@ def format_literal_value(typ: LiteralType) -> str:
25152515
else:
25162516
base_str = itype.type.name
25172517
if not itype.args:
2518-
if not itype.type.has_type_var_tuple_type:
2519-
# No type arguments, just return the type name
2520-
return base_str
2521-
return base_str + "[()]"
2518+
if itype.type.has_type_var_tuple_type and len(itype.type.type_vars) == 1:
2519+
return base_str + "[()]"
2520+
# No type arguments, just return the type name
2521+
return base_str
25222522
elif itype.type.fullname == "builtins.tuple":
25232523
item_type_str = format(itype.args[0])
25242524
return f"{'tuple' if options.use_lowercase_names() else 'Tuple'}[{item_type_str}, ...]"

Diff for: mypy/typeanal.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1866,7 +1866,11 @@ def fix_instance(
18661866
max_tv_count = len(t.type.type_vars)
18671867
if arg_count < min_tv_count or arg_count > max_tv_count:
18681868
# Don't use existing args if arg_count doesn't match
1869+
if arg_count > max_tv_count:
1870+
# Already wrong arg count error, don't emit missing type parameters error as well.
1871+
disallow_any = False
18691872
t.args = ()
1873+
arg_count = 0
18701874

18711875
args: list[Type] = [*(t.args[:max_tv_count])]
18721876
any_type: AnyType | None = None
@@ -2324,7 +2328,6 @@ def validate_instance(t: Instance, fail: MsgCallback, empty_tuple_index: bool) -
23242328
t,
23252329
code=codes.TYPE_ARG,
23262330
)
2327-
t.args = ()
23282331
t.invalid = True
23292332
return False
23302333
return True

Diff for: test-data/unit/check-typevar-defaults.test

+7-4
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ def func_c1(x: Union[int, Callable[[Unpack[Ts1]], None]]) -> Tuple[Unpack[Ts1]]:
118118
[builtins fixtures/tuple.pyi]
119119

120120
[case testTypeVarDefaultsClass1]
121+
# flags: --disallow-any-generics
121122
from typing import Generic, TypeVar, Union, overload
122123

123124
T1 = TypeVar("T1")
@@ -149,7 +150,7 @@ def func_a1(
149150
class ClassA2(Generic[T1, T2, T3]): ...
150151

151152
def func_a2(
152-
a: ClassA2,
153+
a: ClassA2, # E: Missing type parameters for generic type "ClassA2"
153154
b: ClassA2[float],
154155
c: ClassA2[float, float],
155156
d: ClassA2[float, float, float],
@@ -180,7 +181,7 @@ class ClassA3(Generic[T1, T2]):
180181
def __init__(self, var: Union[int, None] = None) -> None: ...
181182

182183
def func_a3(
183-
a: ClassA3,
184+
a: ClassA3, # E: Missing type parameters for generic type "ClassA3"
184185
b: ClassA3[float],
185186
c: ClassA3[float, float],
186187
d: ClassA3[float, float, float], # E: "ClassA3" expects between 1 and 2 type arguments, but 3 given
@@ -200,6 +201,7 @@ def func_a3(
200201
reveal_type(n) # N: Revealed type is "Any"
201202

202203
[case testTypeVarDefaultsClass2]
204+
# flags: --disallow-any-generics
203205
from typing import Generic, ParamSpec
204206

205207
P1 = ParamSpec("P1")
@@ -231,7 +233,7 @@ def func_b1(
231233
class ClassB2(Generic[P1, P2]): ...
232234

233235
def func_b2(
234-
a: ClassB2,
236+
a: ClassB2, # E: Missing type parameters for generic type "ClassB2"
235237
b: ClassB2[[float]],
236238
c: ClassB2[[float], [float]],
237239
d: ClassB2[[float], [float], [float]], # E: "ClassB2" expects between 1 and 2 type arguments, but 3 given
@@ -251,6 +253,7 @@ def func_b2(
251253
reveal_type(n) # N: Revealed type is "Any"
252254

253255
[case testTypeVarDefaultsClass3]
256+
# flags: --disallow-any-generics
254257
from typing import Generic, Tuple, TypeVar
255258
from typing_extensions import TypeVarTuple, Unpack
256259

@@ -315,7 +318,7 @@ def func_c3(
315318
class ClassC4(Generic[T1, Unpack[Ts1], T3]): ...
316319

317320
def func_c4(
318-
a: ClassC4,
321+
a: ClassC4, # E: Missing type parameters for generic type "ClassC4"
319322
b: ClassC4[int],
320323
c: ClassC4[int, float],
321324
) -> None:

0 commit comments

Comments
 (0)