Skip to content

Commit c7a8162

Browse files
authored
stubtest: ignore more dunder positional-only errors (#12294)
This isn't actually a reversion. This logic was asymmetrical for reasons lost to time. Although I suspect it was this way because the delta only a few errors on typeshed. What changed was that as a result of #12203 we actually started checking a lot more dunder methods. Previously, we only checked dunders if either: a) it was a special dunder, like `__init__` or `__call__`, or b) the dunder was defined on the actual stub class itself In particular, we started checking every dunder redefined at runtime that the stub just inherited from object. A possible intermediate option would be to not check positional-only arguments in the case where a stub inherits the definition. I'll experiment with that once typeshed CI is green. Co-authored-by: hauntsaninja <>
1 parent b3752a6 commit c7a8162

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

mypy/stubtest.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,13 +288,14 @@ class SubClass(runtime): # type: ignore
288288
# Examples: ctypes.Array, ctypes._SimpleCData
289289
pass
290290

291-
# Check everything already defined in the stub
291+
# Check everything already defined on the stub class itself (i.e. not inherited)
292292
to_check = set(stub.names)
293+
# Check all public things on the runtime class
293294
to_check.update(
294295
# cast to workaround mypyc complaints
295296
m
296297
for m in cast(Any, vars)(runtime)
297-
if not is_probably_private(m) and m not in ALLOW_MISSING_CLASS_DUNDERS
298+
if not is_probably_private(m) and m not in IGNORABLE_CLASS_DUNDERS
298299
)
299300

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

988-
ALLOW_MISSING_CLASS_DUNDERS = frozenset(
990+
IGNORABLE_CLASS_DUNDERS = frozenset(
989991
{
990992
# Special attributes
991993
"__dict__",
992994
"__text_signature__",
993995
"__weakref__",
994996
"__del__", # Only ever called when an object is being deleted, who cares?
995-
# These two are basically useless for type checkers
996997
"__hash__",
997998
"__getattr__", # resulting behaviour might be typed explicitly
999+
"__setattr__", # defining this on a class can cause worse type checking
9981000
# isinstance/issubclass hooks that type-checkers don't usually care about
9991001
"__instancecheck__",
10001002
"__subclasshook__",

0 commit comments

Comments
 (0)