Skip to content

Fixes type vars semanal with ParamSpec #11220

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
FunctionLike, UnboundType, TypeVarType, TupleType, UnionType, StarType,
CallableType, Overloaded, Instance, Type, AnyType, LiteralType, LiteralValue,
TypeTranslator, TypeOfAny, TypeType, NoneType, PlaceholderType, TPDICT_NAMES, ProperType,
get_proper_type, get_proper_types, TypeAliasType
get_proper_type, get_proper_types, TypeAliasType,
)
from mypy.typeops import function_type
from mypy.type_visitor import TypeQuery
Expand Down Expand Up @@ -1326,10 +1326,10 @@ class Foo(Bar, Generic[T]): ...
tvar_defs: List[TypeVarType] = []
for name, tvar_expr in declared_tvars:
tvar_def = self.tvar_scope.bind_new(name, tvar_expr)
assert isinstance(tvar_def, TypeVarType), (
"mypy does not currently support ParamSpec use in generic classes"
)
tvar_defs.append(tvar_def)
if isinstance(tvar_def, TypeVarType):
# This can also be `ParamSpecType`,
# error will be reported elsewhere: #11218
tvar_defs.append(tvar_def)
return base_type_exprs, tvar_defs, is_protocol

def analyze_class_typevar_declaration(
Expand Down
43 changes: 43 additions & 0 deletions test-data/unit/semanal-errors.test
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,49 @@ x = None # type: X
[out]
main:2: error: Name "X" is not defined

[case testInvalidParamSpecType1]
# flags: --python-version 3.10
from typing import ParamSpec

P = ParamSpec("P")

class MyFunction(P):
...

a: MyFunction[int]
[out]
main:6: error: Invalid location for ParamSpec "P"
main:6: note: You can use ParamSpec as the first argument to Callable, e.g., 'Callable[P, int]'
main:9: error: "MyFunction" expects no type arguments, but 1 given

[case testInvalidParamSpecType2]
from typing_extensions import ParamSpec

P = ParamSpec("P")

class MyFunction(P):
...

a: MyFunction[int]
[out]
main:5: error: Invalid location for ParamSpec "P"
main:5: note: You can use ParamSpec as the first argument to Callable, e.g., 'Callable[P, int]'
main:8: error: "MyFunction" expects no type arguments, but 1 given

[case testGenericParamSpec]
# flags: --python-version 3.10
from typing import Generic, TypeVar, Callable, ParamSpec

T = TypeVar("T")
P = ParamSpec("P")

class X(Generic[T, P]):
f: Callable[P, int]
x: T
[out]
main:7: error: Free type variable expected in Generic[...]
main:8: error: The first argument to Callable must be a list of types or "..."

[case testInvalidGenericArg]
from typing import TypeVar, Generic
t = TypeVar('t')
Expand Down