Improve stubtest handling of expected dunder methods#21764
Open
AlexWaygood wants to merge 2 commits into
Open
Conversation
AlexWaygood
force-pushed
the
alex/stubtest-expected-dunder-wrappers
branch
from
July 21, 2026 15:07
60a3d7f to
f519d92
Compare
Member
Author
|
This patch produces 17 new hits on typeshed's stdlib on Python 3.10–3.14, with three additional hits on Python 3.15. None are currently allowlisted. Python 3.15 only: These all appear to be true positives that we should probably fix in typeshed. Codex analysis of the new hitsAll 20 are callable >>> str.__rmod__("world", "hello %s")
'hello world'
>>> bytes.__rmod__(b"world", b"hello %s")
b'hello world'
>>> bytearray.__rmod__(bytearray(b"world"), bytearray(b"hello %s"))
bytearray(b'hello world')
>>> set.__rand__({2, 3}, {1, 2})
{2}
>>> set.__ror__({2, 3}, {1, 2})
{1, 2, 3}
>>> set.__rsub__({2, 3}, {1, 2})
{1}
>>> set.__rxor__({2, 3}, {1, 2})
{1, 3}
>>> frozenset.__rand__(frozenset({2, 3}), frozenset({1, 2}))
frozenset({2})
>>> frozenset.__ror__(frozenset({2, 3}), frozenset({1, 2}))
frozenset({1, 2, 3})
>>> frozenset.__rsub__(frozenset({2, 3}), frozenset({1, 2}))
frozenset({1})
>>> frozenset.__rxor__(frozenset({2, 3}), frozenset({1, 2}))
frozenset({1, 3})
>>> from datetime import date, datetime, timedelta
>>> date.__rsub__(date(2025, 1, 1), date(2025, 1, 4))
datetime.timedelta(days=3)
>>> datetime.__rsub__(datetime(2025, 1, 1), datetime(2025, 1, 4))
datetime.timedelta(days=3)
>>> timedelta.__rdivmod__(timedelta(days=2), timedelta(days=7))
(3, datetime.timedelta(days=1))
>>> timedelta.__rfloordiv__(timedelta(days=2), timedelta(days=7))
3
>>> timedelta.__rmod__(timedelta(days=2), timedelta(days=7))
datetime.timedelta(days=1)
>>> timedelta.__rtruediv__(timedelta(days=2), timedelta(days=7))
3.5The three Python 3.15-specific hits are also callable: >>> import sys
>>> import types
>>> def get_frame():
... value = 1
... return sys._getframe()
...
>>> frame = get_frame()
>>> locals_proxy = frame.f_locals
>>> types.FrameLocalsProxyType.__or__(locals_proxy, {"other": 2})
{'value': 1, 'other': 2}
>>> types.FrameLocalsProxyType.__ror__(locals_proxy, {"other": 2})
{'other': 2, 'value': 1}
>>> types.FrameLocalsProxyType.__ior__(locals_proxy, {"other": 2})
{'value': 1, 'other': 2}
>>> frame.f_locals
{'value': 1, 'other': 2} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stubtest currently suppresses diagnostics regarding missing dunder methods whenever the runtime attribute is a
WrapperDescriptorType. This is becauseWrapperDescriptorTypedunders are often not actually callable at runtime, so suppressing these diagnostics avoids many false positives. However, it also hides genuine omissions such ascollections.deque.__rmul__(typeshed#16055).This PR keeps the existing suppression in the general case while reporting missing methods that the stub gives us a reason to expect:
__add__method, a diagnostic regarding a missing__radd__method is probably not a false positive__lt__method, a diagnostic regarding a missing__gt__method is probably not a false positiveMapping, diagnostics regarding missing__or__,__ror__or__ior__are probably not false positivesSequence, diagnostics regarding missing__mul__,__rmul__or__imul__methods are probably not false positives.There may be more heuristics we could add similar to these. These were just the ones I/Codex could think of.