Skip to content

Commit be13a0e

Browse files
committed
Bail out if Parameters are generic themselves
1 parent fbb2c46 commit be13a0e

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

mypy/expandtype.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,9 @@ def _possible_callable_varargs(
304304
of other arguments that can be passed positionally.
305305
"""
306306
required_posargs = required_prefix
307+
if repl.variables:
308+
# We will tear the callable apart, do not leak type variables
309+
return tuple_type
307310
optional_posargs: list[Type] = []
308311
for kind, name, type in zip(repl.arg_kinds, repl.arg_names, repl.arg_types):
309312
if kind == ArgKind.ARG_POS and name is None:
@@ -336,6 +339,9 @@ def _possible_callable_kwargs(cls, repl: Parameters, dict_type: Instance) -> Pro
336339
`**kwargs` (until PEP 728 `extra_items` is supported). TypedDict entries will
337340
be required iff the corresponding argument is kw-only and has no default.
338341
"""
342+
if repl.variables:
343+
# We will tear the callable apart, do not leak type variables
344+
return dict_type
339345
kwargs = {}
340346
required_names = set()
341347
extra_items: Type = UninhabitedType()

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2680,6 +2680,33 @@ reveal_type(SneakyPrefix(f10, 1, '', '').args) # N: Revealed type is "Union[tup
26802680
reveal_type(Sneaky(f11).args) # N: Revealed type is "Union[tuple[()], tuple[builtins.int]]"
26812681
[builtins fixtures/paramspec.pyi]
26822682

2683+
[case testRevealBoundParamSpecGeneric]
2684+
from typing import Callable, Generic, ParamSpec, TypeVar
2685+
from typing_extensions import TypeVarTuple, Unpack
2686+
2687+
T = TypeVar("T")
2688+
P = ParamSpec("P")
2689+
Ts = TypeVarTuple("Ts")
2690+
2691+
class SplitSneaky(Generic[P]):
2692+
def __init__(self, target: Callable[P, None]) -> None:
2693+
...
2694+
2695+
def run(self, *args: P.args, **kwargs: P.kwargs) -> None:
2696+
self.args = args
2697+
self.kwargs = kwargs
2698+
2699+
def f1(x: T) -> None: ...
2700+
def f2(*xs: Unpack[Ts]) -> None: ...
2701+
def f3(fn: Callable[P, None]) -> None: ...
2702+
2703+
reveal_type(SplitSneaky(f1).args) # N: Revealed type is "builtins.tuple[builtins.object, ...]"
2704+
reveal_type(SplitSneaky(f1).kwargs) # N: Revealed type is "builtins.dict[builtins.str, builtins.object]"
2705+
reveal_type(SplitSneaky(f2).args) # N: Revealed type is "builtins.tuple[builtins.object, ...]"
2706+
reveal_type(SplitSneaky(f2).kwargs) # N: Revealed type is "builtins.dict[builtins.str, builtins.object]"
2707+
reveal_type(SplitSneaky(f3).args) # N: Revealed type is "builtins.tuple[builtins.object, ...]"
2708+
reveal_type(SplitSneaky(f3).kwargs) # N: Revealed type is "builtins.dict[builtins.str, builtins.object]"
2709+
[builtins fixtures/paramspec.pyi]
26832710

26842711
[case testRevealBoundParamSpecKwargs]
26852712
from typing import Callable, Generic, ParamSpec

0 commit comments

Comments
 (0)