Black: Black does not create pylint-compatible code

Created on 31 Oct 2019  路  6Comments  路  Source: psf/black

Pylint error code: C0330 (bad-continuation; i.e. "Wrong hanging indentation before block")

Original code snippet

class CheckHandler(object):
    def __init__(self, file, out_json, check_dir=u".", files=None):  # type: (str, bool, str, typing.List[str]) -> None
        # Do this here so setup.py doesn't error
        from snekchek.baseconfig import config
        import configobj

Examples in the current _Black_ style

class CheckHandler(object):
    def __init__(
        self, file, out_json, check_dir=u".", files=None
    ):  # type: (str, bool, str, typing.List[str]) -> None
        # Do this here so setup.py doesn't error
        from snekchek.baseconfig import config
        import configobj

Note how the function parameters are on the same indentation level as the function body.

Desired style

# Ideal: comment moved down to fit width
# Note that getting this to work while preserving pylint/flake8 markers is quite the challenge
class CheckHandler(object):
    def __init__(self, file, out_json, check_dir=u".", files=None):  
        # type: (str, bool, str, typing.List[str]) -> None
        # Do this here so setup.py doesn't error
        from snekchek.baseconfig import config
        import configobj

# Alternatively, indent as recommended by pylint
class CheckHandler(object):
    def __init__(
            self, file, out_json, check_dir=u".", files=None
    ):  # type: (str, bool, str, typing.List[str]) -> None
        # Do this here so setup.py doesn't error
        from snekchek.baseconfig import config
        import configobj

It does this in various situations, but the core of the issue stays the same; The newer line ends up on the same indentation level as the function body

design

Most helpful comment

I want to be sure I understand.

For our convenience, I identify

def f1_(
    arg1, ...
# four-space indent

as Style A and

def f1_(
        arg1, ...
# eight-space indent

as style B.

When vianney-g writes, "This breaks the Black's contract", I _think_ vianney-g is saying that:

  • Black enforces Style A;
  • PEP8 prescribes Style B;
  • Style A and Style B are _not_ consistent, and, in particular, Style A canNOT be viewed "... as a strict subset ..." of Style B; and therefore
  • Black appears not to uphold its contract in this one regard.

I welcome any correction in my understanding.

All 6 comments

Black is unlikely to change such a core aspect of its formatting style at this stage. I recommend that you turn off any formatting-related errors from linters if you run Black on your codebase.

Is it possible to add a flag in the config to disable/change this behavior?

Sorry, no. As explained in the README, Black is striving for a single codestyle with minimal configuration.

If I understand correctly, Black has chosen to diverge from PEP8 on this point, because the latter emphasizes "... an extra level of indentation ..." for function arguments.

While I naively prefer Black's choice to that in PEP8, I ask confirmation from those who've studied this more: do Black and PEP8 make different choices for indentation of function arguments?

This breaks the Black's contract:

The coding style used by Black can be viewed as a strict subset of PEP 8.
README.md

I want to be sure I understand.

For our convenience, I identify

def f1_(
    arg1, ...
# four-space indent

as Style A and

def f1_(
        arg1, ...
# eight-space indent

as style B.

When vianney-g writes, "This breaks the Black's contract", I _think_ vianney-g is saying that:

  • Black enforces Style A;
  • PEP8 prescribes Style B;
  • Style A and Style B are _not_ consistent, and, in particular, Style A canNOT be viewed "... as a strict subset ..." of Style B; and therefore
  • Black appears not to uphold its contract in this one regard.

I welcome any correction in my understanding.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kindjacket picture kindjacket  路  21Comments

spapanik picture spapanik  路  23Comments

altendky picture altendky  路  41Comments

odormond picture odormond  路  21Comments

jonadaly picture jonadaly  路  23Comments