4

I am trying to use isort to automatically fix import orders. When I run pre-commit run --files=myfile.py it correctly updates my imports (by adding a line between known third party imports and internal package imports). However, when I try to commit this, pre-commit will cause the commit to fail, and then remove the line between the third-party imports and internal packages.

I have tried setting the known_third_party setting in both setup.cfg and in .isort.cfg and I get the same behaviour. However, I suspect the config itself is not the problem as it is clearly being recognised correctly when I run pre-commit run .... So why does pre-commit run use the correct config, but on the actual commit, it seems to ignore it?

How can I get the hooks to respect the config on the actual commit?

The configuration is: .pre-commit-config.yaml

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
  rev: v3.2.0
  hooks:
    - id: trailing-whitespace
    - id: end-of-file-fixer
    - id: check-yaml
    - id: check-added-large-files
- repo: https://github.com/psf/black
  rev: 21.12b0
  hooks:
    - id: black
- repo: https://github.com/pycqa/isort
  rev: 5.10.1
  hooks:
    - id: isort
      name: isort (python)

.isort.cfg

[settings]
known_third_party=django

Additional Info: Sub-directory configs may have caused this behaviour

I have tried moving config files, and now the problem seems to be resolved. My repo consists of source code for multiple applications, one of which is a python django application. The config files were in this sub-directory. I have moved the config files to the root of the whole project and now this particular problem is resolved. I wonder if there is a way to get pre-commit or isort to recognise the configs in sub-directories but I will leave that for another question.

cammil
  • 9,499
  • 15
  • 55
  • 89
  • try re-running `pre-commit install` to make sure you have the same version running as on your command line? show your configuration? – anthony sottile Apr 12 '22 at 12:59
  • I had run pre-commit install several times. I have tried again for good measure but still doesn't work. I have added my configs to the question. – cammil Apr 12 '22 at 13:15
  • I can't reproduce, can you show your python file and pre-commit version – anthony sottile Apr 12 '22 at 13:25
  • and show the output of your commands – anthony sottile Apr 12 '22 at 13:26
  • Just to be clear, after pre-commit runs, is the isorted file in your sandbox, just not added, or is the file not even modified in your sandbox? – joanis Apr 12 '22 at 15:10
  • Having tried to create a simple and reproducible project, I moved some config files (see notes added to question) and this seems to have resolved the current issue. Thanks for the help everyone. – cammil Apr 13 '22 at 08:31

0 Answers0