Skip to content

Behavior of # fmt: skip in the presence of other comments/pragmas #3330

@e-gebes

Description

@e-gebes

I have this code:

def function():
    from . import something_long_long_long_long  # pylint: disable=import-outside-toplevel

black formats it to:

def function():
    from . import (
        something_long_long_long_long,
    )  # pylint: disable=import-outside-toplevel

This breaks the Pylint pragma, it does not work anymore on the line where black has put it. So, I played around with the pylint and black pragmas to get it working:

def function():
    # option 1: move the pylint pragma after black did its work
    from . import (  # pylint: disable=import-outside-toplevel
        something_long_long_long_long,
    )

    # option 2: try fmt: skip at the end of the line
    from . import something_long_long_long_long  # pylint: disable=import-outside-toplevel  # fmt: skip

    # option 3: try fmt: skip at the beginning of the comment
    from . import something_long_long_long_long  # fmt: skip  # pylint: disable=import-outside-toplevel

    # option 4: use the off/on pragmas
    # fmt: off
    from . import something_long_long_long_long  # pylint: disable=import-outside-toplevel
    # fmt: on

All those 4 options are fine for Pylint. Options 1 and 4 are fine for Black. Black ignores the # fmt: skip pragmas in options 2 & 3.
Option 4 is my current workaround, though it's not quite nice having to use two pragmas on additional lines.

In my opinion, black should at least work with the skip pragmas as in option 2 (at the end of the line). Maybe also option 3 (the first part of the comment).
In the current documentation there is this wording:

[Black] doesn’t reformat lines that end with # fmt: skip

This suggests that option 2 should work, but it doesn't. The documentation should be made clearer, especially if this issue would not be fixed.

An idea is also to exclude Pylint pragmas (and possibly pragmas from other tools) from calculating the line length in black. Pylint does this with its own pragmas (there is also a Pylint line length check), which makes sense because pragmas are usually not something that one needs to read very often, hence it's kinda acceptable to exceed the line limit.

(Related to #3329, which is a matter of style, this here is about interoperability with other tools.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    F: fmtskipfmt: skip implementation

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions