used-before-assignment when variable possibly defined in try, and used in finally #9639
Labels
C: used-before-assignment
Issues related to 'used-before-assignment' check
Duplicate 🐫
Duplicate of an already existing issue
Bug description
Configuration
No response
Command used
Pylint output
Expected behavior
The variable is certainly assigned before use, in all cases unless an exception takes place -- this is why func() succeeds but func2() fails.
In the case of func3() the assignment could be hoisted out of the conditional, but in the case of func2() it cannot easily be. It's really func2() I am concerned about.
Real world code exhibiting func2()'s design: https://github.com/mesonbuild/meson/blob/7d28ff29396f9d7043204de8ddc52226b9903811/mesonbuild/compilers/detect.py#L1143-L1160
If I know that the assignment cannot fail -- but later parts of the try can fail -- then it is definitely not a used-before-assignment. So the lint violation should actually be possibly-used-before-assignment since pylint cannot know whether or not the assignment is exception-proof.
EDIT: ok I overlooked that in my real world code it does report "possibly", but while diagnosing it and writing a test case I got a "used-before-assignment" and forgot what the real code does. So maybe nothing need be for me... the test case is still interesting I guess.
Pylint version
OS / Environment
Gentoo Linux
Additional dependencies
No response
The text was updated successfully, but these errors were encountered: