PyCQA / PyCQA/pycodestyle

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

Open
#372 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.2k
Forks
754
PR merge metrics
No merged PRs in 30d

Description

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?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Run the three snippets through pycodestyle and compare the E122 result for the glued string literal with the dict and parenthesized cases. Trace the E122 continuation-indentation check in the repository's single Python implementation, then confirm whether the normal indentation should be accepted without changing the documented behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.