isort: Support disabling sections with `no-sections = true` (#8657)

## Summary

This adds a ``no-sections`` option for isort in the linter, similar to
the ``no_sections`` option that exists in upstream isort
(https://pycqa.github.io/isort/docs/configuration/options.html#no-sections)

This option puts all imports except for ``__future__`` into the same
section, and is mostly used by monorepos.

I've taken a bit of a leap in assuming that ruff wants to support the
exact same option; more than happy to refactor if you'd prefer a
different way of setting this up.

Fixes #8653

## Test Plan

I've added a test and have run it on a large Python codebase that uses
isort with --no-sections. The option is disabled by default.
This commit is contained in:
Jelmer Vernooij 2023-11-14 21:45:51 +00:00 committed by GitHub
parent 561277925f
commit 9d76e4e0b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 93 additions and 3 deletions

View file

@ -1978,6 +1978,33 @@ pub struct IsortOptions {
)]
pub section_order: Option<Vec<ImportSection>>,
/// Put all imports into the same section bucket.
///
/// For example, rather than separating standard library and third-party imports, as in:
/// ```python
/// import os
/// import sys
///
/// import numpy
/// import pandas
/// ```
///
/// Setting `no-sections = true` will instead group all imports into a single section:
/// ```python
/// import os
/// import numpy
/// import pandas
/// import sys
/// ```
#[option(
default = r#"false"#,
value_type = "bool",
example = r#"
no-sections = true
"#
)]
pub no_sections: Option<bool>,
/// Whether to automatically mark imports from within the same package as first-party.
/// For example, when `detect-same-package = true`, then when analyzing files within the
/// `foo` package, any imports from within the `foo` package will be considered first-party.
@ -2013,6 +2040,15 @@ impl IsortOptions {
pub fn try_into_settings(
self,
) -> Result<isort::settings::Settings, isort::settings::SettingsError> {
// Verify that if `no_sections` is set, then `section_order` is empty.
let no_sections = self.no_sections.unwrap_or_default();
if no_sections && self.section_order.is_some() {
warn_user_once!("`section-order` is ignored when `no-sections` is set to `true`");
}
if no_sections && self.sections.is_some() {
warn_user_once!("`sections` is ignored when `no-sections` is set to `true`");
}
// Extract any configuration options that deal with user-defined sections.
let mut section_order: Vec<_> = self
.section_order
@ -2169,6 +2205,7 @@ impl IsortOptions {
lines_between_types: self.lines_between_types.unwrap_or_default(),
forced_separate: Vec::from_iter(self.forced_separate.unwrap_or_default()),
section_order,
no_sections,
})
}
}