Skip to content

Enforce black style for all commits

john lee requested to merge leej3/PasswordSafe:blacken_all_commits into master

This follows on from !103 (closed). The darker tool seems like a nice way of implementing a gradual migration to blackened code. I have implemented a git hook to use darker to clean up staged changes prior to commit.

In order to activate this behavior, a developer must set git config to use hooks from .githooks:

git config core.hooksPath .githooks

CI now fails if darker indicates that the diff with master is not compliant with black.

Some points to note:

  • darker can run linters as part of it's operation. It might be worth exploring merging the static analysis steps.
  • Currently for any files that are in the staging area for a commit if they have unstaged changes they are ignored. It is conceivable that this behavior could be improved (intelligently leave the unstaged changes untouched but fix the staged changes). This is more work and I don't have a sense of how robust it would be (diff hunks move around as the line numbers change during formatting so a little fuzzy matching of the changes would be required to determine correspondence). Happy to explore it but I'd like to know this is working for people and that the improvement would be worth the effort.
  • The githook is python3.6+ and assumes python3 in the current environment is the appropriate python interpreter to use (darker would need to be installed into this interpreter's site packages). I assume that is fine.

Merge request reports