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

Greg Ewing greg.ewing at canterbury.ac.nz
Sun Oct 11 02:26:08 CEST 2009


Steven D'Aprano wrote:

>>>>assert (1, 2), "error"
> 
> <stdin>:1: SyntaxWarning: assertion is always true, perhaps remove 
> parentheses?
> 
> Why single out always-true tuples for a warning, when other always-true 
> items are considered legitimate, even when there is no ambiguity about 
> what the programmer meant?

I think it's a side effect of the way the check is implemented.
It just looks at whether the first expression is a tuple
constructor, and ignores the second one. While theoretically
it's not ideal, it doesn't do any harm in practice, because
it's so unlikely that anyone would intentionally write such
an assert statement. There's no use for it whatsoever.

>sometimes the warning is *bad advice*:
> 
>>>>assert 1, 2, "error"  # remove the () like the compiler said
> 
>   File "<stdin>", line 1
>     assert 1, 2, "error"  # remove the () like the compiler said
>                ^
> SyntaxError: invalid syntax

Hm, yes... well, at least you get an error rather than
incorrect results!

-- 
Greg



More information about the Python-ideas mailing list