Created on 2019-03-28 03:42 by Tim Mitchell2, last changed 2022-04-11 14:59 by admin.
Messages (5) |
|
|
msg339008 - (view) |
Author: Tim Mitchell (Tim Mitchell2) * |
Date: 2019-03-28 03:42 |
The new functools.singledispatchmethod () 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 :) |
|
|
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) *  |
Date: 2020-06-08 16:51 |
The proposed fix requires using a required base class which I'd like to avoid. |
|
|
msg415470 - (view) |
Author: Tim Mitchell (Tim Mitchell2) * |
Date: 2022-03-18 06:05 |
I've come up with a version that does not require a base class. Seems a bit hacky as the descriptor __get__ method now modifies the class to put the dispatch table in place the first time the method is accessed. |
|
|
History |
|
|
|
Date |
User |
Action |
Args |
2022-04-11 14:59:13 |
admin |
set |
github: 80638 |
2022-03-18 06:10:24 |
Tim Mitchell2 |
set |
files: + test_sdm.py |
2022-03-18 06:05:56 |
Tim Mitchell2 |
set |
files: + singledispatchmethod.pymessages: + |
2020-10-27 17:27:48 |
bojan.jovanovic.gtech |
set |
nosy: + bojan.jovanovic.gtech |
2020-06-08 16:51:53 |
lukasz.langa |
set |
messages: + |
2020-06-05 10:19:07 |
dschaumont |
set |
nosy: + dschaumontmessages: + |
2019-04-12 04:43:16 |
Tim Mitchell2 |
set |
files: + singledispatchmethod.pymessages: + |
2019-03-28 04:28:55 |
methane |
set |
nosy: + lukasz.langa |
2019-03-28 03:42:39 |
Tim Mitchell2 |
create |
|