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: ignore more dunder pos-only errors #12294

Merged
merged 1 commit into from
Mar 5, 2022
Merged
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
10 changes: 6 additions & 4 deletions mypy/stubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,13 +288,14 @@ class SubClass(runtime): # type: ignore
# Examples: ctypes.Array, ctypes._SimpleCData
pass

# Check everything already defined in the stub
# Check everything already defined on the stub class itself (i.e. not inherited)
to_check = set(stub.names)
# Check all public things on the runtime class
to_check.update(
# cast to workaround mypyc complaints
m
for m in cast(Any, vars)(runtime)
if not is_probably_private(m) and m not in ALLOW_MISSING_CLASS_DUNDERS
if not is_probably_private(m) and m not in IGNORABLE_CLASS_DUNDERS
)

for entry in sorted(to_check):
Expand Down Expand Up @@ -629,6 +630,7 @@ def _verify_signature(
if (
runtime_arg.kind != inspect.Parameter.POSITIONAL_ONLY
and stub_arg.variable.name.startswith("__")
and not is_dunder(function_name, exclude_special=True) # noisy for dunder methods
):
yield (
'stub argument "{}" should be positional or keyword '
Expand Down Expand Up @@ -985,16 +987,16 @@ def verify_typealias(
}
)

ALLOW_MISSING_CLASS_DUNDERS = frozenset(
IGNORABLE_CLASS_DUNDERS = frozenset(
{
# Special attributes
"__dict__",
"__text_signature__",
"__weakref__",
"__del__", # Only ever called when an object is being deleted, who cares?
# These two are basically useless for type checkers
"__hash__",
"__getattr__", # resulting behaviour might be typed explicitly
"__setattr__", # defining this on a class can cause worse type checking
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this one seems to be rare enough that I think I'd prefer to consider each case separately and allowlist them in typeshed. I can't think of an example where including it in the stub would improve type checking, but you never know?

# isinstance/issubclass hooks that type-checkers don't usually care about
"__instancecheck__",
"__subclasshook__",
Expand Down