Skip to content

gh-162703: Fix singledispatch cache invalidation for ABCs registered in a union - #162717

Open
Woojay44 wants to merge 1 commit into
python:mainfrom
Woojay44:gh-162703-singledispatch-union-abc
Open

Woojay44 wants to merge 1 commit into
python:mainfrom
Woojay44:gh-162703-singledispatch-union-abc

Conversation

@Woojay44

@Woojay44 Woojay44 commented Nov 27, 2026 •

Copy link
Copy Markdown

When an ABC is registered with singledispatch as part of a union
(e.g. @f.register(A | bytes)), register() checked __abstractmethods__
on the union object itself instead of its members. As a result the ABC
cache token was never set up, and the dispatch cache was not cleared after
A.register(B), so f(B()) kept returning the stale default handler.

register() now checks every member of the union, the same way it already
does for a single class. A regression test is added to test_functools.

@Woojay44
Woojay44 requested a review from rhettinger as a code owner November 27, 2026 04:04
@python-cla-bot

python-cla-bot Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

Copy link
Copy Markdown

Windows verification for gh-162703, with AI assistance from Codex.

Environment: Windows 11 25H2, build 26200.9974, AMD64; CPython 3.13.14 (MSC v.1944, Microsoft Store distribution).

The reported stale-dispatch behavior reproduced in 100/100 independent trials: after warming the fallback and calling ABC.register(), union registration continued returning the fallback. Registering the types separately worked in 100/100 control trials.

A local copy of 3.13.14's functools.py was tested with this addition inside the existing union-registration loop:

             for arg in get_args(cls):
                 registry[arg] = func
+                if cache_token is None and hasattr(arg, '__abstractmethods__'):
+                    cache_token = get_cache_token()

Results:

  • Nine focused tests: original implementation had 7 assertion failures and no errors; patched implementation passed all 9.
  • Coverage includes both ABC positions in a union, typing.Union, inferred annotations, late handler registration, repeated virtual-subclass changes, singledispatchmethod, ambiguity detection, and ordinary registration/replacement controls.
  • All 40 tests in the official v3.13.14 test.test_functools.TestSingleDispatch class passed on both the original and patched implementations, with no skips. The test class was run through unittest; for the patched run, its module's functools reference was rebound to the isolated patched module. Test methods/assertions were unchanged.

This supports the per-union-member cache-token approach in this PR, but does not claim verification of PR head a831a8c on main: the tested implementation is a small 3.13.14 adaptation. The complete CPython test suite was not run, and the installed standard library was not modified.

Credit to the issue reporter and @Woojay44 for the existing report and proposal. Codex prepared the local patch/tests, executed the checks, and drafted this report; this is additional Windows evidence, not a new discovery or a merge-readiness claim.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants