Skip to content

Commit

Permalink
Report E731 for lambda assignment, return E704 for one-liner def inst…
Browse files Browse the repository at this point in the history
…ead of E701; issue #277
  • Loading branch information
florentx committed May 29, 2014
2 parents 164066c + d6d2f0b commit 2d07233
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 14 deletions.
4 changes: 4 additions & 0 deletions docs/intro.rst
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ This is the current list of error and warning codes:
+----------+----------------------------------------------------------------------+
| E703 | statement ends with a semicolon |
+----------+----------------------------------------------------------------------+
| E704 | multiple statements on one line (def) |
+----------+----------------------------------------------------------------------+
| E711 (^) | comparison to None should be 'if cond is None:' |
+----------+----------------------------------------------------------------------+
| E712 (^) | comparison to True should be 'if cond is True:' or 'if cond:' |
Expand All @@ -315,6 +317,8 @@ This is the current list of error and warning codes:
+----------+----------------------------------------------------------------------+
| E721 | do not compare types, use 'isinstance()' |
+----------+----------------------------------------------------------------------+
| E731 | do not assign a lambda expression, use a def |
+----------+----------------------------------------------------------------------+
+----------+----------------------------------------------------------------------+
| **E9** | *Runtime* |
+----------+----------------------------------------------------------------------+
Expand Down
21 changes: 15 additions & 6 deletions pep8.py
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,9 @@ def compound_statements(logical_line):
on the same line, never do this for multi-clause statements.
Also avoid folding such long lines!
Always use a def statement instead of an assignment statement that
binds a lambda expression directly to a name.
Okay: if foo == 'blah':\n do_blah_thing()
Okay: do_one()
Okay: do_two()
Expand All @@ -847,20 +850,26 @@ def compound_statements(logical_line):
E701: try: something()
E701: finally: cleanup()
E701: if foo == 'blah': one(); two(); three()
E702: do_one(); do_two(); do_three()
E703: do_four(); # useless semicolon
E704: def f(x): return 2*x
E731: f = lambda x: 2*x
"""
line = logical_line
last_char = len(line) - 1
found = line.find(':')
while -1 < found < last_char:
before = line[:found]
if (before.count('{') <= before.count('}') and # {'a': 1} (dict)
before.count('[') <= before.count(']') and # [1:2] (slice)
before.count('(') <= before.count(')') and # (Python 3 annotation)
not LAMBDA_REGEX.search(before)): # lambda x: x
yield found, "E701 multiple statements on one line (colon)"
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):
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)"
else:
yield found, "E701 multiple statements on one line (colon)"
found = line.find(':', found + 1)
found = line.find(';')
while -1 < found:
Expand Down
4 changes: 2 additions & 2 deletions testsuite/E22.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ def squares(n):
negative = -1
spam(-1)
-negative
lambda *args, **kw: (args, kw)
lambda a, b=h[:], c=0: (a, b, c)
func1(lambda *args, **kw: (args, kw))
func2(lambda a, b=h[:], c=0: (a, b, c))
if not -5 < x < +5:
print >>sys.stderr, "x is out of range."
print >> sys.stdout, "x is an integer."
Expand Down
19 changes: 13 additions & 6 deletions testsuite/E70.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
#: E701
#: E701:1:5
if a: a = False
#: E701
#: E701:1:40
if not header or header[:6] != 'bytes=': return
#: E702
#: E702:1:10
a = False; b = True
#: E702
#: E702:1:17
import bdist_egg; bdist_egg.write_safety_flag(cmd.egg_info, safe)
#: E703
#: E703:1:13
import shlex;
#: E702 E703
#: E702:1:9 E703:1:23
del a[:]; a.append(42);
#: E704:1:1
def f(x): return 2
#: E704:1:1 E226:1:19
def f(x): return 2*x
#: E704:2:5 E226:2:23
while all is round:
def f(x): return 2*x
#:
7 changes: 7 additions & 0 deletions testsuite/E73.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#: E731:1:1
f = lambda x: 2 * x
#: E731:1:1 E226:1:16
f = lambda x: 2*x
#: E731:2:5
while False:
this = lambda y, z: 2 * x

0 comments on commit 2d07233

Please sign in to comment.