Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
37 changes: 19 additions & 18 deletions mypy/typeanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1927,27 +1927,28 @@ def anal_type(
self.allow_typed_dict_special_forms = old_allow_typed_dict_special_forms
self.allow_ellipsis = old_allow_ellipsis
self.allow_unpack = old_allow_unpack
if (
not allow_param_spec
and isinstance(analyzed, ParamSpecType)
and analyzed.flavor == ParamSpecFlavor.BARE
):
if analyzed.prefix.arg_types:
if not allow_param_spec:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be simpler if you check isinstance for the 2 types up here. Then you can deduplicate the error codepaths.

if isinstance(analyzed, Parameters):
self.fail("Invalid location for Concatenate", t, code=codes.VALID_TYPE)
self.note("You can use Concatenate as the first argument to Callable", t)
analyzed = AnyType(TypeOfAny.from_error)
else:
self.fail(
INVALID_PARAM_SPEC_LOCATION.format(format_type(analyzed, self.options)),
t,
code=codes.VALID_TYPE,
)
self.note(
INVALID_PARAM_SPEC_LOCATION_NOTE.format(analyzed.name),
t,
code=codes.VALID_TYPE,
)
analyzed = AnyType(TypeOfAny.from_error)
elif isinstance(analyzed, ParamSpecType) and analyzed.flavor == ParamSpecFlavor.BARE:
if analyzed.prefix.arg_types:
self.fail("Invalid location for Concatenate", t, code=codes.VALID_TYPE)
self.note("You can use Concatenate as the first argument to Callable", t)
analyzed = AnyType(TypeOfAny.from_error)
else:
self.fail(
INVALID_PARAM_SPEC_LOCATION.format(format_type(analyzed, self.options)),
t,
code=codes.VALID_TYPE,
)
self.note(
INVALID_PARAM_SPEC_LOCATION_NOTE.format(analyzed.name),
t,
code=codes.VALID_TYPE,
)
analyzed = AnyType(TypeOfAny.from_error)
return analyzed

def anal_var_def(self, var_def: TypeVarLikeType) -> TypeVarLikeType:
Expand Down
1 change: 1 addition & 0 deletions test-data/unit/check-parameter-specification.test
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,7 @@ from typing import Callable
P = ParamSpec("P")

def x(f: Callable[Concatenate[int, Concatenate[int, P]], None]) -> None: ... # E: Nested Concatenates are invalid
def y(f: Callable[Concatenate[Concatenate[...], ...], None]) -> None: ... # E: Invalid location for Concatenate # N: You can use Concatenate as the first argument to Callable
[builtins fixtures/paramspec.pyi]

[case testPropagatedAnyConstraintsAreOK]
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-tuples.test
Original file line number Diff line number Diff line change
Expand Up @@ -1835,6 +1835,6 @@ inst_tuple_aa_subclass: tuple_aa_subclass = tuple_aa_subclass((A(), A()))[:] #
[case testTuplePassedParameters]
from typing_extensions import Concatenate

def c(t: tuple[Concatenate[int, ...]]) -> None: # E: Cannot use "[int, VarArg(Any), KwArg(Any)]" for tuple, only for ParamSpec
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove this old error codepath? I can't imagine a scenario where it's useful...

def c(t: tuple[Concatenate[int, ...]]) -> None: # E: Invalid location for Concatenate # N: You can use Concatenate as the first argument to Callable
reveal_type(t) # N: Revealed type is "tuple[Any]"
[builtins fixtures/tuple.pyi]
Loading