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

Steven D'Aprano steve at pearwood.info
Sat Oct 10 14:15:07 CEST 2009


On Sat, 10 Oct 2009 10:22:11 pm Nick Coghlan wrote:

> For the record, there are three cases (not counting Py3k warnings)
> where we currently issue syntax warnings (but generate valid code
> anyway).
>
> 1. Using "import *" at function level (as it disables local variable
> optimisations):

`import *` inside a function is illegal in Python3. It's a warning in 
Python2.x only because of backwards compatibility. This was the primary 
use-case for the warning module in the first place: to warn when 
functionality is being deprecated and will be removed in the future.

Even though `import *` at the module level is a frequent source of bugs 
and confusion even for experienced coders, and the usual advice is Just 
Don't Do It, Python does not generate a warning for that case.


> 2. Referencing a global variable before declaring it as such:
> 3. Assigning to a global variable before declaring it as such:

These are insignificant variations of the same act: using a global 
before declaring it. They may have two different error messages, but 
the underlying problem is the same.


> All 3 could easily be left to pylint/pychecker style tools, but were
> deemed worthy of being warned about by the compiler itself.

The first could not, because it is functionality being removed.

The second and third are warning about the same thing, and this is a 
genuine case of Python warning about a stylistic issue.




-- 
Steven D'Aprano



More information about the Python-ideas mailing list