tests: simplify and document the sorting of pyflake messages
authorSimon Heimberg <simohe@besonet.ch>
Wed, 26 Jun 2013 23:12:55 +0200
changeset 19335 77440de177f7
parent 19334 95a49112e7ab
child 19336 666aa90258e6
tests: simplify and document the sorting of pyflake messages The pyflake messages are simply ordered by message type, path, line no (and message text). The message type is taken from the order of the filters. The previous ordering looks complicated and illogically. It was the following order (r'\3:\5:\4:\1:\2:' + line): message (\3 and \5) var name (\4) path (\1) line no (\2) line reference Ordering by var name before path looks illogically for me.
tests/filterpyflakes.py
--- a/tests/filterpyflakes.py	Tue Jun 25 13:23:12 2013 -0700
+++ b/tests/filterpyflakes.py	Wed Jun 26 23:12:55 2013 +0200
@@ -4,35 +4,48 @@
 
 import sys, re, os
 
-def makekey(message):
-    # "path/file:line: message"
-    match = re.search(r"(line \d+)", message)
-    line = ''
-    if match:
-        line = match.group(0)
-        message = re.sub(r"(line \d+)", '', message)
-    return re.sub(r"([^:]*):([^:]+):([^']*)('[^']*')(.*)$",
-                  r'\3:\5:\4:\1:\2:' + line,
-                  message)
+def makekey(typeandline):
+    """
+    for sorting lines by: msgtype, path/to/file, lineno, message
+
+    typeandline is a sequence of a message type and the entire message line
+    the message line format is path/to/file:line: message
+
+    >>> makekey((3, 'example.py:36: any message'))
+    (3, 'example.py', 36, ' any message')
+    >>> makekey((7, 'path/to/file.py:68: dummy message'))
+    (7, 'path/to/file.py', 68, ' dummy message')
+    >>> makekey((2, 'fn:88: m')) > makekey((2, 'fn:9: m'))
+    True
+    """
+
+    msgtype, line = typeandline
+    fname, line, message = line.split(":", 2)
+    # line as int for ordering 9 before 88
+    return msgtype, fname, int(line), message
+
 
 lines = []
 for line in sys.stdin:
-    # We whitelist tests
+    # We whitelist tests (see more messages in pyflakes.messages)
     pats = [
             r"imported but unused",
             r"local variable '.*' is assigned to but never used",
             r"unable to detect undefined names",
            ]
-    if not re.search('|'.join(pats), line):
-        continue
+    for msgtype, pat in enumerate(pats):
+        if re.search(pat, line):
+            break # pattern matches
+    else:
+        continue # no pattern matched, next line
     fn = line.split(':', 1)[0]
     f = open(os.path.join(os.path.dirname(os.path.dirname(__file__)), fn))
     data = f.read()
     f.close()
     if 'no-check-code' in data:
         continue
-    lines.append(line)
+    lines.append((msgtype, line))
 
-for line in sorted(lines, key = makekey):
+for msgtype, line in sorted(lines, key = makekey):
     sys.stdout.write(line)
 print