changeset 2445:2dc7a3be8ec9

test_sourcecode: check for some silly comparisons with None/True/False
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Fri, 20 Jul 2007 18:39:33 +0200
parents 12854ba50c0d
children 70b1a82ec75b
files MoinMoin/_tests/pep8.py MoinMoin/_tests/test_sourcecode.py
diffstat 2 files changed, 31 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/_tests/pep8.py	Fri Jul 20 17:52:56 2007 +0200
+++ b/MoinMoin/_tests/pep8.py	Fri Jul 20 18:39:33 2007 +0200
@@ -99,6 +99,8 @@
 
 indent_match = re.compile(r'([ \t]*)').match
 raise_comma_match = re.compile(r'raise\s+\w+\s*(,)').match
+equals_boolean_search = re.compile(r'([!=]=\s*(True|False))|((True|False)\s*[!=]=)').search
+equals_None_search = re.compile(r'([!=]=\s*None)|(None\s*[!=]=)').search
 
 operators = """
 +  -  *  /  %  ^  &  |  =  <  >  >>  <<
@@ -415,6 +417,34 @@
         return match.start(1), "W602 deprecated form of raising exception"
 
 
+def dumb_equals_boolean(logical_line):
+    """
+    Using "if x == True:" or "if x == False:" is wrong in any case:
+
+    First if you already have a boolean, you don't need to compare it to
+    another boolean. Just use "if x:" or "if not x:".
+
+    Second, even if you have some sort of "tristate logic", not only using
+    True/False, but other values, then you want to use "if x is True:" or
+    "if x is False:" because there is exactly one True and one False object.
+    """
+    match = equals_boolean_search(logical_line)
+    if match:
+        return match.start(1), "E798 don't use 'x == <boolean>', but just 'x' or 'not x' or 'x is <boolean>'"
+
+
+def dumb_equals_None(logical_line):
+    """
+    Using "if x == None:" is wrong in any case:
+
+    You either want to use "if x is None:" (there is only 1 None object) or -
+    in some simple cases - just "if not x:".
+    """
+    match = equals_None_search(logical_line)
+    if match:
+        return match.start(1), "E799 don't use 'x == None', but just 'x is None' or 'not x'"
+
+
 ##############################################################################
 # Helper functions
 ##############################################################################
--- a/MoinMoin/_tests/test_sourcecode.py	Fri Jul 20 17:52:56 2007 +0200
+++ b/MoinMoin/_tests/test_sourcecode.py	Fri Jul 20 18:39:33 2007 +0200
@@ -29,8 +29,8 @@
                              # use 'fix' with extreme caution and in a separate changeset!
 FIX_TS_RE = re.compile(r' +$', re.M) # 'fix' mode: everything matching the trailing space re will be removed
 
-
 RECENTLY = time.time() - 7 * 24*60*60 # we only check stuff touched recently.
+#RECENTLY = 0 # check everything!
 # After doing a fresh clone, this procedure is recommended:
 # 1. Run the tests once to see if everything is OK (as cloning updates the mtime,
 #    it will test every file).