Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lucaslie opened this issue Dec 23, 2021 · 9 comments
Labels
question Further information is requested repo_needed Can't currently reproduce, if reproduction steps are added we will resivit this issue.

Comments

@lucaslie
Copy link

lucaslie commented Dec 23, 2021

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
Copy link
Member

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 timothycrosley added question Further information is requested repo_needed Can't currently reproduce, if reproduction steps are added we will resivit this issue. labels Dec 24, 2021
@timothycrosley
Copy link
Member

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?

samueljsb added a commit to samueljsb/qaz that referenced this issue Mar 4, 2022
Prior to this change, pre-commit started messing up the import sorting
with isort. It appears to have een picking up the configuration from
`configfiles/.editorconfig` instead of `setup.cfg`.

It appears that this is related to PyCQA/isort#1872 and
PyCQA/isort#1907.

This change explicitly sets the settings file to ensure the right file
is used.

We also fix the incorrect imports.
samueljsb added a commit to samueljsb/qaz that referenced this issue Mar 4, 2022
Prior to this change, pre-commit started messing up the import sorting
with isort. It appears to have been picking up the configuration from
`configfiles/.editorconfig` instead of `setup.cfg`.

It appears that this is related to PyCQA/isort#1872 and
PyCQA/isort#1907.

This change explicitly sets the settings file to ensure the right file
is used.

We also fix the incorrect imports.
@pawel-ch
Copy link

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 <[email protected]>"]

[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
Copy link

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: psf/black#1584 (comment). 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
Copy link

vanaoff commented Apr 27, 2022

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
Copy link

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
Copy link

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
Copy link

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

@Ozaq
Copy link

Ozaq commented Aug 2, 2023

I am running into the same issue :(

Some one from the maintainers should remove the repo_needed Can't currently reproduce, if reproduction steps are added we will resivit this issue. label. @zeshuaro provided a reproducer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested repo_needed Can't currently reproduce, if reproduction steps are added we will resivit this issue.
Projects
None yet
Development

No branches or pull requests

9 participants