-
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.
rule[flake8-import-conventions]: Add a rule for BannedImportAlias
- Loading branch information
Showing
12 changed files
with
288 additions
and
3 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
crates/ruff/resources/test/fixtures/flake8_import_conventions/custom_banned.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,15 @@ | ||
import typing as t # banned | ||
import typing as ty # banned | ||
|
||
import numpy as nmp # banned | ||
import numpy as npy # banned | ||
import tensorflow.keras.backend as K # banned | ||
import torch.nn.functional as F # banned | ||
from tensorflow.keras import backend as K # banned | ||
from torch.nn import functional as F # banned | ||
|
||
from typing import Any # ok | ||
|
||
import numpy as np # ok | ||
import tensorflow as tf # ok | ||
import torch.nn as nn # 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
70 changes: 70 additions & 0 deletions
70
crates/ruff/src/rules/flake8_import_conventions/rules/check_banned_import.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,70 @@ | ||
use rustc_hash::FxHashMap; | ||
use rustpython_parser::ast::Stmt; | ||
|
||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::types::Range; | ||
|
||
/// ## What it does | ||
/// Checks for imports that should not be using a non-standard convention, | ||
/// like `import tensorflow.keras.backend as K`, and suggest avoiding such practice. | ||
/// | ||
/// ## Why is this bad? | ||
/// Consistency is good. Avoid using a non-standard convention for imports violating | ||
/// PEP 8 principle to make your code more readable idiomatic. | ||
/// | ||
/// For example, `import tensorflow.keras.backend as K` is an example of violating | ||
/// PEP 8 principle, and users should typically avoid such imports in large codebases. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// import tensorflow.keras.backend as K | ||
/// ``` | ||
/// | ||
/// Use instead, for example,: | ||
/// ```python | ||
/// import tensorflow as tf | ||
/// ... | ||
/// tf.keras.backend | ||
/// ``` | ||
#[violation] | ||
pub struct BannedImportAlias(pub String, pub String); | ||
|
||
impl Violation for BannedImportAlias { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
let BannedImportAlias(name, asname) = self; | ||
format!("`{name}` should not be imported as `{asname}`") | ||
} | ||
} | ||
|
||
/// ICN002 | ||
pub fn check_banned_import( | ||
import_from: &Stmt, | ||
name: &str, | ||
asname: Option<&str>, | ||
banned_conventions: &FxHashMap<String, Vec<String>>, | ||
) -> Option<Diagnostic> { | ||
if let Some(banned_aliases) = banned_conventions.get(name) { | ||
let mut is_valid_import = true; | ||
for banned_alias in banned_aliases { | ||
if !banned_alias.is_empty() { | ||
if let Some(alias) = asname { | ||
if banned_alias == alias { | ||
is_valid_import = false; | ||
} | ||
} else { | ||
is_valid_import = false; | ||
} | ||
break; | ||
} | ||
} | ||
if !is_valid_import { | ||
return Some(Diagnostic::new( | ||
BannedImportAlias(name.to_string(), banned_aliases.join(", ")), | ||
Range::from(import_from), | ||
)); | ||
} | ||
} | ||
None | ||
} |
File renamed without changes.
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,5 @@ | ||
pub use check_banned_import::{check_banned_import, BannedImportAlias}; | ||
pub use check_conventional_import::{check_conventional_import, UnconventionalImportAlias}; | ||
|
||
mod check_banned_import; | ||
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
89 changes: 89 additions & 0 deletions
89
...t_conventions/snapshots/ruff__rules__flake8_import_conventions__tests__custom_banned.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,89 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_import_conventions/mod.rs | ||
expression: diagnostics | ||
--- | ||
- kind: | ||
name: BannedImportAlias | ||
body: "`typing` should not be imported as `t, ty`" | ||
suggestion: ~ | ||
fixable: false | ||
location: | ||
row: 1 | ||
column: 0 | ||
end_location: | ||
row: 1 | ||
column: 18 | ||
fix: | ||
edits: [] | ||
parent: ~ | ||
- kind: | ||
name: BannedImportAlias | ||
body: "`numpy` should not be imported as `nmp, npy`" | ||
suggestion: ~ | ||
fixable: false | ||
location: | ||
row: 4 | ||
column: 0 | ||
end_location: | ||
row: 4 | ||
column: 19 | ||
fix: | ||
edits: [] | ||
parent: ~ | ||
- kind: | ||
name: BannedImportAlias | ||
body: "`tensorflow.keras.backend` should not be imported as `K`" | ||
suggestion: ~ | ||
fixable: false | ||
location: | ||
row: 6 | ||
column: 0 | ||
end_location: | ||
row: 6 | ||
column: 36 | ||
fix: | ||
edits: [] | ||
parent: ~ | ||
- kind: | ||
name: BannedImportAlias | ||
body: "`torch.nn.functional` should not be imported as `F`" | ||
suggestion: ~ | ||
fixable: false | ||
location: | ||
row: 7 | ||
column: 0 | ||
end_location: | ||
row: 7 | ||
column: 31 | ||
fix: | ||
edits: [] | ||
parent: ~ | ||
- kind: | ||
name: BannedImportAlias | ||
body: "`tensorflow.keras.backend` should not be imported as `K`" | ||
suggestion: ~ | ||
fixable: false | ||
location: | ||
row: 8 | ||
column: 0 | ||
end_location: | ||
row: 8 | ||
column: 41 | ||
fix: | ||
edits: [] | ||
parent: ~ | ||
- kind: | ||
name: BannedImportAlias | ||
body: "`torch.nn.functional` should not be imported as `F`" | ||
suggestion: ~ | ||
fixable: false | ||
location: | ||
row: 9 | ||
column: 0 | ||
end_location: | ||
row: 9 | ||
column: 36 | ||
fix: | ||
edits: [] | ||
parent: ~ | ||
|
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
Oops, something went wrong.