You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
singledispatch keeps a stale handler after ABC.register() when the ABC was registered in a union #162703
When a singledispatch handler is registered for Message | bytes, registering a new virtual subclass of Message does not invalidate a previously cached dispatch decision for that class.
Two controls isolate the problem. Removing the first print(encode(event)) makes the final call return "message". Keeping both calls and replacing the union decorator with stacked @encode.register(Message) and @encode.register(bytes) decorators also gives the expected result.
In Lib/functools.py, register() adds each union member to the registry, then checks the union object itself for __abstractmethods__. That misses the ABC member, so this dispatcher never starts checking abc.get_cache_token(). After Message.register(Event), isinstance() sees the new relationship while encode() continues using its cached fallback.
I do not think this is a problem honestly. Unless the intent was to keep the cache synchronized this seems a normal limitation, same as when one unloads a module while some other references still exist in other modules.
Activity
picnixz commentedon Nov 26, 2026
I do not think this is a problem honestly. Unless the intent was to keep the cache synchronized this seems a normal limitation, same as when one unloads a module while some other references still exist in other modules.
picnixz commentedon Nov 26, 2026
Ah I missed the fact that having a non-union approach works.
Woojay44 commentedon Nov 27, 2026
I've opened #162717 with a fix (checking each union member for
__abstractmethods__) and a regression test.