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

Are isort configurations able to be skipped or just applicable in some files? #7890

Closed
BT-rmartin opened this issue Oct 10, 2023 · 6 comments

Comments

@BT-rmartin
Copy link

Hi, I would like to know if there is a way to skip the application of some isort checks for some specific files, or the other way round, to enforce the application of some isort configurations for specific files.

Here you have an example.
In general, I would like to have something like this in the files gathering imports
image

However for the __init__py file, I would prefer the single line imports, but it's changed to gather imports
image

If I configure it like this, it keeps the format of init.py but splits the other imports of the other files.
[tool.ruff.isort]
force-single-line = true

So I wonder if there is a way to apply different isort configurations for different files.

If this option is not available, it would be super great if it could be added in the near future.
Something similar to ignore some checks in particular files, but for isort configurations.
[tool.ruff.per-file-ignores]
"init.py" = ["F401"]
"manifest.py" = ["B018"]
"**/tests/*.py" = ["S101"]

Thanks for the help in advance

@charliermarsh
Copy link
Member

We don't support this kind of file-scoped configuration. The general request has come up a few times (e.g., #7696).

That being said... if you use an as alias, Ruff will avoid merging them:

from . import account_account as account_account

Ruff will also avoiding marking them as unused in this case. I believe this pattern is quite common -- Pyright treats these redundant aliases as public exports: https://github.com/python/typing/blob/master/docs/source/libraries.rst#library-interface-public-and-private-symbols.

@BT-rmartin
Copy link
Author

BT-rmartin commented Oct 11, 2023

Thanks for the quick answer @charliermarsh
I'm not so interested in using such aliases.
Any plan to do that file-scope config in the near future or is it discarded?
Any other potential workaround?

@charliermarsh
Copy link
Member

Okay, got it. I do consider it a valid solution as it's the recommended mechanism in the typing community for marking imported symbols as public rather than private (and so if you're re-importing things in an __init__.py file for the purpose of re-exporting them, and you want re-exports to be formatted differently than other imports as above, it seems like an appropriate pattern to use). But hopefully we will support file-scoped configuration at some point. I expect that we will support it, though I doubt it will be within the next month or two.

I'm going to merge into #7696 and make that issue more general to track the file-scoped configuration request.

@BT-rmartin
Copy link
Author

BT-rmartin commented Oct 26, 2023

We don't support this kind of file-scoped configuration. The general request has come up a few times (e.g., #7696).

That being said... if you use an as alias, Ruff will avoid merging them:

from . import account_account as account_account

Ruff will also avoiding marking them as unused in this case. I believe this pattern is quite common -- Pyright treats these redundant aliases as public exports: https://github.com/python/typing/blob/master/docs/source/libraries.rst#library-interface-public-and-private-symbols.

@charliermarsh I was by the way trying this approach and it continues being replaced
For instance, from
from . import test_crm_activity_report as test_crm_activity_report
from . import test_crm_lead as test_crm_lead
from . import test_disable_records as test_disable_records

into
from . import test_crm_activity_report, test_crm_lead, test_disable_records

when running ruff check . --fix

Any other solution?

@dhruvmanila
Copy link
Member

Hey, can you provide us with the config being used for this? I'm unable to reproduce this locally with v0.1.3. If I use aliases, then it keeps the imports separated and it'll only merge when I remove the aliases.

@BT-rmartin
Copy link
Author

@dhruvmanila

Here it is. Maybe it was a version issue. I had ruff 0.0.278. Now with 0.1.3 it's respected but the imports are reordered

My configuration was like this

[tool.ruff]
select = ["E", "F", "I", "PL", "B", "Q", "D", "N", "S", "G", "RUF", "W"]
ignore = ["D100", "D101", "D104", "D213", "D203", "RUF012"]
exclude = ["**/ext/"]

line-length = 115

[tool.ruff.per-file-ignores]
"init.py" = ["F401"]
"manifest.py" = ["B018"]
"**/tests/*.py" = ["S101"]

[tool.ruff.flake8-quotes]
docstring-quotes = "double"
inline-quotes = "single"
multiline-quotes = "double"
avoid-escape = false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants