From 9df32fb0299721013df62095591c0ecd72fd9c29 Mon Sep 17 00:00:00 2001 From: Hana Joo Date: Mon, 5 May 2025 06:50:24 -0700 Subject: [PATCH] Do not assert at this location. It is possible that the assertion triggers but pytype is indeed looking at a ParamSpec being passed in. PiperOrigin-RevId: 754917442 --- pytype/matcher.py | 8 ++++++-- pytype/tests/test_paramspec.py | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/pytype/matcher.py b/pytype/matcher.py index 870f4139e..34d1e93a2 100644 --- a/pytype/matcher.py +++ b/pytype/matcher.py @@ -1198,14 +1198,18 @@ def _match_subst_against_subst( type_param_map[t], old_subst.copy(t=b1.AssignToNewVariable(self._node)), ) - else: - # If t isn't a TypeVar here it should be a ParamSpec + elif t in self._paramspecs: assert t in self._paramspecs new_var = self.ctx.program.NewVariable() new_var.PasteBindingWithNewData( b2, self.ctx.convert.get_maybe_abstract_instance(b2.data) ) has_error = False + else: + new_var = self.ctx.program.NewVariable() + # Here' it's possibly a ParamSpec but the callpath to this code + # somehow does not assign to the self._paramspecs list. + has_error = False # If new_subst contains a TypeVar that is mutually exclusive with t, # then we can ignore this error because it is legal for t to not be # present in new_subst. diff --git a/pytype/tests/test_paramspec.py b/pytype/tests/test_paramspec.py index a2f1540d8..5c9ab9a08 100644 --- a/pytype/tests/test_paramspec.py +++ b/pytype/tests/test_paramspec.py @@ -611,6 +611,25 @@ def f(x: int, y) -> Any: ... """, ) + def test_paramspec_in_callable_as_param_not_fail(self): + self.Check(""" + from typing import ParamSpec, Callable + + _P = ParamSpec("_P") + CallbleWithParamSpec = Callable[_P, None] + + class A: + def __init__(self, callable_1: CallbleWithParamSpec, callable_2: CallbleWithParamSpec): + pass + + class B(A): + def __init__(self, callable_1: CallbleWithParamSpec, callable_2: CallbleWithParamSpec): + super().__init__( + callable_1, + callable_2, + ) + """) + class ContextlibTest(test_base.BaseTest): """Test some more complex uses of contextlib."""