black
black copied to clipboard
Black ignores # fmt: skip if run via pre-commit
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 --
You're probably running the wrong version of Black somewhere.
You're probably running the wrong version of Black somewhere.
Can't imagine where to look.
Could you try to set "required-version" in your pyproject to e.g. 22.10? That would make sure it's not the issue.
Running black with --version flag results in 0.1.dev1+g27d2014, that's g-tag matches 22.10 tag

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.
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)