Skip to content

Commit 1b6dc3e

Browse files
thomasleeilevkivskyi
authored andcommitted
Recognize a class as protocol if at least one of the bases is Protocol (#7455)
Before only the last base might take effect in some situations. Fixes #7381
1 parent 211d182 commit 1b6dc3e

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

mypy/semanal.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1227,7 +1227,8 @@ class Foo(Bar, Generic[T]): ...
12271227
if declared_tvars:
12281228
self.fail('Only single Generic[...] or Protocol[...] can be in bases', context)
12291229
removed.append(i)
1230-
tvars, is_protocol = result
1230+
tvars = result[0]
1231+
is_protocol |= result[1]
12311232
declared_tvars.extend(tvars)
12321233
if isinstance(base, UnboundType):
12331234
sym = self.lookup_qualified(base.name, base)

test-data/unit/check-semanal-error.test

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,19 @@ def m() -> None: ... # E: Name 'm' already defined (possibly by an import)
9797
def f() -> None: ... # E: Name 'f' already defined (possibly by an import)
9898

9999
[out]
100+
101+
[case testRuntimeProtoTwoBases]
102+
from typing_extensions import Protocol, runtime_checkable
103+
from typing import TypeVar, Generic
104+
105+
T = TypeVar('T')
106+
107+
@runtime_checkable
108+
class P(Protocol, Generic[T]):
109+
attr: T
110+
111+
class C:
112+
attr: int
113+
114+
x: P[int] = C()
115+
[out]

0 commit comments

Comments
 (0)