summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarius Gedminas <marius@gedmin.as>2019-07-09 16:37:43 +0300
committerSteven Myint <git@stevenmyint.com>2019-07-09 06:37:43 -0700
commit2136e1e9f4559930114c82ba316daada7d31836a (patch)
tree3175461058241a87cb911f4e7a887c5a890dabeb
parent4a807d45f9dd47266c82035bf6e04508a77f0258 (diff)
downloadpyflakes-2136e1e9f4559930114c82ba316daada7d31836a.tar.gz
Fix NoneType has no attribute parent when handling doctests (#452)
Do not define a special `_` name in doctest scope when the global scope already has a `_`. This mirrors the way doctests actually work: the special `_` value is assigned only when a global of that name doesn't already exist. Fixes #421.
-rw-r--r--pyflakes/checker.py3
-rw-r--r--pyflakes/test/test_doctests.py10
2 files changed, 12 insertions, 1 deletions
diff --git a/pyflakes/checker.py b/pyflakes/checker.py
index 83f92aa..86cfd11 100644
--- a/pyflakes/checker.py
+++ b/pyflakes/checker.py
@@ -1162,7 +1162,8 @@ class Checker(object):
self.scopeStack = [self.scopeStack[0]]
node_offset = self.offset or (0, 0)
self.pushScope(DoctestScope)
- self.addBinding(None, Builtin('_'))
+ if '_' not in self.scopeStack[0]:
+ self.addBinding(None, Builtin('_'))
for example in examples:
try:
tree = ast.parse(example.source, "<doctest>")
diff --git a/pyflakes/test/test_doctests.py b/pyflakes/test/test_doctests.py
index 0825ffe..5565226 100644
--- a/pyflakes/test/test_doctests.py
+++ b/pyflakes/test/test_doctests.py
@@ -433,6 +433,16 @@ class Test(TestCase):
return 1
''')
+ def test_globalUnderscoreInDoctest(self):
+ self.flakes("""
+ from gettext import ugettext as _
+
+ def doctest_stuff():
+ '''
+ >>> pass
+ '''
+ """, m.UnusedImport)
+
class TestOther(_DoctestMixin, TestOther):
"""Run TestOther with each test wrapped in a doctest."""