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 28, 2024
1 parent 09490c8 commit 2b21ed3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 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
9 changes: 6 additions & 3 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

T1 = TypeVar("T1")
Expand All @@ -140,7 +141,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 All @@ -153,6 +154,7 @@ def func_a2(
reveal_type(e) # N: Revealed type is "__main__.ClassA2[Any, builtins.int, builtins.str]"

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

P1 = ParamSpec("P1")
Expand All @@ -175,7 +177,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 @@ -186,6 +188,7 @@ def func_b2(
reveal_type(d) # N: Revealed type is "__main__.ClassB2[Any, [builtins.int, builtins.str]]"

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

Expand Down Expand Up @@ -231,7 +234,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 2b21ed3

Please sign in to comment.