Skip to content

Commit

Permalink
stubtest: fix error for Protocol.__init__ and __annotations__ (#13179)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood authored Jul 20, 2022
1 parent 64035bd commit 53465bd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
9 changes: 9 additions & 0 deletions mypy/stubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,14 @@ class SubClass(runtime): # type: ignore
for m in cast(Any, vars)(runtime)
if not is_probably_private(m) and m not in IGNORABLE_CLASS_DUNDERS
)
# Special-case the __init__ method for Protocols
#
# TODO: On Python <3.11, __init__ methods on Protocol classes
# are silently discarded and replaced.
# However, this is not the case on Python 3.11+.
# Ideally, we'd figure out a good way of validating Protocol __init__ methods on 3.11+.
if stub.is_protocol:
to_check.discard("__init__")

for entry in sorted(to_check):
mangled_entry = entry
Expand Down Expand Up @@ -1090,6 +1098,7 @@ def verify_typealias(
{
# Special attributes
"__dict__",
"__annotations__",
"__text_signature__",
"__weakref__",
"__del__", # Only ever called when an object is being deleted, who cares?
Expand Down
5 changes: 3 additions & 2 deletions mypy/test/teststubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1033,16 +1033,17 @@ def test_protocol(self) -> Iterator[Case]:
from typing_extensions import Protocol
class X(Protocol):
bar: int
def foo(self, x: int, y: bytes = ...) -> str: ...
""",
runtime="""
from typing_extensions import Protocol
class X(Protocol):
bar: int
def foo(self, x: int, y: bytes = ...) -> str: ...
""",
# TODO: this should not be an error, #12820
error="X.__init__"
error=None
)

@collect_cases
Expand Down

0 comments on commit 53465bd

Please sign in to comment.