@@ -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 :
0 commit comments