-
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
Daniel
committed
Apr 10, 2023
1 parent
002caad
commit 22fc205
Showing
12 changed files
with
254 additions
and
3 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
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,12 @@ | ||
import typing as t # unconventional | ||
import typing as ty # unconventional | ||
|
||
import tensorflow.keras.backend as K # unconventional | ||
import torch.nn.functional as F # unconventional | ||
from tensorflow.keras import backend as K # unconventional | ||
from torch.nn import functional as F # unconventional | ||
|
||
from typing import Any # conventional | ||
|
||
import tensorflow as tf # conventional | ||
import torch.nn as nn # conventional |
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
67 changes: 67 additions & 0 deletions
67
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,67 @@ | ||
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 idiomatic. | ||
/// | ||
/// For example, `import tensorflow.keras.backend as K` is an example of vioalting | ||
/// PEP 8 principle, and users should typically avoid such imports in large codebases. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// import tensorflow.keras.backend as K | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```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, String>, | ||
) -> Option<Diagnostic> { | ||
let mut is_valid_import = true; | ||
if let Some(expected_alias) = banned_conventions.get(name) { | ||
if !expected_alias.is_empty() { | ||
if let Some(alias) = asname { | ||
if expected_alias == alias { | ||
is_valid_import = false; | ||
} | ||
} else { | ||
is_valid_import = false | ||
} | ||
} | ||
if !is_valid_import { | ||
return Some(Diagnostic::new( | ||
BannedImportAlias(name.to_string(), expected_alias.to_string()), | ||
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
75 changes: 75 additions & 0 deletions
75
...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,75 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_import_conventions/mod.rs | ||
expression: diagnostics | ||
--- | ||
- kind: | ||
name: BannedImportAlias | ||
body: "`typing` should not be imported as `ty`" | ||
suggestion: ~ | ||
fixable: false | ||
location: | ||
row: 2 | ||
column: 0 | ||
end_location: | ||
row: 2 | ||
column: 19 | ||
fix: | ||
edits: [] | ||
parent: ~ | ||
- kind: | ||
name: BannedImportAlias | ||
body: "`tensorflow.keras.backend` should not be imported as `K`" | ||
suggestion: ~ | ||
fixable: false | ||
location: | ||
row: 4 | ||
column: 0 | ||
end_location: | ||
row: 4 | ||
column: 36 | ||
fix: | ||
edits: [] | ||
parent: ~ | ||
- kind: | ||
name: BannedImportAlias | ||
body: "`torch.nn.functional` should not be imported as `F`" | ||
suggestion: ~ | ||
fixable: false | ||
location: | ||
row: 5 | ||
column: 0 | ||
end_location: | ||
row: 5 | ||
column: 31 | ||
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: 41 | ||
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: 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.