Skip to content

Commit cd17c50

Browse files
h-joocopybara-github
authored andcommitted
Avoid crashing in case where a TypeVar is trying to be matched with a ParamSpecMatch. It seems there's currently no machinery to match it, so we just avoid it from crashing, by considering them to match.
PiperOrigin-RevId: 774822686
1 parent 48d1294 commit cd17c50

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

pytype/matcher.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,11 @@ def _check_type_param_consistency(
757757
for old_value in old_concrete_values:
758758
if self._satisfies_common_superclass([new_value, old_value]):
759759
has_error = False
760+
elif isinstance(old_value, function.ParamSpecMatch):
761+
# TODO - Technically we need to check the type parameter against
762+
# ParamSpec to check case the restrictions are different, but it
763+
# seems we don't have the machinery to check that yet in pytype.
764+
has_error = False
760765
elif old_value.cls.is_protocol:
761766
with self._track_partially_matched_protocols():
762767
protocol_subst = datatypes.AliasingDict(subst)

pytype/tests/test_typevar2.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,6 +1217,23 @@ def f(self, x: T) -> T:
12171217
return x
12181218
""")
12191219

1220+
def test_check_type_param_against_param_spec(self):
1221+
self.Check("""
1222+
import multiprocessing.pool
1223+
from typing import Callable, ParamSpec, TypeVar, Any
1224+
_T = TypeVar('_T')
1225+
_Args = ParamSpec('_Args')
1226+
1227+
def decorator() -> Callable[[Callable[_Args, _T]], Callable[_Args, _T]]:
1228+
pass # pytype: disable=bad-return-type
1229+
def foo():
1230+
@decorator()
1231+
def f(i: int):
1232+
return i
1233+
with multiprocessing.pool.ThreadPool(10) as pool:
1234+
a: list[Any] = pool.map(f, list(range(10))) # TODO: Should be list[int]
1235+
""")
1236+
12201237

12211238
if __name__ == "__main__":
12221239
test_base.main()

0 commit comments

Comments
 (0)