Problem with commit - black failed

I’m trying to extend time pattern trigger, so I did some code changes, tested, etc. So it’s time to commit (to my cloned repo) but I have some problem with it:

(hass-core) rufik@LAPTOP:hass-core$ git status
On branch feature/time_pattern_extended
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   homeassistant/util/dt.py
        modified:   tests/util/test_dt.py
[...]

(hass-core) rufik@LAPTOP-RUFIK:hass-core$ git commit -m "Extending time pattern trigger....."
[WARNING] Unstaged files detected.
[INFO] Stashing unstaged files to /home/rufik/.cache/pre-commit/patch1585303626.
black....................................................................Failed
- hook id: black
- files were modified by this hook
[...]
[WARNING] Stashed changes conflicted with hook auto-fixes... Rolling back fixes...
[INFO] Restored changes from /home/rufik/.cache/pre-commit/patch1585303626.

Why black formatting failed? Where can I find details about that?

black is installed as a pre-commit check. It reformats your code to a specific style so that reviewers don’t have to review things like indentation for long lines.

You can inspect the changes, which will most likely be fine, then re add them and commit them. If you’d rather it pass the first time, you can run black before committing.

But why black failed?
It fails all the time…I’m unable to commit :frowning:

It only passes if black doesn’t modify anything. You need to add the changes from black and commit with those changes included. Or you can run black before committing.

Otherwise it won’t pass the CI checks on the pull request.

Explicitly do this:

git commit # this might fail the first time

black had modified files if it failed

git add <files here>
git commit

Then it will pass

1 Like

This solved my problem, thank you :slight_smile: