pycodestyle icon indicating copy to clipboard operation
pycodestyle copied to clipboard

E122: Suspicious error for arguments of method called on multiline glued string literal

Open east825 opened this issue 11 years ago • 3 comments

This is kind of tricky case. Suppose I have the following Python snippet:

def func(*args):
    pass


func('{foo} '
     'bar'.format(
    foo='baz'))

Everything is fine here except warning "7:5: E122 continuation line missing indentation or outdented" about foo keyword argument of format. In fact the only indentation accepted by pep8 for it is 4 spaces to the right relatively to the start of the string literal, i.e.

def func(*args):
    pass


func('{foo} '
     'bar'.format(
         foo='baz'))

What is suspicious to me is that for any other kind of first argument of func that spans several physical lines, there are two positions for foo accepted by pep8: 4 characters to the right of previous physical line and 4 characters to the right relatively to func. E.g. for dict literal

# Both cases are valid for pep8
func({'foo': 1,
      'bar': 2}.update(
    foo='baz'))


func({'foo': 1,
      'bar': 2}.update(
          foo='baz'))

or dict constructor:

# Both cases are valid for pep8
func(dict(foo=1,
          bar=2).update(
    foo='baz'))


func(dict(foo=1,
          bar=2).update(
              foo='baz'))

Moreover if I merely wrap glued string literal in parenthesis, pep8 doesn't complain about normal indentation anymore, i.e.

# It's ok however
func(('{foo} '
     'bar').format(
    foo='baz'))

Four space indentation relative to the start of func seems more natural to me and in my opinion it doesn't violate any PEP-8 guidelines. Am I wrong? Is it done so intentionally?

east825 avatar Jan 19 '15 16:01 east825