Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stubtest: fix false-positive error for Protocol __init__s and Protocol __annotations__ #13179

Merged
merged 2 commits into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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