-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Do not require built-in sections to be present in section-order
#7868
Comments
Ran into this as well, isort supports a sections = "FUTURE,THIRDPARTY,FIRSTPARTY,LOCALFOLDER"
default_section = "THIRDPARTY" The end goal is that stdlib and third party are combined, as it's common in the libraries that I work on that we are using bw-compat shims etc and it's irritating to do treat things like |
Looks like something along similar lines was just merged #8657. It would've been possible to implement
|
I've implemented |
## Summary This fixes #7868. Support isort's `default-section` feature which allows any imports that match sections that are not in `section-order` to be mapped to a specifically named section. https://pycqa.github.io/isort/docs/configuration/options.html#default-section This has a few implications: - It is no longer required that all known sections are defined in `section-order`. - This is technically a bw-incompat change because currently if folks define custom groups, and do not define a `section-order`, the code used to add all known sections to `section-order` while emitting warnings. **However, when this happened, users would be seeing warnings so I do not think it should count as a bw-incompat change.** ## Test Plan - Added a new test. - Did not break any existing tests. Finally, I ran the following config against Pyramid's complex codebase that was previously using isort and this change worked there. ### pyramid's previous isort config https://github.com/Pylons/pyramid/blob/5f7e286b0629b0a5f1225fe51172cba77eb8fda1/pyproject.toml#L22-L37 ```toml [tool.isort] profile = "black" multi_line_output = 3 src_paths = ["src", "tests"] skip_glob = ["docs/*"] include_trailing_comma = true force_grid_wrap = false combine_as_imports = true line_length = 79 force_sort_within_sections = true no_lines_before = "THIRDPARTY" sections = "FUTURE,THIRDPARTY,FIRSTPARTY,LOCALFOLDER" default_section = "THIRDPARTY" known_first_party = "pyramid" ``` ### tested with ruff isort config ```toml [tool.ruff.lint.isort] case-sensitive = true combine-as-imports = true force-sort-within-sections = true section-order = [ "future", "third-party", "first-party", "local-folder", ] default-section = "third-party" known-first-party = [ "pyramid", ] ```
## Summary This fixes astral-sh#7868. Support isort's `default-section` feature which allows any imports that match sections that are not in `section-order` to be mapped to a specifically named section. https://pycqa.github.io/isort/docs/configuration/options.html#default-section This has a few implications: - It is no longer required that all known sections are defined in `section-order`. - This is technically a bw-incompat change because currently if folks define custom groups, and do not define a `section-order`, the code used to add all known sections to `section-order` while emitting warnings. **However, when this happened, users would be seeing warnings so I do not think it should count as a bw-incompat change.** ## Test Plan - Added a new test. - Did not break any existing tests. Finally, I ran the following config against Pyramid's complex codebase that was previously using isort and this change worked there. ### pyramid's previous isort config https://github.com/Pylons/pyramid/blob/5f7e286b0629b0a5f1225fe51172cba77eb8fda1/pyproject.toml#L22-L37 ```toml [tool.isort] profile = "black" multi_line_output = 3 src_paths = ["src", "tests"] skip_glob = ["docs/*"] include_trailing_comma = true force_grid_wrap = false combine_as_imports = true line_length = 79 force_sort_within_sections = true no_lines_before = "THIRDPARTY" sections = "FUTURE,THIRDPARTY,FIRSTPARTY,LOCALFOLDER" default_section = "THIRDPARTY" known_first_party = "pyramid" ``` ### tested with ruff isort config ```toml [tool.ruff.lint.isort] case-sensitive = true combine-as-imports = true force-sort-within-sections = true section-order = [ "future", "third-party", "first-party", "local-folder", ] default-section = "third-party" known-first-party = [ "pyramid", ] ```
I'm currently in the process of migrating from flake8 and isort to Ruff.
Something I've noticed is that when leaving out a built-in section from
section-order
, a warning appears and these sections are appended to the end ofsection-order
. With isort imports that correspond to a built-in section that is missing fromsection-order
, are assigned to thethird-party
section instead. I used this before to combinestandard-library
andthird-party
sections into a single one as I don't see much value in separating these.As a workaround I've added all packages from https://github.com/astral-sh/ruff/blob/main/crates/ruff_python_stdlib/src/sys.rs to
known-third-party
but it's rather ugly.It would be great if the behavior could match
isort
or could be configured to match it somehow.The text was updated successfully, but these errors were encountered: