From 397719c883b0a334585e8b902de725ad029e5ca4 Mon Sep 17 00:00:00 2001 From: Ian Cordasco Date: Thu, 7 Aug 2014 13:39:18 -0500 Subject: Fix #311. Add regex to check for field assignment --- pep8.py | 5 ++++- testsuite/E73.py | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/pep8.py b/pep8.py index b31a978..f9a3d5e 100755 --- a/pep8.py +++ b/pep8.py @@ -108,6 +108,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') +FIELD_ASSIGNMENT_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 @@ -872,7 +873,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 not FIELD_ASSIGNMENT_REGEX.search(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..ede1403 100644 --- a/testsuite/E73.py +++ b/testsuite/E73.py @@ -5,3 +5,6 @@ f = lambda x: 2*x #: E731:2:5 while False: this = lambda y, z: 2 * x +#: Okay +f = object() +f.method = lambda: 'Method' -- cgit v1.2.1 From f925a0e017ec686f9f638a8038dc5e0d0161d6d7 Mon Sep 17 00:00:00 2001 From: Ian Cordasco Date: Fri, 8 Aug 2014 19:38:04 -0500 Subject: Update fix for E731 Thanks to @mypalmike for thinking of the extra cases --- pep8.py | 4 ++-- testsuite/E73.py | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/pep8.py b/pep8.py index f9a3d5e..8e16faf 100755 --- a/pep8.py +++ b/pep8.py @@ -108,7 +108,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') -FIELD_ASSIGNMENT_REGEX = re.compile('\s*.+\..+\s=') +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 @@ -873,7 +873,7 @@ def compound_statements(logical_line): before.count('[') <= before.count(']') and # [1:2] (slice) before.count('(') <= before.count(')'))): # (annotation) if LAMBDA_REGEX.search(before): - if not FIELD_ASSIGNMENT_REGEX.search(before): + if IDENTIFIER_REGEX.match(before): yield 0, ("E731 do not assign a lambda expression, use a" " def") break diff --git a/testsuite/E73.py b/testsuite/E73.py index ede1403..aab8a24 100644 --- a/testsuite/E73.py +++ b/testsuite/E73.py @@ -8,3 +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' -- cgit v1.2.1