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

Nick Coghlan ncoghlan at gmail.com
Sat Oct 10 13:32:55 CEST 2009

Steven D'Aprano wrote:
>> The point of a warning is very much that the code works and is legal,
>>   but *in most case* is not what people would want. 
> Can you give any examples in the Python standard library where code 
> raises a warning on such a basis?
> I would be shocked if you can give any examples.

See my other post - there are currently 3 in the 2.x branch. Two of them
are pure style warnings strongly encouraging people to put their global
statements at the beginning of the function, while the third is a
warning issued due to the fact that using "import *" inside a function
is a really bad idea (at the very least, it forces the compiler to
disable all of its normal local variable optimisations, as it has no
idea what local variable names are going to exist after that import
statement executes).

I just checked the Py3k branch, and that adds a few more:

- A warning for assert statements with a non-empty tuple as the first
argument (i.e. cases where the assert will never fail)

- Another two extending the global statement warnings to cover nonlocal
statements as well.

All 6 are cases where the code is legal and the interpreter will do the
right thing, but the core developers feel people should be encouraged to
write their program differently.

In this thread, the proposal is that we encourage people to only use the
else clause on loops containing at least one break statement.


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-ideas mailing list