Skip to content

Commit f87bf23

Browse files
committed
Merge pull request #312 from sigmavirus24/bug/311
Fix #311. Add regex to check for field assignment
2 parents bbe349e + f925a0e commit f87bf23

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

pep8.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
KEYWORD_REGEX = re.compile(r'(\s*)\b(?:%s)\b(\s*)' % r'|'.join(KEYWORDS))
113113
OPERATOR_REGEX = re.compile(r'(?:[^,\s])(\s*)(?:[-+*/|!<=>%&^]+)(\s*)')
114114
LAMBDA_REGEX = re.compile(r'\blambda\b')
115+
IDENTIFIER_REGEX = re.compile('\s*[^.[\]]+\s=')
115116
HUNK_REGEX = re.compile(r'^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@.*$')
116117

117118
# Work around Python < 2.6 behaviour, which does not generate NL after
@@ -923,7 +924,9 @@ def compound_statements(logical_line):
923924
before.count('[') <= before.count(']') and # [1:2] (slice)
924925
before.count('(') <= before.count(')'))): # (annotation)
925926
if LAMBDA_REGEX.search(before):
926-
yield 0, "E731 do not assign a lambda expression, use a def"
927+
if IDENTIFIER_REGEX.match(before):
928+
yield 0, ("E731 do not assign a lambda expression, use a"
929+
" def")
927930
break
928931
if before.startswith('def '):
929932
yield 0, "E704 multiple statements on one line (def)"

testsuite/E73.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,14 @@
55
#: E731:2:5
66
while False:
77
this = lambda y, z: 2 * x
8+
#: Okay
9+
f = object()
10+
f.method = lambda: 'Method'
11+
#: Okay
12+
f = {}
13+
f['a'] = lambda x: x ** 2
14+
#: Okay
15+
f = []
16+
f.append(lambda x: x ** 2)
17+
#: Okay
18+
lambda: 'no-op'

0 commit comments

Comments
 (0)