Skip to content

Commit 402bd16

Browse files
committed
Fix a minor bug in pytype's expansion of Protocol[T].
Inheriting from Protocol[T] is a shorthand for inheriting from both Protocol and Generic[T]. We were handling this case properly for typing.Protocol but not for typing_extensions.Protocol. Context: python/typeshed#5172 PiperOrigin-RevId: 366414389
1 parent cadff0c commit 402bd16

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

pytype/pyi/classdef.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from typed_ast import ast3
1313

1414

15+
_PROTOCOL_ALIASES = ("typing.Protocol", "typing_extensions.Protocol")
1516
_TYPED_DICT_ALIASES = ("typing.TypedDict", "typing_extensions.TypedDict")
1617

1718

@@ -101,4 +102,4 @@ def check_for_duplicate_defs(methods, constants, aliases) -> None:
101102

102103
def _is_parameterized_protocol(t) -> bool:
103104
return (isinstance(t, pytd.GenericType) and
104-
t.base_type.name == "typing.Protocol")
105+
pytd_utils.MatchesFullName(t.base_type, _PROTOCOL_ALIASES))

pytype/pyi/parser_test.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,6 +1496,26 @@ class Foo(Protocol[T]): ...
14961496
class Foo(Protocol, Generic[T]): ...
14971497
""")
14981498

1499+
def test_typing_extensions_parameterized_protocol(self):
1500+
self.check("""
1501+
from typing import TypeVar
1502+
1503+
from typing_extensions import Protocol
1504+
1505+
T = TypeVar('T')
1506+
1507+
class Foo(Protocol[T]): ...
1508+
""", """
1509+
from typing import Generic, TypeVar
1510+
import typing_extensions
1511+
1512+
from typing_extensions import Protocol
1513+
1514+
T = TypeVar('T')
1515+
1516+
class Foo(typing_extensions.Protocol, Generic[T]): ...
1517+
""")
1518+
14991519
def test_bad_typevar_in_mutation(self):
15001520
self.check_error("""
15011521
from typing import Generic, TypeVar

0 commit comments

Comments
 (0)