**Title: Recurring issue with `black` and `blacken-docs` while contributing to Django**

Hello everyone,

I’m contributing to the Django project and I’m facing a recurring issue that puts me in a sort of loop when using black and blacken-docs via Git hooks. Here’s a summary of the situation:

  1. When I run black . to reformat files, everything seems to go well, and black reformats several files.
  2. Then, I run tox to check that everything is in order, and there appear to be no errors in the tests, including those related to black.
  3. However, when I attempt to make a commit, the black hook fails and reformats the same files again. Additionally, the blacken-docs hook also fails and modifies some documentation files.
  4. If I run tox again after this, the tests related to black fail this time, indicating that files require reformatting.

It’s important to note that the files in question are not the ones I’m currently working on. This happened after a two-month break and a code update to Django that required rebasing my branch.

I’m somewhat lost on how to resolve this issue and break out of this loop. Do you have any suggestions or advice on how to handle this situation?

Thank you in advance for your help.

the work

It sounds like you have two different versions of black installed and they’re not formatting identically.

Thanks Lily-Foote for the guidance.
but I don’t think I use more than one version of black.

(.djangodev) barhamou@barhamou-hplaptop15sfq5xxx:~/mes-projets/contribution_a_django/django$ black --version
black, 23.11.0 (compiled: yes)
Python (CPython) 3.11.6
(.djangodev) barhamou@barhamou-hplaptop15sfq5xxx:~/mes-projets/contribution_a_django/django$ pip list | grep "black"
black                         23.11.0
blacken-docs                  1.16.0
(.djangodev) barhamou@barhamou-hplaptop15sfq5xxx:~/mes-projets/contribution_a_django/django$ 

My guess would be that pre-commit is using its own private version of black, but I’m not sure how to check that.

Something you could try first is to update black - I don’t think you’re on the latest version.

ok. i’ll try that and get back to you. thanks

The version of Black is pinned in the .pre-commit-config.yaml file, currently v24.2.0: django/.pre-commit-config.yaml at main · django/django · GitHub . Black changes its code style yearly, so the 24 release differs significantly from the 23 one, changes here: Change Log - Black 24.2.0 documentation .

I would advise that you stop formatting with black manually. Instead use pre-commit only, like: pre-commit run black --all or pre-commit run black --file django/__init__.py as needed. Or you can just ignore formatting until you commit, when pre-commit will fix things for you before you commit.

thank you for your guidance.
I will look into it further.
but after updating black, the problem is reduced to that of blacken-docs.
i had to manually execute this command to pass the test:
find -name "*.txt" -not -path "./_build/*" -not -path "./_theme/*" | xargs blacken-docs --rst-literal-block;

I think my tox and pre-commit are no longer using the same version of blacken-docs.
Where can I check the black and blacken-docs versions for tox?

I suggest not attempting to run the same tool in multiple different ways. There is not much point running black and blacken-docs under tox, pre-commit, and manually as per your command line above. This will end up running the risk of using three different versions, especially as the dependencies are not pinned in tox.ini. Just use pre-commit (as Adam has said) prior to pushing your changes and it’ll avoid a lot of issues.

I have noted on the pull request that it seems that black no longer cares about the presence or absence of the two remaining lines that need to be reverted - you’ll need to revert those changes manually.

thank you for the orientation. i take note.
clarification: so if I understand correctly I can use either tox or pre-commit, no need to use both at the same time?
because i need tox to run all the tests, unless pre-commit offers the same service as tox. is that the case?