Closed dnicolodi closed 2 years ago
I don't remember when or why this code was implemented. It seems that the intent can only be handled by a metaclass.
As far as I know, NodeWalker
classes are not created in large number. I think the most common case is to have one NodeWalker
sublclass instance per parser instance. If more than one NodeWalker
subclasse instances are created per parser, they are almost certainly different subclasses. Thus I don't see how sharing a cache between different instances of NodeWalker
is useful.
Should we make this __new__()
into an __init__()
?
Do you have some project that you can use to benchmark if this causes a performance regression?
Thinking a bit more about this, it may be that this implementation was an attempt to add instance initialization without requiting subclasses to have to call super().__init__()
in their initialization. However, this results in somehow bizarre behavior, thus I think changing the implementation is worth the minuscule compatibility breakage. The breakage manifests with an obvious error and the fix is straightforward.
Hunting for old-style
super()
calls I bumped into the__new__()
method of theNodeWalker
class:As the class does not inherit from other classes the use of
super()
seems redundant here as it is equivalent to simply callingcls()
but this is not what is unusual. What is unusual is modifying the class definition in the__new__()
method. This means that allNodeWalker
instances share the same_walker_cache
dictionary and that this dictionary in reset every time a newNodeWalker
instance is created. I cannot find a reason why this could be desirable.Shouldn't this
__new__()
method be turned into a__init__()
method?