pylint #6954 false "Using variable before assignment" on one-liner with-statements [resolved]
Mads Kiilerich from __future__ import with_statement with file('f') as f: print f.read() gives E: 2: Using variable 'f' before assignment When the print statement is moved to its own line pylint correctly doesn't see an error | |
priority | normal |
---|---|
type | bug |
done in | 0.16.0 |
load | 0.500 |
load left | 0.000 |
closed by | <not specified> |
Comments
-
2009/01/15 00:48, written by mkiilerich
-
2009/01/15 07:54, written by sthenault
- the _py25 suffix makes it executed only with python 2.5
- the _noerror_ prefix tells that no message should be issued when this file is analysed (else a file containing messages is expected to be found in pylint/test/messages/<test file name>.txt)
-
2009/01/15 07:55, written by sthenault
-
2009/01/25 01:56, written by mkiilerich
add commentThe error message is E0601.
In variables.VariablesChecker.visit_name a criteria for getting the error is if a variable is used on the line it is defined. That is not right in cases like this. But it seems to be what prevents the error message if it isn't used on the same line.
But another criteria is utils.is_defined_before, and that seems to be where the real problem is; the check doesn't consider "with" statements. The following seems to fix the simple test case:
I haven't been able to run the test suite yet. It is not obvious to me how to add such a 2.5-specific test. It seems like test/input/func_e0601.py could be the right place...
great, thanks for the patch!
I've checked-it in the repository.
FYI, I've added a test by adding your sample code in a file pylint/test/input/func_noerror_defined_and_used_on_same_line_py25.py :
one more thing: to run the functionnal test suite, type "python test_func.py" in pylint test directory.
Please change the state from "open" to "validation pending" or something like that