-
-
Notifications
You must be signed in to change notification settings - Fork 244
Description
I'm picturing a subclass of ObjectProxy (suggested name: StickyObjectProxy or ViralObjectProxy) which wraps the result of every method called on it with itself:
class StickyObjectProxy(ObjectProxy):
@staticmethod
def can_stick(from_object, to_object):
raise NotImplementedError("sticky proxies must define their stickiness")
...
def __str__(self):
result = str(self.__wrapped__)
if type(self).can_stick(self, result):
return type(self)(result)
return result
...
def __add__(self, other):
result = self.__wrapped__ + other
if type(self).can_stick(self, result):
return type(self)(result)
return resultSubclasses would override can_stick with logic that is appropriate for that wrapper.
[Edit: I have updated this proposal since Graham's first reply.]
(Of course I also suggest adding a corresponding Callable{Sticky,Viral}ObjectProxy.)
This would enable propagating
-
annotations (for example, when debugging/exploring complex code, I sometimes really want to know where a value "came from", which sometimes means "what data paths fed into this value? what was this computed from"? the
can_stickfor such a thing would probably be justto_object is not Noneto keep it from breakingis Nonechecks) and -
behaviors (for example, ergonomics improvements like adding an operator overload for function composition) (in this example, the
can_stickcheck would becallable(to_object).)
through code with minimal boilerplate.
The big reason for having this inside of wrapt is that wrapt is already in the business of knowing when a new dunder method is added to the language, and a StickyObjectProxy would have to override almost every method that ObjectProxy has to override, and if ObjectProxy has to change (for example, adding a new dunder method or a new workaround for newer versions of Python) then StickyObjectProxy probably needs the same exact change, so it would be a really big efficiency gain to consolidate that effort, visibility, and need-for-awareness in one place.