Skip to content

Commit 53465bd

Browse files
authored
stubtest: fix error for Protocol.__init__ and __annotations__ (#13179)
1 parent 64035bd commit 53465bd

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

mypy/stubtest.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,14 @@ class SubClass(runtime): # type: ignore
344344
for m in cast(Any, vars)(runtime)
345345
if not is_probably_private(m) and m not in IGNORABLE_CLASS_DUNDERS
346346
)
347+
# Special-case the __init__ method for Protocols
348+
#
349+
# TODO: On Python <3.11, __init__ methods on Protocol classes
350+
# are silently discarded and replaced.
351+
# However, this is not the case on Python 3.11+.
352+
# Ideally, we'd figure out a good way of validating Protocol __init__ methods on 3.11+.
353+
if stub.is_protocol:
354+
to_check.discard("__init__")
347355

348356
for entry in sorted(to_check):
349357
mangled_entry = entry
@@ -1090,6 +1098,7 @@ def verify_typealias(
10901098
{
10911099
# Special attributes
10921100
"__dict__",
1101+
"__annotations__",
10931102
"__text_signature__",
10941103
"__weakref__",
10951104
"__del__", # Only ever called when an object is being deleted, who cares?

mypy/test/teststubtest.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,16 +1033,17 @@ def test_protocol(self) -> Iterator[Case]:
10331033
from typing_extensions import Protocol
10341034
10351035
class X(Protocol):
1036+
bar: int
10361037
def foo(self, x: int, y: bytes = ...) -> str: ...
10371038
""",
10381039
runtime="""
10391040
from typing_extensions import Protocol
10401041
10411042
class X(Protocol):
1043+
bar: int
10421044
def foo(self, x: int, y: bytes = ...) -> str: ...
10431045
""",
1044-
# TODO: this should not be an error, #12820
1045-
error="X.__init__"
1046+
error=None
10461047
)
10471048

10481049
@collect_cases

0 commit comments

Comments
 (0)