Skip to content
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
17 changes: 10 additions & 7 deletions mypy/semanal_typeargs.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,18 @@ def validate_args(
is_error = False
is_invalid = False
for (i, arg), tvar in zip(enumerate(args), type_vars):
context = ctx if arg.line < 0 else arg
if isinstance(tvar, TypeVarType):
if isinstance(arg, ParamSpecType):
is_invalid = True
self.fail(
INVALID_PARAM_SPEC_LOCATION.format(format_type(arg, self.options)),
ctx,
context,
code=codes.VALID_TYPE,
)
self.note(
INVALID_PARAM_SPEC_LOCATION_NOTE.format(arg.name),
ctx,
context,
code=codes.VALID_TYPE,
)
continue
Expand All @@ -167,7 +168,7 @@ def validate_args(
self.fail(
f"Cannot use {format_type(arg, self.options)} for regular type variable,"
" only for ParamSpec",
ctx,
context,
code=codes.VALID_TYPE,
)
continue
Expand All @@ -182,13 +183,15 @@ def validate_args(
is_error = True
self.fail(
message_registry.INVALID_TYPEVAR_AS_TYPEARG.format(arg.name, name),
ctx,
context,
code=codes.TYPE_VAR,
)
continue
else:
arg_values = [arg]
if self.check_type_var_values(name, arg_values, tvar.name, tvar.values, ctx):
if self.check_type_var_values(
name, arg_values, tvar.name, tvar.values, context
):
is_error = True
# Check against upper bound. Since it's object the vast majority of the time,
# add fast path to avoid a potentially slow subtype check.
Expand All @@ -209,7 +212,7 @@ def validate_args(
name,
format_type(upper_bound, self.options),
),
ctx,
context,
code=codes.TYPE_VAR,
)
elif isinstance(tvar, ParamSpecType):
Expand All @@ -220,7 +223,7 @@ def validate_args(
self.fail(
"Can only replace ParamSpec with a parameter types list or"
f" another ParamSpec, got {format_type(arg, self.options)}",
ctx,
context,
code=codes.VALID_TYPE,
)
if is_invalid:
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -6112,8 +6112,8 @@ A = G
x: A[B[int]] # E
B = G
[out]
main:8:4: error: Type argument "G[int]" of "G" must be a subtype of "str"
main:8:6: error: Type argument "int" of "G" must be a subtype of "str"
main:8:6: error: Type argument "G[int]" of "G" must be a subtype of "str"
main:8:8: error: Type argument "int" of "G" must be a subtype of "str"

[case testExtremeForwardReferencing]
from typing import TypeVar, Generic
Expand Down
11 changes: 10 additions & 1 deletion test-data/unit/check-columns.test
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,18 @@ T = TypeVar('T', int, str)
class C(Generic[T]):
pass

def f(c: C[object]) -> None: pass # E:10: Value of type variable "T" of "C" cannot be "object"
def f(c: C[object]) -> None: pass # E:12: Value of type variable "T" of "C" cannot be "object"
(C[object]()) # E:2: Value of type variable "T" of "C" cannot be "object"

[case testColumnInvalidLocationForParamSpec]
from typing import List
from typing_extensions import ParamSpec

P = ParamSpec('P')
def foo(x: List[P]): pass # E:17: Invalid location for ParamSpec "P" \
# N:17: You can use ParamSpec as the first argument to Callable, e.g., "Callable[P, int]"
[builtins fixtures/list.pyi]

[case testColumnSyntaxErrorInTypeAnnotation]
if int():
def f(x # type: int,
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-generics.test
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ reveal_type(a) # N: Revealed type is "other.array[Any, other.dtype[builtins.floa
[out]
main:3: error: Type argument "float" of "Array" must be a subtype of "generic" [type-var]
a: other.Array[float]
^
^
[file other.py]
from typing import Any, Generic, TypeVar

Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-newsemanal.test
Original file line number Diff line number Diff line change
Expand Up @@ -1666,8 +1666,8 @@ T = TypeVar('T', bound=int)
class C(Generic[T]): pass
class C2(Generic[T]): pass

A = C[str] # E: Type argument "str" of "C" must be a subtype of "int" \
# E: Value of type variable "T" of "C" cannot be "str"
A = C[str] # E: Value of type variable "T" of "C" cannot be "str" \
# E: Type argument "str" of "C" must be a subtype of "int"
B = Union[C[str], int] # E: Type argument "str" of "C" must be a subtype of "int"
S = TypeVar('S', bound=C[str]) # E: Type argument "str" of "C" must be a subtype of "int"
U = TypeVar('U', C[str], str) # E: Type argument "str" of "C" must be a subtype of "int"
Expand Down
Loading