The following patch fixes various problems with warnings C0322 (Operator not
preceded by a space) and C0323 (Operator not followed by a space), in
particular:

1.  These warnings incorrectly warned about uses of the >> and << operators.
2.  These warnings didn't recognize the <> operator.
3.  These warnings didn't recognize all the augmented assignment operators
(namely //=, %=, **=, >>=, <<=, &=, ^= and |=).

For example:

x = 0

# All of the following lines incorrectly generate warnings.
print 1>> 2
print 1<< 2
print 1 >>2
print 1 <<2

# The following line fails to generate a C0322 or C0323 warning
# (though it does generate a W0331 warning).
print 1<>2

# The following lines fail to generate any warnings.
x//= 2
x**= 2
x&= 2
x^= 2
x|= 2

# The following lines do generate warnings, but the ^^^s in the warning text
# point to only part of the operators (a very cosmetic issue).
x%= 2
x<<= 2
x>>= 2
x %=2
x <<=2
x >>=2

I also restructured the regexes used by the implementation of these warnings
to make them easier to understand.

--- pylint-0.22.0/checkers/format.py.orig    2011-03-08 12:21:07.126160641
+0000
+++ pylint-0.22.0/checkers/format.py    2011-03-08 14:40:25.217122318 +0000
@@ -55,11 +55,15 @@
     'C0321': ('More than one statement on a single line',
               'Used when more than on statement are found on the same
line.'),
     'C0322': ('Operator not preceded by a space\n%s',
-              'Used when one of the following operator (!= | <= | == | >= |
< '
-              '| > | = | \+= | -= | \*= | /= | %) is not preceded by a
space.'),
+              'Used when one of the following operators '
+              '(!= | <> | <= | == | >= | < | > '
+              '| = | += | -= | *= | /= | //= | %= | **= | >>= | <<= | &= |
^= | |= '
+              '| %) is not preceded by a space.'),
     'C0323': ('Operator not followed by a space\n%s',
-              'Used when one of the following operator (!= | <= | == | >= |
< '
-              '| > | = | \+= | -= | \*= | /= | %) is not followed by a
space.'),
+              'Used when one of the following operators '
+              '(!= | <> | <= | == | >= | < | > '
+              '| = | += | -= | *= | /= | //= | %= | **= | >>= | <<= | &= |
^= | |= '
+              '| %) is not followed by a space.'),
     'C0324': ('Comma not followed by a space\n%s',
               'Used when a comma (",") is not followed by a space.'),

@@ -91,13 +95,16 @@

 COMMENT_RGX = re.compile("#.*$", re.M)

-OPERATORS = r'!=|<=|==|>=|<|>|=|\+=|-=|\*=|/=|%'
+ASSIGN_OPS =
r'(?<![<>=])=(?![<>=])|\+=|-=|\*=|/=|//=|%=|\*\*=|>>=|<<=|&=|\^=|\|='
+REL_OPS = r'!=|<>|<=|==|>=|(?<!<)<(?!<)|(?<!>)>(?!>)'
+OPERATORS = '%s|%s|%%' % ( ASSIGN_OPS, REL_OPS )
+OPERATOR_CHARS = r'[<>=+\-*/!%^&|]'

-OP_RGX_MATCH_1 = r'[^(]*(?<!\s|\^|<|>|=|\+|-|\*|/|!|%%|&|\|)(%s).*' %
OPERATORS
-OP_RGX_SEARCH_1 = r'(?<!\s|\^|<|>|=|\+|-|\*|/|!|%%|&|\|)(%s)' % OPERATORS
+OP_RGX_MATCH_1 = r'[^(]*(?<!\s|%s)(%s)' % ( OPERATOR_CHARS, OPERATORS )
+OP_RGX_SEARCH_1 = r'(?<!\s|%s)(%s)' % ( OPERATOR_CHARS, OPERATORS )

-OP_RGX_MATCH_2 = r'[^(]*(%s)(?!\s|=|>|<).*' % OPERATORS
-OP_RGX_SEARCH_2 = r'(%s)(?!\s|=|>)' % OPERATORS
+OP_RGX_MATCH_2 = r'[^(]*(%s)(?!\s)' % OPERATORS
+OP_RGX_SEARCH_2 = r'(%s)(?!\s)' % OPERATORS

 BAD_CONSTRUCT_RGXS = (

@@ -109,7 +116,7 @@
      re.compile(OP_RGX_SEARCH_2, re.M),
      'C0323'),

-    (re.compile(r'.*,[^(\s|\]|}|\))].*', re.M),
+    (re.compile(r'.*,[^(\s|\]|}|\))]', re.M),
      re.compile(r',[^\s)]', re.M),
      'C0324'),
     )
_______________________________________________
Python-Projects mailing list
[email protected]
http://lists.logilab.org/mailman/listinfo/python-projects

Reply via email to