component registry navelgazing
Currently if you ask a registry to singly-adapt an object to an interface, and the object you're trying to adapt implements that interface, here's what happens:
from zope.component.registry import Components c = Components() from zope.interface import Interface, implements class IFoo(Interface): pass ... class Foo(object): ... implements(IFoo) ... foo = Foo() c.queryAdapter(IFoo, foo) <None>
In order to get the object itself back from such an adaptation, you need to use the default= argument.
c.queryAdapter(IFoo, foo, default=foo) <__main__.Foo object at 0x24a3910>
This seems slightly inconsistent with the adaptation worldview imposed by getAdapter/queryAdapter. I think it would be more consistent if "c.queryAdapter(IFoo, foo)" returned foo if foo already implemented IFoo and there was no other more specific adapter registered for the IFoo/foo pair in the registry, no? Let the bikeshedding begin, - C
On 12 June 2011 21:48, Chris McDonough <chrism@plope.com> wrote:
Currently if you ask a registry to singly-adapt an object to an interface, and the object you're trying to adapt implements that interface, here's what happens:
from zope.component.registry import Components c = Components() from zope.interface import Interface, implements class IFoo(Interface): pass ... class Foo(object): ... implements(IFoo) ... foo = Foo() c.queryAdapter(IFoo, foo) <None>
In order to get the object itself back from such an adaptation, you need to use the default= argument.
c.queryAdapter(IFoo, foo, default=foo) <__main__.Foo object at 0x24a3910>
This seems slightly inconsistent with the adaptation worldview imposed by getAdapter/queryAdapter. I think it would be more consistent if "c.queryAdapter(IFoo, foo)" returned foo if foo already implemented IFoo and there was no other more specific adapter registered for the IFoo/foo pair in the registry, no?
+1 Martin
* 2011-06-12 22:49, Chris McDonough wrote:
This seems slightly inconsistent with the adaptation worldview imposed by getAdapter/queryAdapter. I think it would be more consistent if "c.queryAdapter(IFoo, foo)" returned foo if foo already implemented IFoo and there was no other more specific adapter registered for the IFoo/foo pair in the registry, no?
+1, way more consistent than returning None. Regards, Fabio
On 12/06/2011 21:48, Chris McDonough wrote:
Currently if you ask a registry to singly-adapt an object to an interface, and the object you're trying to adapt implements that interface, here's what happens:
from zope.component.registry import Components c = Components() from zope.interface import Interface, implements class IFoo(Interface): pass ... class Foo(object): ... implements(IFoo) ... foo = Foo() c.queryAdapter(IFoo, foo) <None>
Looking back in history: https://mail.zope.org/pipermail/zope-dev/2008-August/032902.html I guess one or other of us has the parameter order wrong, probably me. Much thread ensued: https://mail.zope.org/pipermail/zope-dev/2008-August/thread.html#32902 https://mail.zope.org/pipermail/zope-dev/2008-September/thread.html#33174 Some justification for keeping the status quo from Fred: https://mail.zope.org/pipermail/zope-dev/2008-September/033172.html I stand by my resultant request here: https://mail.zope.org/pipermail/zope-dev/2008-September/033170.html Maybe less people care about ZCA now and so we might actually get sensible changes made. Maybe forking would be easier... cheers, Chris -- Simplistix - Content Management, Batch Processing & Python Consulting - http://www.simplistix.co.uk
On Mon, 2011-06-13 at 10:08 +0100, Chris Withers wrote:
On 12/06/2011 21:48, Chris McDonough wrote:
Currently if you ask a registry to singly-adapt an object to an interface, and the object you're trying to adapt implements that interface, here's what happens:
from zope.component.registry import Components c = Components() from zope.interface import Interface, implements class IFoo(Interface): pass ... class Foo(object): ... implements(IFoo) ... foo = Foo() c.queryAdapter(IFoo, foo) <None>
Looking back in history:
https://mail.zope.org/pipermail/zope-dev/2008-August/032902.html
I guess one or other of us has the parameter order wrong, probably me.
Much thread ensued: https://mail.zope.org/pipermail/zope-dev/2008-August/thread.html#32902 https://mail.zope.org/pipermail/zope-dev/2008-September/thread.html#33174
Some justification for keeping the status quo from Fred:
https://mail.zope.org/pipermail/zope-dev/2008-September/033172.html
I stand by my resultant request here:
https://mail.zope.org/pipermail/zope-dev/2008-September/033170.html
Maybe less people care about ZCA now and so we might actually get sensible changes made.
This argument against makes sense to me: https://mail.zope.org/pipermail/zope-dev/2008-August/032908.html So I've added a method to a subclass of zope.component.registry.Components for my own purposes: def queryAdapterOrSelf(self, object, interface, default=None): provides = providedBy(object) if not interface in provides: return self.queryAdapter(object, interface, default=default) return object - C
Hiya, Am 12.06.2011, 22:48 Uhr, schrieb Chris McDonough <chrism@plope.com>:
from zope.component.registry import Components c = Components() from zope.interface import Interface, implements class IFoo(Interface): pass ... class Foo(object): ... implements(IFoo) ... foo = Foo() c.queryAdapter(IFoo, foo) <None>
In order to get the object itself back from such an adaptation, you need to use the default= argument.
I know that the question has been answered but your question makes me ask another: why would you want to adapt an object with itself? Charlie -- Charlie Clark Managing Director Clark Consulting & Research German Office Helmholtzstr. 20 Düsseldorf D- 40215 Tel: +49-211-600-3657 Mobile: +49-178-782-6226
Stepping in… and +1 for the original question On 06/13/2011 04:26 PM, Charlie Clark wrote:
I know that the question has been answered but your question makes me ask another: why would you want to adapt an object with itself?
The sake of logical consistency should be enough. If you think of it, it's very similar to addition of zero : why would you want to add "nothing" ? Not having to predict where/if/when such special cases may blow in your face is very useful, and has indeed been very productive in the past. -- Georges Racinet, http://www.racinet.fr, http://anybox.fr Zope/CPS & OpenERP expertise, assistance & development GPG: 0x4862FFF7 identi.ca & twitter: gracinet
On 13/06/2011 15:26, Charlie Clark wrote:
In order to get the object itself back from such an adaptation, you need to use the default= argument.
I know that the question has been answered but your question makes me ask another: why would you want to adapt an object with itself?
I have something, I want something that implements IWhatever. Now, it may well be that something implements IWhatever, but it may also not. As the author of the code in question, I don't want to have to care. Other authors can plug in adapters for the cases where something doesn't implement IWhatever ;-) cheers, Chris -- Simplistix - Content Management, Batch Processing & Python Consulting - http://www.simplistix.co.uk
Am 13.06.2011, 17:01 Uhr, schrieb Chris Withers <chris@simplistix.co.uk>:
I have something, I want something that implements IWhatever. Now, it may well be that something implements IWhatever, but it may also not. As the author of the code in question, I don't want to have to care. Other authors can plug in adapters for the cases where something doesn't implement IWhatever
I can appreciate the use case but not the semantics - you are not looking for an adapter but an implementation. I guess this is related to the IWhatever(object) approach which does the magic for you. I guess queryProvider(object, interface) would be a better spelling for that use case, which may be more general than I'm suggesting. Charlie -- Charlie Clark Managing Director Clark Consulting & Research German Office Helmholtzstr. 20 Düsseldorf D- 40215 Tel: +49-211-600-3657 Mobile: +49-178-782-6226
On Sun, Jun 12, 2011 at 4:48 PM, Chris McDonough <chrism@plope.com> wrote:
Currently if you ask a registry to singly-adapt an object to an interface, and the object you're trying to adapt implements that interface, here's what happens:
from zope.component.registry import Components c = Components() from zope.interface import Interface, implements class IFoo(Interface): pass ... class Foo(object): ... implements(IFoo) ... foo = Foo() c.queryAdapter(IFoo, foo) <None>
In order to get the object itself back from such an adaptation, you need to use the default= argument.
c.queryAdapter(IFoo, foo, default=foo) <__main__.Foo object at 0x24a3910>
This seems slightly inconsistent with the adaptation worldview imposed by getAdapter/queryAdapter. I think it would be more consistent if "c.queryAdapter(IFoo, foo)" returned foo if foo already implemented IFoo and there was no other more specific adapter registered for the IFoo/foo pair in the registry, no?
I don't know what getAdapter/queryAdapter you're referring to in "This seems slightly inconsistent with the adaptation worldview imposed by getAdapter/queryAdapter." Do you mean " This seems slightly inconsistent with the adaptation worldview imposed by interface __call__." ? Jim -- Jim Fulton http://www.linkedin.com/in/jimfulton
On Mon, Jun 13, 2011 at 12:38 PM, Jim Fulton <jim@zope.com> wrote:
On Sun, Jun 12, 2011 at 4:48 PM, Chris McDonough <chrism@plope.com> wrote:
Currently if you ask a registry to singly-adapt an object to an interface, and the object you're trying to adapt implements that interface, here's what happens:
from zope.component.registry import Components c = Components() from zope.interface import Interface, implements class IFoo(Interface): pass ... class Foo(object): ... implements(IFoo) ... foo = Foo() c.queryAdapter(IFoo, foo) <None>
In order to get the object itself back from such an adaptation, you need to use the default= argument.
c.queryAdapter(IFoo, foo, default=foo) <__main__.Foo object at 0x24a3910>
This seems slightly inconsistent with the adaptation worldview imposed by getAdapter/queryAdapter. I think it would be more consistent if "c.queryAdapter(IFoo, foo)" returned foo if foo already implemented IFoo and there was no other more specific adapter registered for the IFoo/foo pair in the registry, no?
I don't know what getAdapter/queryAdapter you're referring to in "This seems slightly inconsistent with the adaptation worldview imposed by getAdapter/queryAdapter."
Do you mean " This seems slightly inconsistent with the adaptation worldview imposed by interface __call__." ?
By the later messages, I can see that that is what you meant to say. Jim -- Jim Fulton http://www.linkedin.com/in/jimfulton
participants (7)
-
Charlie Clark -
Chris McDonough -
Chris Withers -
Fabio Tranchitella -
Georges Racinet -
Jim Fulton -
Martin Aspeli