Replace the IDENTIFIER_REGEX with the isidentifier function
authorFlorent Xicluna <florent.xicluna@gmail.com>
Tue, 16 Dec 2014 00:38:56 +0000 (01:38 +0100)
committerFlorent Xicluna <florent.xicluna@gmail.com>
Tue, 16 Dec 2014 00:38:56 +0000 (01:38 +0100)
pep8.py
testsuite/E73.py

diff --git a/pep8.py b/pep8.py
index 87e288125efca5f08e05277a4cdb87c8e3c4d8af..cbd3068e4b3c6b133c658b05c5f335ba604431f7 100755 (executable)
--- a/pep8.py
+++ b/pep8.py
@@ -112,7 +112,6 @@ COMPARE_TYPE_REGEX = re.compile(r'(?:[=!]=|is(?:\s+not)?)\s*type(?:s.\w+Type'
 KEYWORD_REGEX = re.compile(r'(\s*)\b(?:%s)\b(\s*)' % r'|'.join(KEYWORDS))
 OPERATOR_REGEX = re.compile(r'(?:[^,\s])(\s*)(?:[-+*/|!<=>%&^]+)(\s*)')
 LAMBDA_REGEX = re.compile(r'\blambda\b')
-IDENTIFIER_REGEX = re.compile('\s*[^.[\]]+\s=')
 HUNK_REGEX = re.compile(r'^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@.*$')
 
 # Work around Python < 2.6 behaviour, which does not generate NL after
@@ -923,10 +922,11 @@ def compound_statements(logical_line):
         if ((before.count('{') <= before.count('}') and   # {'a': 1} (dict)
              before.count('[') <= before.count(']') and   # [1:2] (slice)
              before.count('(') <= before.count(')'))):    # (annotation)
-            if LAMBDA_REGEX.search(before):
-                if IDENTIFIER_REGEX.match(before):
-                    yield 0, ("E731 do not assign a lambda expression, use a"
-                              " def")
+            lambda_kw = LAMBDA_REGEX.search(before)
+            if lambda_kw:
+                before = line[:lambda_kw.start()].rstrip()
+                if before[-1:] == '=' and isidentifier(before[:-1].strip()):
+                    yield 0, "E731 do not assign a lambda expression, use a def"
                 break
             if before.startswith('def '):
                 yield 0, "E704 multiple statements on one line (def)"
index aab8a24ed38d01717c81bd02ece1b0a1f58b34d3..60fcd2390887ffa95291096d1a3d035bb780b8b2 100644 (file)
@@ -8,11 +8,11 @@ while False:
 #: Okay
 f = object()
 f.method = lambda: 'Method'
-#: Okay
+
 f = {}
 f['a'] = lambda x: x ** 2
-#: Okay
+
 f = []
 f.append(lambda x: x ** 2)
-#: Okay
+
 lambda: 'no-op'