Skip to content

Idea: Sticky/Viral ObjectProxy #244

@mentalisttraceur

Description

@mentalisttraceur

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 result

Subclasses 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

  1. 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_stick for such a thing would probably be just to_object is not None to keep it from breaking is None checks) and

  2. behaviors (for example, ergonomics improvements like adding an operator overload for function composition) (in this example, the can_stick check would be callable(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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions