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

fix: mark __main__ as first-party import #8805

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import os

import __main__
import third_party

import first_party

os.a
third_party.a
__main__.a
first_party.a
5 changes: 5 additions & 0 deletions crates/ruff_linter/src/rules/isort/categorize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ pub(crate) fn categorize<'a>(
&ImportSection::Known(ImportType::FirstParty),
Reason::SourceMatch(src),
)
} else if matches!(level, None | Some(0)) && module_name == "__main__" {
(
&ImportSection::Known(ImportType::FirstParty),
Reason::KnownFirstParty,
)
} else {
(
&ImportSection::Known(ImportType::ThirdParty),
Expand Down
24 changes: 24 additions & 0 deletions crates/ruff_linter/src/rules/isort/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,30 @@ mod tests {
Ok(())
}

#[test_case(Path::new("main_first_party.py"))]
fn main_is_first_party(path: &Path) -> Result<()> {
let snapshot = format!("sections_{}", path.to_string_lossy());
let diagnostics = test_path(
Path::new("isort").join(path).as_path(),
&LinterSettings {
src: vec![test_resource_path("fixtures/isort")],
isort: super::settings::Settings {
known_modules: KnownModules::new(
vec![pattern("first_party")],
vec![],
vec![],
vec![],
FxHashMap::default(),
),
..super::settings::Settings::default()
},
..LinterSettings::for_rule(Rule::UnsortedImports)
},
)?;
assert_messages!(snapshot, diagnostics);
Ok(())
}

#[test]
fn detect_same_package() -> Result<()> {
let diagnostics = test_path(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
source: crates/ruff_linter/src/rules/isort/mod.rs
---
main_first_party.py:1:1: I001 [*] Import block is un-sorted or un-formatted
|
1 | / import os
2 | |
3 | | import __main__
4 | | import third_party
5 | |
6 | | import first_party
7 | |
8 | | os.a
| |_^ I001
9 | third_party.a
10 | __main__.a
|
= help: Organize imports

ℹ Safe fix
1 1 | import os
2 2 |
3 |-import __main__
4 3 | import third_party
5 4 |
5 |+import __main__
6 6 | import first_party
7 7 |
8 8 | os.a


Loading