Title: functools.singledispatchmethod interacts poorly with subclasses
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.8
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Tim Mitchell2, bojan.jovanovic.gtech, dschaumont, lukasz.langa, methane
Priority: normal Keywords:

Created on 2019-03-28 03:42 by Tim Mitchell2, last changed 2020-10-27 17:27 by bojan.jovanovic.gtech.

File name Uploaded Description Edit Tim Mitchell2, 2019-03-28 03:42 alternate implementation from methoddispatch package Tim Mitchell2, 2019-04-12 04:43 improved implementation
Messages (4)
msg339008 - (view) Author: Tim Mitchell (Tim Mitchell2) * Date: 2019-03-28 03:42
The new functools.singledispatchmethod (issue32380) class interacts poorly with subclasses.
There is no way for a sub-class to override or extend the dispatch registry.

class BaseVistor:
   def visit(self, obj):
      raise ValueError('Explict vistor implementation missing)

class AVisitor(BaseVisitor):
   # problem: here we can only register against base class method
   def visit_int(self, obj):
       print ('integer')

The AVistor class has now changed the dispatch registry for BaseVistor class which is bad.

To fix this the dispatch registry needs to be copied for each subclass and an alternate register mechanism provided for subclasses to register against a subclass method.
See attached file and pypi methoddispatch for details :)
msg340005 - (view) Author: Tim Mitchell (Tim Mitchell2) * Date: 2019-04-12 04:43
Attached is an improved implementation that does not use a module level register() function.  
It makes the code in the original post work as expected: The `@BaseVisitor.visit.register()` decorator on the `AVisitor` class does not modify the base class dispatch table.  This works by constructing the dispatch registry in the `__init_subclass__` method instead.
msg370758 - (view) Author: Dries Schaumont (dschaumont) Date: 2020-06-05 10:19
I am interested in the fix, added myself to nosy list.
msg371014 - (view) Author: Ɓukasz Langa (lukasz.langa) * (Python committer) Date: 2020-06-08 16:51
The proposed fix requires using a required base class which I'd like to avoid.
Date User Action Args
2020-10-27 17:27:48bojan.jovanovic.gtechsetnosy: + bojan.jovanovic.gtech
2020-06-08 16:51:53lukasz.langasetmessages: + msg371014
2020-06-05 10:19:07dschaumontsetnosy: + dschaumont
messages: + msg370758
2019-04-12 04:43:16Tim Mitchell2setfiles: +

messages: + msg340005
2019-03-28 04:28:55methanesetnosy: + lukasz.langa
2019-03-28 03:42:39Tim Mitchell2create