[Twisted-Python] __nonzero__ for Deferred

Glyph Lefkowitz glyph at twistedmatrix.com
Thu Mar 19 15:13:48 MDT 2015


> On Mar 18, 2015, at 5:50 PM, exarkun at twistedmatrix.com wrote:
> 
> On 18 Mar, 10:03 pm, glyph at twistedmatrix.com wrote:
>> I'd like to deprecate and remove __nonzero__ on Deferreds. Occasionally I see code where someone (maybe even me) does "if d:" where they should have been doing "@d.addCallback def cb(result): if result: ...".
>> 
>> Anybody else have this issue?  Objections?
> 
> Er.  What?  It's a little early for april fools' day, isn't it?

> Seriously, this is weird.  Am I missing something?

Apparently?

> Can expand on what it would mean to deprecated a special method of a class that doesn't even define it

Add such a method and cause it to emit a warning, then raise an exception.

> and what makes Deferred unique among all user-defined classes in this regard.

As I stated in my original message, there's a class of bug where someone does "if somethingThatReturnsDeferred()".  Particularly, "if checkPassword()" is a dangerous manifestation of this class of bug.  I'd like this to fail noisily instead of appearing to work.

In my opinion, having all user-defined classes implement true truthiness by default is a design error in the language.  It's a usually-harmless one except in a few cases, and this is one of those cases, which is why Deferred is not the same as the average user-defined class.

-glyph





More information about the Twisted-Python mailing list