Skip to content

Commit 582a4de

Browse files
author
Ivan Levkivskyi
committed
Fix urllib crash; some assert tweak
1 parent c5c1b76 commit 582a4de

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

mypy/applytype.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
AnyType,
1010
CallableType,
1111
Instance,
12-
Parameters,
1312
ParamSpecType,
1413
PartialType,
1514
TupleType,
@@ -109,8 +108,13 @@ def apply_generic_arguments(
109108
if param_spec is not None:
110109
nt = id_to_type.get(param_spec.id)
111110
if nt is not None:
112-
if isinstance(nt, Parameters):
113-
callable = callable.expand_param_spec(nt)
111+
# ParamSpec expansion is special-cased, so we need to always expand callable
112+
# as a whole, not expanding arguments individually.
113+
callable = expand_type(callable, id_to_type)
114+
assert isinstance(callable, CallableType)
115+
return callable.copy_modified(
116+
variables=[tv for tv in tvars if tv.id not in id_to_type]
117+
)
114118

115119
# Apply arguments to argument types.
116120
var_arg = callable.var_arg()

mypy/types.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1577,7 +1577,7 @@ def __init__(
15771577
self.arg_kinds = arg_kinds
15781578
self.arg_names = list(arg_names)
15791579
assert len(arg_types) == len(arg_kinds) == len(arg_names)
1580-
assert not any(isinstance(t, (Parameters, ParamSpecType)) for t in arg_types)
1580+
assert not any(isinstance(t, Parameters) for t in arg_types)
15811581
self.min_args = arg_kinds.count(ARG_POS)
15821582
self.is_ellipsis_args = is_ellipsis_args
15831583
self.variables = variables or []

test-data/unit/check-parameter-specification.test

+13
Original file line numberDiff line numberDiff line change
@@ -1555,3 +1555,16 @@ def test(x: U) -> U: ...
15551555
reveal_type(dec) # N: Revealed type is "def [P, T] (f: def (*P.args, **P.kwargs) -> T`-2) -> def (*P.args, **P.kwargs) -> builtins.list[T`-2]"
15561556
reveal_type(dec(test)) # N: Revealed type is "def [T] (x: T`2) -> builtins.list[T`2]"
15571557
[builtins fixtures/paramspec.pyi]
1558+
1559+
[case testParamSpecNestedApplyNoCrash]
1560+
from typing import Callable, TypeVar
1561+
from typing_extensions import ParamSpec
1562+
1563+
P = ParamSpec("P")
1564+
T = TypeVar("T")
1565+
1566+
def apply(fn: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T: ...
1567+
def test() -> None: ...
1568+
# TODO: avoid this error, although it may be non-trivial.
1569+
apply(apply, test) # E: Argument 2 to "apply" has incompatible type "Callable[[], None]"; expected "Callable[P, T]"
1570+
[builtins fixtures/paramspec.pyi]

0 commit comments

Comments
 (0)