Push the delegate's 'self' through to Pass-through calls on the context

Bug #373715 reported by Michael Nelson
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
lazr.delegates
Invalid
Undecided
Unassigned

Bug Description

I wonder if it's been discussed whether we could call pass-through methods using the delegate's self, rather than the context? ie:

= Steps to reproduce =
{{{
>>> from zope.interface import Interface
>>> class IMyClass(Interface):
... def superExpensiveQuery():
... """Does lots of stuff"""
... def otherQueryThatUsesExpensiveQuery():
... """Calls superExpensiveQuery"""
...
>>> class MyClass(object):
... implements(IMyClass)
... def superExpensiveQuery(self):
... print "Lots of heavy queries"
... def otherQueryThatUsesExpensiveQuery(self):
... self.superExpensiveQuery()
...
>>> myclassobj = MyClass()
>>> myclassobj.otherQueryThatUsesExpensiveQuery()
Lots of heavy queries

>>> class MyDelegateClass(object):
... delegates(IMyClass)
... def __init__(self, context):
... self.context = context
... def superExpensiveQuery(self):
... print "Nice quick cached version"
...
>>> mydelobj = MyDelegateClass(myclassobj)
>>> mydelobj.superExpensiveQuery()
Nice quick cached version
}}}

= Expected Result =
When calling mydelobj.otherQueryThatUsesExpensiveQuery(), I'd hope to see "Nice quick cached version"

= Actual Result =
{{{
>>> mydelobj.otherQueryThatUsesExpensiveQuery()
Lots of heavy queries
}}}

To do this at the moment the code looks very hacky, ie. in the definition of MyDelegateClass, adding:

{{{
    def otherQueryThatUsesExpensiveQuery(self):
        return MyClass.__dict__['otherQueryThatUsesExpensiveQuery'](self)
}}}

Tags: tech-debt
Revision history for this message
Francis J. Lacoste (flacoste) wrote :

This isn't something we should do as it really breaks the type system.

A delegate isn't a subclass of the delegated class, so passing itself as self is likely to break code (if any method depends on private/protected attributes).

Changed in lazr.delegates:
status: New → Invalid
Curtis Hovey (sinzui)
tags: added: tech-debt
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.