PyCQA / isort

A Python utility / library to sort imports.
https://pycqa.github.io/isort/
MIT License
6.5k stars 580 forks source link

`isort` ignores config files like `pyproject.toml` when explicitly passing files #1872

Open lucaslie opened 2 years ago

lucaslie commented 2 years ago

I noticed that isort ignores config files as soon as any arguments are passed in on the command line.

Is this really the desired behavior? Especially when isort is called on a specific file only it is very counterintuitive that config files like pyproject.toml are ignored, i.e.,

isort <some-folder>/some_file.py

will ignore my toml file at ./pyproject.toml while

isort

will load the configuration from the toml file.

I have noticed this discrepancy while using isort with the pre-commit framework. Depending on how pre-commit is called it might actually call the tools individually on each file, e.g., when called via pre-commit run --from-ref main --to-ref HEAD. In this case isort won't load the configuration.

This is unlike most other linting/quality check tools like flake8, black, or pylint that will still load the configuration files.

And while I can still explicitly pass in the config file via

isort --sp pyproject.toml <some-folder>/some_file.py

it's still quite unintuitive and should at least be documented.

timothycrosley commented 2 years ago

This is not isort's behavior intended behavior, and would represent a bug. isort is coded and tested to behave in this way the same as all other linting / formatting tools you mention. Can you provide some more information about what you are seeing?

timothycrosley commented 2 years ago

In particular, if you could demonstrate in a repo or docker machine exactly the behavior you are seeing in a reproducible way. The only way isort wouldn't pick up a config file, in the given examples by design, is if the settings file lives outside of either a repository that the source file is in, or does not live in a parent directory of the file (unlike the given example) which is the same behavior exhibited by these other tools.

Is there any chance you are using an older version of isort?

pawel-ch commented 2 years ago

In my tests isort ignores pyproject.toml regardless the files being passed.

Example pyproject.toml:

[tool.poetry]
name = "isort_test"
version = "0.1.0"
description = ""
authors = ["Python Developer <developer@example.com>"]

[tool.poetry.dependencies]
python = "^3.8"

[tool.poetry.dev-dependencies]
isort = "^5.10.1"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

[tool.isort]
line_length = 120
profile = "black"
reverse_relative = 1
src_paths = ["."]

bash

poetry install
poetry run isort . --show-config | less  # search for line_length and you will find 79

This also causes false positives, when using pre-commit:

.pre-commit-config.yaml

exclude: 'docs|node_modules|.git|.tox'
default_stages: [commit]
fail_fast: true

repos:
  - repo: https://github.com/timothycrosley/isort
    rev: 5.10.1
    hooks:
      - id: isort

bash

pre-commit run --all-files
zeshuaro commented 2 years ago

Hey @timothycrosley, I recently ran into this same issue. So I created this repository to reproduce the issue: https://github.com/zeshuaro/isort-exclude-demo

In the repository, file2.py contains unsorted imports. But in pyproject.toml, we explicitly set it for isort to ignore file2.py. And so isort should ignore it.

If you try to run pre-commit run --all-files (which has been configured to run isort), isort will report an error in file2.py. This is most likely due to pre-commit directly passing the file to isort, and isort doesn't ignore the file in this case.

Black had the exact same issue, and the workarounds are listed in this comment: https://github.com/psf/black/issues/1584#issuecomment-672124752. At the end, they added a --force-exclude argument to address the issue.

Would it be possible for isort to add the same function?

vanaoff commented 2 years ago

We are experiencing same issue in pre-commit hook. Maybe this could be helpful - I believe the issue occurs just when using python>3.8.

pierre-pvln commented 2 years ago

I get the following error when isort is run from windows commandline. isort .

UserWarning: Failed to pull configuration information

When I add a print statement in the code \lib\site-packages\isort\settings.py it shows that it reads the info from the pyproject.toml file.

running python=3.9.12 and isort=5.10.1

my pyproject.toml file.

#
# SOME COMMENT
#

[tool.isort]
reverse_relative = 1
src_paths = ["."]
verbose = true

multi_line_output = 3
include_trailing_comma = true
force_grid_wrap = 0  
use_parentheses = true
ensure_newline_before_comments = true
lines_after_imports = 2

git_ignore = true
comment_prefix = '# isort|'
force_single_line = true

[tool.black]

[tool.interrogate]
# https://interrogate.readthedocs.io/en/latest/#configuration
verbose = 2
exclude = ['docs','build','history']
John98Zakaria commented 1 year ago

I have the same issue. So I tried to create a .isort.cfg file to see whether it would use its own format. And It sure did.

dariocurr commented 1 year ago

I experienced the same error while dealing with pre-commit and isort configuration in pyproject.toml

Ozaq commented 1 year ago

I am running into the same issue :(

Some one from the maintainers should remove the https://github.com/PyCQA/isort/labels/repo_needed label. @zeshuaro provided a reproducer.