diff --git a/mypy/stubtest.py b/mypy/stubtest.py index 044412299023..b4447d798cdd 100644 --- a/mypy/stubtest.py +++ b/mypy/stubtest.py @@ -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 @@ -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? diff --git a/mypy/test/teststubtest.py b/mypy/test/teststubtest.py index 72944f44414c..197669714ad3 100644 --- a/mypy/test/teststubtest.py +++ b/mypy/test/teststubtest.py @@ -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