black icon indicating copy to clipboard operation
black copied to clipboard

Black ignores # fmt: skip if run via pre-commit

Open viivanofu opened this issue 3 years ago • 4 comments

Describe the bug

When I run Black as a commit hook via pre-commit it ignores # fmt: skip on a long lines. I afraid that it may ignore # fmt: skip pragmas totally, making Black unusable for me as a pre-commit tool.

To Reproduce

have a pre-commit with .pre-commit-config.yaml

# Pre-commit config, see: https://pre-commit.com/
repos:
  - repo: https://github.com/psf/black
    rev: '22.10.0'
    hooks:
      - id: black

install hooks with $ pre-commit --install

have a really long line with # fmt: skip in the end

a = '<200 characters in my case>' # fmt: skip  OR # fmt: pass

OR

# fmt: off
a = '<200 characters in my case>'   # noqa
# fmt: on

Run pre-commit manually $ pre-commit --all-files and it works! No formatting.

Have this long line as a git diff, try to commit it

$ git add *
$ git commit -m 'test'

The resulting error is:

black....................................................................Failed
- hook id: black
- files were modified by this hook

And comment after long line moved to next line

a = '<200 characters in my case>' 
# fmt: skip  OR # fmt: pass
# fmt: off
a = '<200 characters in my case>'  
# noqa
# fmt: on

Expected behavior

Pragma comment left on the same line. No formatting on #fmt: skip, pass, off/on lines

Environment

  • Black's version: 22.10
  • OS and Python version: [macOS/12.5, Python3.10.8]

Additional context

I use the last version of pre-commit. I use PyCharm to run git commit. Exact command is:

git -c credential.helper= -c core.quotepath=false -c log.showSignature=false add --ignore-errors -A -f -- src/FILE_NAME.py git -c credential.helper= -c core.quotepath=false -c log.showSignature=false commit -F /private/var/folders/20/SOME_HASH/T/git-commit-msg-.txt --

viivanofu avatar Oct 21 '22 01:10 viivanofu

You're probably running the wrong version of Black somewhere.

JelleZijlstra avatar Oct 21 '22 02:10 JelleZijlstra

You're probably running the wrong version of Black somewhere.

Can't imagine where to look.

viivanofu avatar Oct 21 '22 02:10 viivanofu

Could you try to set "required-version" in your pyproject to e.g. 22.10? That would make sure it's not the issue.

felix-hilden avatar Oct 21 '22 12:10 felix-hilden

Running black with --version flag results in 0.1.dev1+g27d2014, that's g-tag matches 22.10 tag image

adding --required-version=0.1.dev1+g27d2014 to pre-commit config as a black arg, runs black as usual (so the version is correct). The problem still persists.

viivanofu avatar Oct 24 '22 01:10 viivanofu

Closing since there isn't a clear repro and because this is almost certainly an incorrect version being used somewhere. Note that using https://github.com/psf/black-pre-commit-mirror will make required version work as expected (and also make Black 2x faster)

hauntsaninja avatar Aug 04 '23 01:08 hauntsaninja