Skip to content

Commit 032f3b6

Browse files
Fixes type vars semanal with ParamSpec (#11220)
Closes #11218 Co-authored-by: Shantanu <[email protected]>
1 parent 7f89cf8 commit 032f3b6

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

mypy/semanal.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
FunctionLike, UnboundType, TypeVarType, TupleType, UnionType, StarType,
9292
CallableType, Overloaded, Instance, Type, AnyType, LiteralType, LiteralValue,
9393
TypeTranslator, TypeOfAny, TypeType, NoneType, PlaceholderType, TPDICT_NAMES, ProperType,
94-
get_proper_type, get_proper_types, TypeAliasType
94+
get_proper_type, get_proper_types, TypeAliasType,
9595
)
9696
from mypy.typeops import function_type
9797
from mypy.type_visitor import TypeQuery
@@ -1326,10 +1326,10 @@ class Foo(Bar, Generic[T]): ...
13261326
tvar_defs: List[TypeVarType] = []
13271327
for name, tvar_expr in declared_tvars:
13281328
tvar_def = self.tvar_scope.bind_new(name, tvar_expr)
1329-
assert isinstance(tvar_def, TypeVarType), (
1330-
"mypy does not currently support ParamSpec use in generic classes"
1331-
)
1332-
tvar_defs.append(tvar_def)
1329+
if isinstance(tvar_def, TypeVarType):
1330+
# This can also be `ParamSpecType`,
1331+
# error will be reported elsewhere: #11218
1332+
tvar_defs.append(tvar_def)
13331333
return base_type_exprs, tvar_defs, is_protocol
13341334

13351335
def analyze_class_typevar_declaration(

test-data/unit/semanal-errors.test

+43
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,49 @@ x = None # type: X
4141
[out]
4242
main:2: error: Name "X" is not defined
4343

44+
[case testInvalidParamSpecType1]
45+
# flags: --python-version 3.10
46+
from typing import ParamSpec
47+
48+
P = ParamSpec("P")
49+
50+
class MyFunction(P):
51+
...
52+
53+
a: MyFunction[int]
54+
[out]
55+
main:6: error: Invalid location for ParamSpec "P"
56+
main:6: note: You can use ParamSpec as the first argument to Callable, e.g., 'Callable[P, int]'
57+
main:9: error: "MyFunction" expects no type arguments, but 1 given
58+
59+
[case testInvalidParamSpecType2]
60+
from typing_extensions import ParamSpec
61+
62+
P = ParamSpec("P")
63+
64+
class MyFunction(P):
65+
...
66+
67+
a: MyFunction[int]
68+
[out]
69+
main:5: error: Invalid location for ParamSpec "P"
70+
main:5: note: You can use ParamSpec as the first argument to Callable, e.g., 'Callable[P, int]'
71+
main:8: error: "MyFunction" expects no type arguments, but 1 given
72+
73+
[case testGenericParamSpec]
74+
# flags: --python-version 3.10
75+
from typing import Generic, TypeVar, Callable, ParamSpec
76+
77+
T = TypeVar("T")
78+
P = ParamSpec("P")
79+
80+
class X(Generic[T, P]):
81+
f: Callable[P, int]
82+
x: T
83+
[out]
84+
main:7: error: Free type variable expected in Generic[...]
85+
main:8: error: The first argument to Callable must be a list of types or "..."
86+
4487
[case testInvalidGenericArg]
4588
from typing import TypeVar, Generic
4689
t = TypeVar('t')

0 commit comments

Comments
 (0)