False positive on comprehensions in class scope
Bug #1184732 reported by
Ben Darnell
This bug affects 2 people
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
Pyflakes |
Fix Released
|
Low
|
Unassigned |
Bug Description
Pyflakes reports "undefined name A" on the third line of this code:
class C(object):
A = [1, 2, 3]
B = dict((i, str(i)) for i in A)
This construction works in every version of Python since at least 2.5. (real-world use case from which this was extracted: https:/
Related branches
lp:~pyflakes-dev/pyflakes/1184732-defined-class-level
- Pyflakes Dev: Pending requested
-
Diff: 162 lines (+52/-23)3 files modifiedNEWS.txt (+6/-0)
pyflakes/checker.py (+24/-23)
pyflakes/test/test_undefined_names.py (+22/-0)
Changed in pyflakes: | |
status: | Confirmed → Fix Committed |
Changed in pyflakes: | |
status: | Fix Committed → Fix Released |
To post a comment you must log in.
Changing the except clause at line 444 to also catch ClassScope fixes it, i.e.,
diff --git a/pyflakes/ checker. py b/pyflakes/ checker. py checker. py checker. py
importStarred = self.scope. importStarred [-2:0:- 1]:
importSta rred = importStarred or scope.importStarred
continue
scope[ name].used = (self.scope, node)
index ed9381e..6ed7f9c 100644
--- a/pyflakes/
+++ b/pyflakes/
@@ -441,7 +441,7 @@ class Checker(object):
for scope in self.scopeStack
- if not isinstance(scope, FunctionScope):
+ if not isinstance(scope, (FunctionScope, ClassScope)):
try:
But I'm not sure what the implications of this change are. It seems to lead to some test failures (which I cannot understand how to read).