-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[
flake8-import-conventions
] Implement new rule ICN003
to ban `fro…
…m ... import ...` for selected modules (#4040)
- Loading branch information
1 parent
f5cd659
commit cfc7d8a
Showing
11 changed files
with
172 additions
and
21 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
crates/ruff/resources/test/fixtures/flake8_import_conventions/custom_banned_from.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from logging.config import BaseConfigurator # banned | ||
from typing import Any, Dict # banned | ||
from typing import * # banned | ||
|
||
from pandas import DataFrame # banned | ||
from pandas import * # banned | ||
|
||
import logging.config # ok | ||
import typing # ok | ||
import pandas # ok |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
crates/ruff/src/rules/flake8_import_conventions/rules/check_banned_import_from.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use rustc_hash::FxHashSet; | ||
use rustpython_parser::ast::Stmt; | ||
|
||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::types::Range; | ||
|
||
#[violation] | ||
pub struct BannedImportFrom(pub String); | ||
|
||
impl Violation for BannedImportFrom { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
let BannedImportFrom(name) = self; | ||
format!("Members of `{name}` should not be imported explicitly") | ||
} | ||
} | ||
|
||
/// ICN003 | ||
pub fn check_banned_import_from( | ||
import_from: &Stmt, | ||
name: &str, | ||
banned_conventions: &FxHashSet<String>, | ||
) -> Option<Diagnostic> { | ||
if banned_conventions.contains(name) { | ||
return Some(Diagnostic::new( | ||
BannedImportFrom(name.to_string()), | ||
Range::from(import_from), | ||
)); | ||
} | ||
None | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
pub use check_banned_import::{check_banned_import, BannedImportAlias}; | ||
pub use check_banned_import_from::{check_banned_import_from, BannedImportFrom}; | ||
pub use check_conventional_import::{check_conventional_import, UnconventionalImportAlias}; | ||
|
||
mod check_banned_import; | ||
mod check_banned_import_from; | ||
mod check_conventional_import; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...ventions/snapshots/ruff__rules__flake8_import_conventions__tests__custom_banned_from.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_import_conventions/mod.rs | ||
--- | ||
custom_banned_from.py:1:1: ICN003 Members of `logging.config` should not be imported explicitly | ||
| | ||
1 | from logging.config import BaseConfigurator # banned | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ICN003 | ||
2 | from typing import Any, Dict # banned | ||
3 | from typing import * # banned | ||
| | ||
|
||
custom_banned_from.py:2:1: ICN003 Members of `typing` should not be imported explicitly | ||
| | ||
2 | from logging.config import BaseConfigurator # banned | ||
3 | from typing import Any, Dict # banned | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ICN003 | ||
4 | from typing import * # banned | ||
| | ||
|
||
custom_banned_from.py:3:1: ICN003 Members of `typing` should not be imported explicitly | ||
| | ||
3 | from logging.config import BaseConfigurator # banned | ||
4 | from typing import Any, Dict # banned | ||
5 | from typing import * # banned | ||
| ^^^^^^^^^^^^^^^^^^^^ ICN003 | ||
6 | | ||
7 | from pandas import DataFrame # banned | ||
| | ||
|
||
custom_banned_from.py:5:1: ICN003 Members of `pandas` should not be imported explicitly | ||
| | ||
5 | from typing import * # banned | ||
6 | | ||
7 | from pandas import DataFrame # banned | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ICN003 | ||
8 | from pandas import * # banned | ||
| | ||
|
||
custom_banned_from.py:6:1: ICN003 Members of `pandas` should not be imported explicitly | ||
| | ||
6 | from pandas import DataFrame # banned | ||
7 | from pandas import * # banned | ||
| ^^^^^^^^^^^^^^^^^^^^ ICN003 | ||
8 | | ||
9 | import logging.config # ok | ||
| | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.