-
Notifications
You must be signed in to change notification settings - Fork 179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
False positive undefined name after del in branch #175
Comments
Original comment by florent.x (@florentx?) on Launchpad: I agree with the initial bug reported : probably we could do better in this case. The issue reported by @ramalho is not related to the initial report, and it's not an issue at all:
|
Original comment by blueyed (@blueyed?) on Launchpad: I came across a similar issue like in #175 (comment), where flake8/pyflakes on python3 reports "F821: undefined name 'unicode'" , but the code path is not triggered for Python3:
|
Original comment by jayvdb (@jayvdb?) on Launchpad: This bug is about |
This patch add new feature to automatically expand a star (wildcard) import to specify all names that used inside the code. A sample code like this ```python from math import * sin(1) cos(0) ``` will be changed into ```python from math import cos, sin sin(1) cos(0) ``` Note that there are still 2 bugs regarding this feature, which mainly caused by related upstream bug from pyflakes: 1. A function/names that declared but later deleted by `del` command will raise a false positive that the names is undeclared and could possibly come from a star import (if present). PyCQA/pyflakes#175 2. pyflakes is "inconsistent" on defining an undefined var in case of __all__ is used (like in module API files). ```python from foo import * # contain function_1 and function_2 __all__ = ['function_1', 'function_2', 'function_3'] function_2() # just use it somewhere to trigger pyflake def function_3: return 'something' ``` pyflakes will complain that function_2 is undefined and possibly come from module foo. The import then will be expanded into... ```python from foo import function_2 ``` But then pyflakes will complain function_1 is undefined because its used in `__all__` Closes PyCQA#14
This patch add new feature to automatically expand a star (wildcard) import to specify all names that used inside the code. A sample code like this ```python from math import * sin(1) cos(0) ``` will be changed into ```python from math import cos, sin sin(1) cos(0) ``` Note that there are still 2 bugs regarding this feature, which mainly caused by related upstream bug from pyflakes: 1. A function/names that declared but later deleted by `del` command will raise a false positive that the names is undeclared and could possibly come from a star import (if present). PyCQA/pyflakes#175 2. pyflakes is "inconsistent" on defining an undefined var in case of __all__ is used (like in module API files). ```python from foo import * # contain function_1 and function_2 __all__ = ['function_1', 'function_2', 'function_3'] function_2() # just use it somewhere to trigger pyflake def function_3: return 'something' ``` pyflakes will complain that function_2 is undefined and possibly come from module foo. The import then will be expanded into... ```python from foo import function_2 ``` But then pyflakes will complain function_1 is undefined because its used in `__all__` Closes PyCQA#14
This patch add new feature to automatically expand a star (wildcard) import to specify all names that used inside the code. A sample code like this ```python from math import * sin(1) cos(0) ``` will be changed into ```python from math import cos, sin sin(1) cos(0) ``` Note that there are still 2 bugs regarding this feature, which mainly caused by related upstream bug from pyflakes: 1. A function/names that declared but later deleted by `del` command will raise a false positive that the names is undeclared and could possibly come from a star import (if present). PyCQA/pyflakes#175 2. pyflakes is "inconsistent" on defining an undefined var in case of __all__ is used (like in module API files). ```python from foo import * # contain function_1 and function_2 __all__ = ['function_1', 'function_2', 'function_3'] function_2() # just use it somewhere to trigger pyflake def function_3: return 'something' ``` pyflakes will complain that function_2 is undefined and possibly come from module foo. The import then will be expanded into... ```python from foo import function_2 ``` But then pyflakes will complain function_1 is undefined because its used in `__all__` Closes PyCQA#14
This patch add new feature to automatically expand a star (wildcard) import to specify all names that used inside the code. A sample code like this ```python from math import * sin(1) cos(0) ``` will be changed into ```python from math import cos, sin sin(1) cos(0) ``` Note that there are still 2 bugs regarding this feature, which mainly caused by related upstream bug from pyflakes: 1. A function/names that declared but later deleted by `del` command will raise a false positive that the names is undeclared and could possibly come from a star import (if present). PyCQA/pyflakes#175 2. pyflakes is "inconsistent" on defining an undefined var in case of __all__ is used (like in module API files). ```python from foo import * # contain function_1 and function_2 __all__ = ['function_1', 'function_2', 'function_3'] function_2() # just use it somewhere to trigger pyflake def function_3: return 'something' ``` pyflakes will complain that function_2 is undefined and possibly come from module foo. The import then will be expanded into... ```python from foo import function_2 ``` But then pyflakes will complain function_1 is undefined because its used in `__all__` Closes PyCQA#14
Is there a recommended way of dealing with this issue? Or is adding a |
The underlying issue is conditional branches are "invisible" to pyflakes. So def foo():
bar = 1
if 0:
del bar
else:
del bar looks like: def foo():
bar = 1
del bar
del bar Once you understand what's going on under the hood, it's often possible to rewrite the code in a way to avoid the issue, though the specific solution depends on the specific situation. This has been a long-standing bug since it's a complex problem to track all the possible paths though the code and calculate all the possible states of the variable assignments. |
collecting duplicates for "pyflakes doesn't do branch analysis" here: #715 |
Original report by dimaqq (@dimaqq?) on Launchpad:
Consider this test code:
pyflakes reports test.py:6: undefined name 'bar', that is 2nd
del bar
is considered in error.understandably it is impossible to analyse all code paths in general case, so how about tagging
bar
asuncertain if label is present
and allowing todel
it?The text was updated successfully, but these errors were encountered: