[Twisted-Python] Interfaces and adapters

Andrew Bennetts andrew-twisted at puzzling.org
Fri Aug 26 04:40:07 EDT 2005

On Fri, Aug 26, 2005 at 12:00:39PM +0400, en.karpachov at ospaz.ru wrote:
> On Fri, 26 Aug 2005 16:30:26 +1000
> Andrew Bennetts wrote:
> > > Could somebody give any rationale why this condition shouldn't always hold:
> > > 
> > > foo = IFoo(bar)
> > > assert IFoo.providedBy(foo)
> > > 
> > > Seems it is not unreasonable to expect this behaviour, because it is
> > > exactly what interfaces are all about, or am I wrong?
> > 
> > That's a pretty reasonable expectation.  I think emitting a warning if this
> > doesn't hold would be a sane thing to do, and it wouldn't break any code.
> For me, raising CannotAdapt would be better choice. After all, getting an
> object that implements requested interface is the very thing what
> adaptation is used for.
> If I, say, adapt WovenContext to ISession, I get a session object which
> does not implement the ISession interface itself. Absurdly.

Yep.  But that's a trivially fixable bug in that adapter.  And this is
exactly why raising an exception would be bad -- it will break code that
otherwise works fine.  Even though such code is arguably buggy, Twisted
tries reasonably hard not to break backwards compatibility without warning.
We couldn't raise CannotAdapt here without first giving DeprecationWarnings
for at least one release.


More information about the Twisted-Python mailing list