[Python-ideas] SyntaxWarning for for/while/else without break or return?

Steven D'Aprano steve at pearwood.info
Sun Oct 11 13:12:59 CEST 2009


On Sun, 11 Oct 2009 05:36:07 am Stephen J. Turnbull wrote:
> Arnaud Delobelle writes:
>  > Also, it would be odd to have a language feature that generates a
>  > warning whenever it is used :)
>
> Not really.  Deprecation warnings behave that way.  AFAICS, a
> deprecation warning is just the extreme case of a syntax warning.

I don't see it that way at all. A deprecation warning makes sense, it's 
the primary use case for the warnings module. It says "Here is a 
feature which is changing, or being removed -- you MUST take steps to 
change your code, or be stuck with this version forever, because the 
language is changing."

A syntax warning, as being suggested, is nothing like that. It 
says "Here is a legitimate feature, part of the language. It's not 
going away. You may have a good reason for doing what you want, but 
just in case you're ignorant or naive or stupid, I'm going to warn you 
that your perfectly legal code might be doing what something other than 
what you expect."

This is, to my mind, particularly egregious because every single 
language feature might be doing something other than what you expect, 
if you are sufficiently ignorant, naive or stupid. Such warnings not 
only try to guess what users want, but they make an arbitrary, and 
inconsistent, decision as to how much ignorance they "protect" the user 
from.

We don't, for example, protect the user from thinking that list.sort() 
returns the sorted list, or that arithmetic on floats is simple, or 
that locals() can be meaningfully modified, or that mutable defaults 
are recreated every time the function is called -- all errors FAR more 
common than misunderstanding for...else.

If your aim is to protect users from their own mistakes, then there are 
far more serious mistakes that we just ignore. And if our attitude 
is "we're all adults here", then why on earth single out such an 
uncommon gotcha for special treatment?



-- 
Steven D'Aprano



More information about the Python-ideas mailing list