Skip to content

Commit

Permalink
feat(isort): Implement known-local-folder
Browse files Browse the repository at this point in the history
  • Loading branch information
spaceone committed Feb 8, 2023
1 parent 2a0927a commit efce053
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 9 deletions.
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3327,7 +3327,7 @@ known to Ruff in advance.

```toml
[tool.ruff.isort]
extra-standard-library = ["path"]
known-local-folder = ["path"]
```

---
Expand Down Expand Up @@ -3437,6 +3437,25 @@ known-first-party = ["src"]

---

#### [`known-local-folder`](#known-local-folder)

A list of modules to consider being a local folder.
Generally, this is reserved for relative
imports (from . import module).

**Default value**: `[]`

**Type**: `list[str]`

**Example usage**:

```toml
[tool.ruff.isort]
extra-standard-library = ["path"]
```

---

#### [`known-third-party`](#known-third-party)

A list of modules to consider third-party, regardless of whether they
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,18 @@ pub fn typing_only_runtime_import(
package,
&settings.isort.known_first_party,
&settings.isort.known_third_party,
&settings.isort.known_local_folder,
&settings.isort.extra_standard_library,
settings.target_version,
) {
ImportType::LocalFolder | ImportType::FirstParty => Some(Diagnostic::new(
TypingOnlyFirstPartyImport {
full_name: full_name.to_string(),
},
binding.range,
)),
ImportType::Anything | ImportType::LocalFolder | ImportType::FirstParty => {
Some(Diagnostic::new(
TypingOnlyFirstPartyImport {
full_name: full_name.to_string(),
},
binding.range,
))
}
ImportType::ThirdParty => Some(Diagnostic::new(
TypingOnlyThirdPartyImport {
full_name: full_name.to_string(),
Expand Down
13 changes: 12 additions & 1 deletion crates/ruff/src/rules/isort/categorize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ pub enum ImportType {
ThirdParty,
FirstParty,
LocalFolder,
Anything,
}

#[derive(Debug)]
enum Reason<'a> {
NonZeroLevel,
KnownFirstParty,
KnownThirdParty,
KnownLocalFolder,
ExtraStandardLibrary,
Future,
KnownStandardLibrary,
Expand All @@ -43,16 +45,19 @@ pub fn categorize(
package: Option<&Path>,
known_first_party: &BTreeSet<String>,
known_third_party: &BTreeSet<String>,
known_local_folder: &BTreeSet<String>,
extra_standard_library: &BTreeSet<String>,
target_version: PythonVersion,
) -> ImportType {
let (import_type, reason) = {
if level.map_or(false, |level| *level > 0) {
(ImportType::LocalFolder, Reason::NonZeroLevel)
(ImportType::Anything, Reason::NonZeroLevel)
} else if known_first_party.contains(module_base) {
(ImportType::FirstParty, Reason::KnownFirstParty)
} else if known_third_party.contains(module_base) {
(ImportType::ThirdParty, Reason::KnownThirdParty)
} else if known_local_folder.contains(module_base) {
(ImportType::LocalFolder, Reason::KnownLocalFolder)
} else if extra_standard_library.contains(module_base) {
(ImportType::StandardLibrary, Reason::ExtraStandardLibrary)
} else if module_base == "__future__" {
Expand Down Expand Up @@ -98,12 +103,14 @@ fn match_sources<'a>(paths: &'a [PathBuf], base: &str) -> Option<&'a Path> {
None
}

#[allow(clippy::too_many_arguments)]
pub fn categorize_imports<'a>(
block: ImportBlock<'a>,
src: &[PathBuf],
package: Option<&Path>,
known_first_party: &BTreeSet<String>,
known_third_party: &BTreeSet<String>,
known_local_folder: &BTreeSet<String>,
extra_standard_library: &BTreeSet<String>,
target_version: PythonVersion,
) -> BTreeMap<ImportType, ImportBlock<'a>> {
Expand All @@ -117,6 +124,7 @@ pub fn categorize_imports<'a>(
package,
known_first_party,
known_third_party,
known_local_folder,
extra_standard_library,
target_version,
);
Expand All @@ -135,6 +143,7 @@ pub fn categorize_imports<'a>(
package,
known_first_party,
known_third_party,
known_local_folder,
extra_standard_library,
target_version,
);
Expand All @@ -153,6 +162,7 @@ pub fn categorize_imports<'a>(
package,
known_first_party,
known_third_party,
known_local_folder,
extra_standard_library,
target_version,
);
Expand All @@ -171,6 +181,7 @@ pub fn categorize_imports<'a>(
package,
known_first_party,
known_third_party,
known_local_folder,
extra_standard_library,
target_version,
);
Expand Down
5 changes: 5 additions & 0 deletions crates/ruff/src/rules/isort/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ pub fn format_imports(
force_wrap_aliases: bool,
known_first_party: &BTreeSet<String>,
known_third_party: &BTreeSet<String>,
known_local_folder: &BTreeSet<String>,
order_by_type: bool,
relative_imports_order: RelativeImportsOrder,
single_line_exclusions: &BTreeSet<String>,
Expand Down Expand Up @@ -155,6 +156,7 @@ pub fn format_imports(
force_wrap_aliases,
known_first_party,
known_third_party,
known_local_folder,
order_by_type,
relative_imports_order,
single_line_exclusions,
Expand Down Expand Up @@ -212,6 +214,7 @@ fn format_import_block(
force_wrap_aliases: bool,
known_first_party: &BTreeSet<String>,
known_third_party: &BTreeSet<String>,
known_local_folder: &BTreeSet<String>,
order_by_type: bool,
relative_imports_order: RelativeImportsOrder,
single_line_exclusions: &BTreeSet<String>,
Expand All @@ -229,6 +232,7 @@ fn format_import_block(
package,
known_first_party,
known_third_party,
known_local_folder,
extra_standard_library,
target_version,
);
Expand Down Expand Up @@ -709,6 +713,7 @@ mod tests {
ImportType::ThirdParty,
ImportType::FirstParty,
ImportType::LocalFolder,
ImportType::Anything,
]),
..super::settings::Settings::default()
},
Expand Down
1 change: 1 addition & 0 deletions crates/ruff/src/rules/isort/rules/organize_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ pub fn organize_imports(
settings.isort.force_wrap_aliases,
&settings.isort.known_first_party,
&settings.isort.known_third_party,
&settings.isort.known_local_folder,
settings.isort.order_by_type,
settings.isort.relative_imports_order,
&settings.isort.single_line_exclusions,
Expand Down
15 changes: 15 additions & 0 deletions crates/ruff/src/rules/isort/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,17 @@ pub struct Options {
extra-standard-library = ["path"]
"#
)]
/// A list of modules to consider being a local folder.
/// Generally, this is reserved for relative
/// imports (from . import module).
pub known_local_folder: Option<Vec<String>>,
#[option(
default = r#"[]"#,
value_type = "list[str]",
example = r#"
known-local-folder = ["path"]
"#
)]
/// A list of modules to consider standard-library, in addition to those
/// known to Ruff in advance.
pub extra_standard_library: Option<Vec<String>>,
Expand Down Expand Up @@ -247,6 +258,7 @@ pub struct Settings {
pub force_wrap_aliases: bool,
pub known_first_party: BTreeSet<String>,
pub known_third_party: BTreeSet<String>,
pub known_local_folder: BTreeSet<String>,
pub order_by_type: bool,
pub relative_imports_order: RelativeImportsOrder,
pub single_line_exclusions: BTreeSet<String>,
Expand All @@ -270,6 +282,7 @@ impl Default for Settings {
force_wrap_aliases: false,
known_first_party: BTreeSet::new(),
known_third_party: BTreeSet::new(),
known_local_folder: BTreeSet::new(),
order_by_type: true,
relative_imports_order: RelativeImportsOrder::default(),
single_line_exclusions: BTreeSet::new(),
Expand Down Expand Up @@ -297,6 +310,7 @@ impl From<Options> for Settings {
force_wrap_aliases: options.force_wrap_aliases.unwrap_or(false),
known_first_party: BTreeSet::from_iter(options.known_first_party.unwrap_or_default()),
known_third_party: BTreeSet::from_iter(options.known_third_party.unwrap_or_default()),
known_local_folder: BTreeSet::from_iter(options.known_local_folder.unwrap_or_default()),
order_by_type: options.order_by_type.unwrap_or(true),
relative_imports_order: options.relative_imports_order.unwrap_or_default(),
single_line_exclusions: BTreeSet::from_iter(
Expand Down Expand Up @@ -324,6 +338,7 @@ impl From<Settings> for Options {
force_wrap_aliases: Some(settings.force_wrap_aliases),
known_first_party: Some(settings.known_first_party.into_iter().collect()),
known_third_party: Some(settings.known_third_party.into_iter().collect()),
known_local_folder: Some(settings.known_local_folder.into_iter().collect()),
order_by_type: Some(settings.order_by_type),
relative_imports_order: Some(settings.relative_imports_order),
single_line_exclusions: Some(settings.single_line_exclusions.into_iter().collect()),
Expand Down
13 changes: 12 additions & 1 deletion ruff.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,8 @@
"standard-library",
"third-party",
"first-party",
"local-folder"
"local-folder",
"anything"
]
},
"IsortOptions": {
Expand Down Expand Up @@ -964,6 +965,16 @@
"type": "string"
}
},
"known-local-folder": {
"description": "A list of modules to consider being a local folder. Generally, this is reserved for relative imports (from . import module).",
"type": [
"array",
"null"
],
"items": {
"type": "string"
}
},
"known-third-party": {
"description": "A list of modules to consider third-party, regardless of whether they can be identified as such via introspection of the local filesystem.",
"type": [
Expand Down

0 comments on commit efce053

Please sign in to comment.