Skip to content

Commit 2d07233

Browse files
committed
Report E731 for lambda assignment, return E704 for one-liner def instead of E701; issue #277
2 parents 164066c + d6d2f0b commit 2d07233

File tree

5 files changed

+41
-14
lines changed

5 files changed

+41
-14
lines changed

docs/intro.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,8 @@ This is the current list of error and warning codes:
305305
+----------+----------------------------------------------------------------------+
306306
| E703 | statement ends with a semicolon |
307307
+----------+----------------------------------------------------------------------+
308+
| E704 | multiple statements on one line (def) |
309+
+----------+----------------------------------------------------------------------+
308310
| E711 (^) | comparison to None should be 'if cond is None:' |
309311
+----------+----------------------------------------------------------------------+
310312
| E712 (^) | comparison to True should be 'if cond is True:' or 'if cond:' |
@@ -315,6 +317,8 @@ This is the current list of error and warning codes:
315317
+----------+----------------------------------------------------------------------+
316318
| E721 | do not compare types, use 'isinstance()' |
317319
+----------+----------------------------------------------------------------------+
320+
| E731 | do not assign a lambda expression, use a def |
321+
+----------+----------------------------------------------------------------------+
318322
+----------+----------------------------------------------------------------------+
319323
| **E9** | *Runtime* |
320324
+----------+----------------------------------------------------------------------+

pep8.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,9 @@ def compound_statements(logical_line):
834834
on the same line, never do this for multi-clause statements.
835835
Also avoid folding such long lines!
836836
837+
Always use a def statement instead of an assignment statement that
838+
binds a lambda expression directly to a name.
839+
837840
Okay: if foo == 'blah':\n do_blah_thing()
838841
Okay: do_one()
839842
Okay: do_two()
@@ -847,20 +850,26 @@ def compound_statements(logical_line):
847850
E701: try: something()
848851
E701: finally: cleanup()
849852
E701: if foo == 'blah': one(); two(); three()
850-
851853
E702: do_one(); do_two(); do_three()
852854
E703: do_four(); # useless semicolon
855+
E704: def f(x): return 2*x
856+
E731: f = lambda x: 2*x
853857
"""
854858
line = logical_line
855859
last_char = len(line) - 1
856860
found = line.find(':')
857861
while -1 < found < last_char:
858862
before = line[:found]
859-
if (before.count('{') <= before.count('}') and # {'a': 1} (dict)
860-
before.count('[') <= before.count(']') and # [1:2] (slice)
861-
before.count('(') <= before.count(')') and # (Python 3 annotation)
862-
not LAMBDA_REGEX.search(before)): # lambda x: x
863-
yield found, "E701 multiple statements on one line (colon)"
863+
if ((before.count('{') <= before.count('}') and # {'a': 1} (dict)
864+
before.count('[') <= before.count(']') and # [1:2] (slice)
865+
before.count('(') <= before.count(')'))): # (annotation)
866+
if LAMBDA_REGEX.search(before):
867+
yield 0, "E731 do not assign a lambda expression, use a def"
868+
break
869+
if before.startswith('def '):
870+
yield 0, "E704 multiple statements on one line (def)"
871+
else:
872+
yield found, "E701 multiple statements on one line (colon)"
864873
found = line.find(':', found + 1)
865874
found = line.find(';')
866875
while -1 < found:

testsuite/E22.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ def squares(n):
135135
negative = -1
136136
spam(-1)
137137
-negative
138-
lambda *args, **kw: (args, kw)
139-
lambda a, b=h[:], c=0: (a, b, c)
138+
func1(lambda *args, **kw: (args, kw))
139+
func2(lambda a, b=h[:], c=0: (a, b, c))
140140
if not -5 < x < +5:
141141
print >>sys.stderr, "x is out of range."
142142
print >> sys.stdout, "x is an integer."

testsuite/E70.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
1-
#: E701
1+
#: E701:1:5
22
if a: a = False
3-
#: E701
3+
#: E701:1:40
44
if not header or header[:6] != 'bytes=': return
5-
#: E702
5+
#: E702:1:10
66
a = False; b = True
7-
#: E702
7+
#: E702:1:17
88
import bdist_egg; bdist_egg.write_safety_flag(cmd.egg_info, safe)
9-
#: E703
9+
#: E703:1:13
1010
import shlex;
11-
#: E702 E703
11+
#: E702:1:9 E703:1:23
1212
del a[:]; a.append(42);
13+
#: E704:1:1
14+
def f(x): return 2
15+
#: E704:1:1 E226:1:19
16+
def f(x): return 2*x
17+
#: E704:2:5 E226:2:23
18+
while all is round:
19+
def f(x): return 2*x
1320
#:

testsuite/E73.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#: E731:1:1
2+
f = lambda x: 2 * x
3+
#: E731:1:1 E226:1:16
4+
f = lambda x: 2*x
5+
#: E731:2:5
6+
while False:
7+
this = lambda y, z: 2 * x

0 commit comments

Comments
 (0)