Skip to content

Commit a0b3fae

Browse files
committed
Fix a crash in pytype/overriding_checks.py.
Very rarely, the type of a kwargs parameter can be a plain dict rather than a Dict[str, X]. PiperOrigin-RevId: 445439268
1 parent b61e00f commit a0b3fae

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

pytype/overriding_checks.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,11 +273,14 @@ def _check_keyword_only_parameters(method_signature, base_signature,
273273
continue
274274

275275
if method_param_name == method_signature.kwargs_name:
276-
# If kwargs are annotated with type T in the function definition,
277-
# the annotation in the signature will be Dict[str, T].
278-
assert isinstance(method_param_type, abstract.ParameterizedClass)
279-
method_param_type = method_param_type.get_formal_type_parameter(
280-
abstract_utils.V)
276+
if isinstance(method_param_type, abstract.ParameterizedClass):
277+
# If kwargs are annotated with type T in the function definition,
278+
# the annotation in the signature will be Dict[str, T].
279+
method_param_type = method_param_type.get_formal_type_parameter(
280+
abstract_utils.V)
281+
else:
282+
# If the kwargs type is a plain dict, there's nothing to check.
283+
continue
281284

282285
# Parameter type of the overridden method must be a subtype of the
283286
# parameter type of the overriding method.

pytype/tests/test_overriding.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from pytype.tests import test_utils
55

66

7-
class OverrdingTest(test_base.BaseTest):
7+
class OverridingTest(test_base.BaseTest):
88
"""Tests for overridden and overriding methods signature match."""
99

1010
# Positional-or-keyword -> positional-or-keyword, same name or underscore.
@@ -779,6 +779,16 @@ def f(self, a: int, /) -> None: # signature-mismatch
779779
pass
780780
""")
781781

782+
def test_callable_multiple_inheritance(self):
783+
self.Check("""
784+
from typing import Callable
785+
class Foo:
786+
def __call__(self, x: int, *, y: str):
787+
pass
788+
class Bar(Callable, Foo):
789+
pass
790+
""")
791+
782792

783793
if __name__ == "__main__":
784794
test_base.main()

0 commit comments

Comments
 (0)