Patchwork [4,of,4] test-pyflakes: exclude undefined name warning declared as global

login
register
mail settings
Submitter Yuya Nishihara
Date May 2, 2014, 4:29 a.m.
Message ID <56f57f34c1f2b494ab40.1399004940@gimlet>
Download mbox | patch
Permalink /patch/4485/
State Changes Requested
Headers show

Comments

Yuya Nishihara - May 2, 2014, 4:29 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1398743641 -32400
#      Tue Apr 29 12:54:01 2014 +0900
# Branch stable
# Node ID 56f57f34c1f2b494ab403c5c2b544d333cb94184
# Parent  ce23921fe33159c5ddec89474ae4a01caf301481
test-pyflakes: exclude undefined name warning declared as global

If we don't like the coding style that defines global variables in function,
this patch should be dropped.  Instead, we could initialize them as None to
suppress warning.

    _executable = _mountpoint = _service = None
Matt Mackall - May 2, 2014, 5:22 a.m.
On Fri, 2014-05-02 at 13:29 +0900, Yuya Nishihara wrote:
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1398743641 -32400
> #      Tue Apr 29 12:54:01 2014 +0900
> # Branch stable
> # Node ID 56f57f34c1f2b494ab403c5c2b544d333cb94184
> # Parent  ce23921fe33159c5ddec89474ae4a01caf301481
> test-pyflakes: exclude undefined name warning declared as global
> 
> If we don't like the coding style that defines global variables in function,
> this patch should be dropped.  Instead, we could initialize them as None to
> suppress warning.
> 
>     _executable = _mountpoint = _service = None

Let's fix this instead. The fix should come before the test that reports
the bogus names.

Patch

diff --git a/tests/filterpyflakes.py b/tests/filterpyflakes.py
--- a/tests/filterpyflakes.py
+++ b/tests/filterpyflakes.py
@@ -24,6 +24,12 @@  def makekey(typeandline):
     # line as int for ordering 9 before 88
     return msgtype, fname, int(line), message
 
+def collectglobals(data):
+    names = set()
+    for m in re.finditer(r'\bglobal +(\w[\w ,]*)', data):
+        names.update(s.strip() for s in m.group(1).split(','))
+    return names
+
 
 lines = []
 for line in sys.stdin:
@@ -45,6 +51,11 @@  for line in sys.stdin:
     f.close()
     if 'no-' 'check-code' in data:
         continue
+    # assumes variables declared as global are defined. pyflakes cannot
+    # handle them: https://bugs.launchpad.net/pyflakes/+bug/1158585
+    m = re.search(r"undefined name '(.*)'", line)
+    if m and m.group(1) in collectglobals(data):
+        continue
     lines.append((msgtype, line))
 
 for msgtype, line in sorted(lines, key=makekey):
diff --git a/tests/test-check-pyflakes.t b/tests/test-check-pyflakes.t
--- a/tests/test-check-pyflakes.t
+++ b/tests/test-check-pyflakes.t
@@ -16,35 +16,5 @@  run pyflakes on all tracked python scrip
   tests/hghave.py:*: 'pygments' imported but unused (glob)
   tests/hghave.py:*: 'ssl' imported but unused (glob)
   contrib/win32/hgwebdir_wsgi.py:93: 'from isapi.install import *' used; unable to detect undefined names (glob)
-  hgext/factotum.py:61: undefined name '_executable'
-  hgext/factotum.py:66: undefined name '_mountpoint'
-  hgext/factotum.py:105: undefined name '_service'
-  tests/run-tests.py:426: undefined name 'HGTMP'
-  tests/run-tests.py:427: undefined name 'HGTMP'
-  tests/run-tests.py:441: undefined name 'TMPBINDIR'
-  tests/run-tests.py:495: undefined name 'HGTMP'
-  tests/run-tests.py:496: undefined name 'BINDIR'
-  tests/run-tests.py:496: undefined name 'INST'
-  tests/run-tests.py:496: undefined name 'PYTHONDIR'
-  tests/run-tests.py:508: undefined name 'TESTDIR'
-  tests/run-tests.py:514: undefined name 'BINDIR'
-  tests/run-tests.py:518: undefined name 'BINDIR'
-  tests/run-tests.py:523: undefined name 'BINDIR'
-  tests/run-tests.py:540: undefined name 'TESTDIR'
-  tests/run-tests.py:541: undefined name 'PYTHONDIR'
-  tests/run-tests.py:544: undefined name 'TESTDIR'
-  tests/run-tests.py:547: undefined name 'INST'
-  tests/run-tests.py:561: undefined name 'PYTHONDIR'
-  tests/run-tests.py:569: undefined name 'BINDIR'
-  tests/run-tests.py:569: undefined name 'TESTDIR'
-  tests/run-tests.py:572: undefined name 'TESTDIR'
-  tests/run-tests.py:575: undefined name 'TESTDIR'
-  tests/run-tests.py:679: undefined name 'TESTDIR'
-  tests/run-tests.py:927: undefined name 'TESTDIR'
-  tests/run-tests.py:928: undefined name 'TESTDIR'
-  tests/run-tests.py:956: undefined name 'TESTDIR'
-  tests/run-tests.py:967: undefined name 'HGTMP'
-  tests/run-tests.py:1090: undefined name 'PYTHONDIR'
-  tests/run-tests.py:1142: undefined name 'INST'
   
 #endif