Pycodestyle: The preferred place to break around a binary operator is after the operator, not before it

Created on 14 May 2013  路  14Comments  路  Source: PyCQA/pycodestyle

Pep8 recommends to break a long line after a binary operator.
This library doesn't catch when we break before the operator.

feature

Most helpful comment

I think this is a misreading of PEP8. The PEP allows both, but weakly recommends the opposite behaviour, suggesting Knuth's style of breaking _before the operator_ for new code.

From the PEP:

# No: operators sit far away from their operands
income = (gross_wages +
          taxable_interest +
          (dividends - qualified_dividends) -
          ira_deduction -
          student_loan_interest)

# Yes: easy to match operators with operands
income = (gross_wages
          + taxable_interest
          + (dividends - qualified_dividends)
          - ira_deduction
          - student_loan_interest)

https://www.python.org/dev/peps/pep-0008/#should-a-line-break-before-or-after-a-binary-operator

I can disable this locally for myself, but I don't think this one should be turned on by default.

All 14 comments

You'd like a new error then for when someone does something like:

a = ('abcdefghijk'
     + 'lmnop')

Instead of

a = ('abcdefghijk' +
     'lmnop')

Correct?

Correct.

+1

Closed with Pull #305 . Thanks for the report!

I think this is a misreading of PEP8. The PEP allows both, but weakly recommends the opposite behaviour, suggesting Knuth's style of breaking _before the operator_ for new code.

From the PEP:

# No: operators sit far away from their operands
income = (gross_wages +
          taxable_interest +
          (dividends - qualified_dividends) -
          ira_deduction -
          student_loan_interest)

# Yes: easy to match operators with operands
income = (gross_wages
          + taxable_interest
          + (dividends - qualified_dividends)
          - ira_deduction
          - student_loan_interest)

https://www.python.org/dev/peps/pep-0008/#should-a-line-break-before-or-after-a-binary-operator

I can disable this locally for myself, but I don't think this one should be turned on by default.

Sorry for commenting on a very old issue. Looks like PEP8 has have been updated, there was more discussion about this in #498.

@larsyencken -- No worries! That's one of the difficulties of this tool, it's trying to hit a moving target, as the actual PEP is updated from time to time.

I can't get W504 passing after this fix and recent pycodestyle release.

This isn't passing:

expected_url = (CONFIG['client']['url_prefix']
                + CONFIG['client']['url_path']
                + CONFIG['client']['create_parcel'].format(service_code='runjump'))

Neither is this:

expected_url = (CONFIG['client']['url_prefix'] +
                CONFIG['client']['url_path'] +
                CONFIG['client']['create_parcel'].format(service_code='runjump'))

Is there possibly a bug in the latest pycodestyle release, or can someone tell me what I am doing wrong, in the mean time I'm having to disable W504 as this is driving me pretty crazy :)

@robvdl "recent pycodestyle releases" is unfortunately vague. Can you please list which versions you've tried and your configuration?

To be clear, W503 and W504 which control the behaviour here are both disabled by default in 2.4.0, so it's unclear how you're encountering W504: https://github.com/PyCQA/pycodestyle/blob/036461879fbfdb4ddbb689163e968d7f00ded66f/pycodestyle.py#L84

This one bit me too today.

$ pycodestyle --ignore=E111,E221,E114,E501,C901 .
./tests/testd.py:45:24: W503 line break before binary operator
./tests/testd.py:46:24: W503 line break before binary operator
ERROR: Job failed: exit code 1

Version info:

Collecting pycodestyle
  Downloading https://files.pythonhosted.org/packages/e5/c6/blablabla/pycodestyle-2.4.0-py2.py3-none-any.whl (62kB)

Could it be that the --ignore overrides the internal defaults instead of adding to them?

Yes, that is precisely what it does and is also mentioned in the docs.

It could be possible I was getting W504 after a pycodestyle upgrade because I already had some ignores defined in setup.cfg that I was still trying to resolve after taking over a codebase.

Is it possible that once you define ignores in setup.cfg the defaults no longer apply? and therefore get replaced by my ignore list defined in setup.cfg

edit: I see I am basically asking the same question as @Mausy5043 I was observing this too.

Yes, once you define a custom ignore, that overrides the default ignore list. If you wanted to also ignore those, you would need to manually specify them in addition to what you wanted to ignore.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

garawalid picture garawalid  路  4Comments

Behoston picture Behoston  路  6Comments

willhardy picture willhardy  路  5Comments

asergi picture asergi  路  9Comments

gvanrossum picture gvanrossum  路  7Comments