[issue36457] functools.singledispatchmethod interacts poorly with subclasses

Tim Mitchell report at bugs.python.org
Wed Mar 27 23:42:39 EDT 2019


New submission from Tim Mitchell <tim.mitchell at seequent.com>:

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.
E.g.

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

class AVisitor(BaseVisitor):
   # problem: here we can only register against base class method
   @BaseVistor.visit.reister(int)
   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 :)

----------
components: Library (Lib)
files: methoddispatch36.py
messages: 339008
nosy: Tim Mitchell2, inada.naoki
priority: normal
severity: normal
status: open
title: functools.singledispatchmethod interacts poorly with subclasses
type: behavior
versions: Python 3.8
Added file: https://bugs.python.org/file48235/methoddispatch36.py

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue36457>
_______________________________________


More information about the Python-bugs-list mailing list