[Python-ideas] Please reconsider the Boolean evaluation of midnight

Shai Berger shai at platonix.com
Wed Mar 5 14:10:54 CET 2014


On Wednesday 05 March 2014 14:34:09 Oscar Benjamin wrote:
> 
> I think it's clear that if this were new code being added to the
> stdlib then the consensus would be that having midnight evaluate as
> False is ridiculous.
> 
> The question is surely whether the issue is worth a backwards
> compatibility break not whether the current behaviour is a good idea
> (it clearly isn't).
> 

If this is the case, then we should be asking a variant of Paul's question:

Why would anyone check if a time is equal to midnight, by Boolean evaluation 
rather than comparison to midnight? Or, rather, how many people do this?

We have several reports of the current behavior causing surprising, hard-to-
detect bugs (a co-worker spent a couple of hours on one such bug yesterday; 
frankly, I'm impressed he found it that fast). Unless we have reason to think 
people are using this on purpose, the effect of the change will probably be to 
fix many more bugs than it causes.

I suggest to handle the backwards-compatibility issue with a deprecation 
cycle: Python 3.4 is already feature frozen, so make 3.5 raise a 
PendingDeprecationWarning when datetime.time.__nonzero__ returns False; 3.6 a 
DeprecationWarning; and 3.7 return True. That's what we would do in Django.

On Wednesday 05 March 2014 14:54:27 Paul Moore wrote:
> 
> And FWIW, my opinion is that the problem is not worth a compatibility
> break, because it's so easily solved (and the fixed code is probably
> an improvement in any case).

Ah, but it is only easily solved once it is detected; the behavior causes bugs 
that surface once in a blue moon (in production, and then they cannot be 
reproduced by developers who, usually, do not work at midnight).

So, on a second thought -- an alternative solution is to make any Boolean 
evaluation of time objects warn the user about midnight.
 
Thanks for your attention,

Shai.
	


More information about the Python-ideas mailing list