summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorent Xicluna <florent.xicluna@gmail.com>2014-12-16 01:15:15 +0100
committerFlorent Xicluna <florent.xicluna@gmail.com>2014-12-16 01:15:15 +0100
commitf87bf238186741169a62ee0e9827068c209bed6f (patch)
treea2872531647fb4a8328e4032ffc17daaac557b17
parentbbe349e1bed836349ab1c39bdd7b3a9c85979443 (diff)
parentf925a0e017ec686f9f638a8038dc5e0d0161d6d7 (diff)
downloadpep8-f87bf238186741169a62ee0e9827068c209bed6f.tar.gz
Merge pull request #312 from sigmavirus24/bug/311
Fix #311. Add regex to check for field assignment
-rwxr-xr-xpep8.py5
-rw-r--r--testsuite/E73.py11
2 files changed, 15 insertions, 1 deletions
diff --git a/pep8.py b/pep8.py
index d6f1617..87e2881 100755
--- a/pep8.py
+++ b/pep8.py
@@ -112,6 +112,7 @@ 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,7 +924,9 @@ def compound_statements(logical_line):
before.count('[') <= before.count(']') and # [1:2] (slice)
before.count('(') <= before.count(')'))): # (annotation)
if LAMBDA_REGEX.search(before):
- yield 0, "E731 do not assign a lambda expression, use a def"
+ if IDENTIFIER_REGEX.match(before):
+ 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)"
diff --git a/testsuite/E73.py b/testsuite/E73.py
index 0673e0f..aab8a24 100644
--- a/testsuite/E73.py
+++ b/testsuite/E73.py
@@ -5,3 +5,14 @@ f = lambda x: 2*x
#: E731:2:5
while False:
this = lambda y, z: 2 * x
+#: 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'