Skip to content

Commit

Permalink
Fix disallow-any errors for Instance types (PEP 696)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p committed Jan 31, 2024
1 parent 55247c4 commit 308e87a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
5 changes: 4 additions & 1 deletion mypy/typeanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1866,7 +1866,11 @@ def fix_instance(
max_tv_count = len(t.type.type_vars)
if arg_count < min_tv_count or arg_count > max_tv_count:
# Don't use existing args if arg_count doesn't match
if arg_count > max_tv_count:
# Already wrong arg count error, don't emit missing type parameters error as well.
disallow_any = False
t.args = ()
arg_count = 0

args: list[Type] = [*(t.args[:max_tv_count])]
any_type: AnyType | None = None
Expand Down Expand Up @@ -2324,7 +2328,6 @@ def validate_instance(t: Instance, fail: MsgCallback, empty_tuple_index: bool) -
t,
code=codes.TYPE_ARG,
)
t.args = ()
t.invalid = True
return False
return True
Expand Down
11 changes: 7 additions & 4 deletions test-data/unit/check-typevar-defaults.test
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ def func_c1(x: Union[int, Callable[[Unpack[Ts1]], None]]) -> Tuple[Unpack[Ts1]]:
[builtins fixtures/tuple.pyi]

[case testTypeVarDefaultsClass1]
# flags: --disallow-any-generics
from typing import Generic, TypeVar, Union, overload

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

def func_a2(
a: ClassA2,
a: ClassA2, # E: Missing type parameters for generic type "ClassA2"
b: ClassA2[float],
c: ClassA2[float, float],
d: ClassA2[float, float, float],
Expand Down Expand Up @@ -180,7 +181,7 @@ class ClassA3(Generic[T1, T2]):
def __init__(self, var: Union[int, None] = None) -> None: ...

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

[case testTypeVarDefaultsClass2]
# flags: --disallow-any-generics
from typing import Generic, ParamSpec

P1 = ParamSpec("P1")
Expand Down Expand Up @@ -231,7 +233,7 @@ def func_b1(
class ClassB2(Generic[P1, P2]): ...

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

[case testTypeVarDefaultsClass3]
# flags: --disallow-any-generics
from typing import Generic, Tuple, TypeVar
from typing_extensions import TypeVarTuple, Unpack

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

def func_c4(
a: ClassC4,
a: ClassC4, # E: Missing type parameters for generic type "ClassC4[()]"
b: ClassC4[int],
c: ClassC4[int, float],
) -> None:
Expand Down

0 comments on commit 308e87a

Please sign in to comment.