-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
[flake8-import-conventions
] Add a rule for BannedImportAlias
#3926
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
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,16 @@ | ||
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 | ||
from tensorflow.keras import backend # 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
60 changes: 60 additions & 0 deletions
60
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,60 @@ | ||
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 use non-standard naming conventions, like | ||
/// `import tensorflow.keras.backend as K`. | ||
/// | ||
/// ## Why is this bad? | ||
/// Consistency is good. Avoid using a non-standard naming convention for | ||
/// imports, and, in particular, choosing import aliases that violate PEP 8. | ||
/// | ||
/// For example, aliasing via `import tensorflow.keras.backend as K` violates | ||
/// the guidance of PEP 8, and is thus avoided in some projects. | ||
/// | ||
/// ## 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: &str, | ||
banned_conventions: &FxHashMap<String, Vec<String>>, | ||
) -> Option<Diagnostic> { | ||
if let Some(banned_aliases) = banned_conventions.get(name) { | ||
if banned_aliases | ||
.iter() | ||
.any(|banned_alias| banned_alias == asname) | ||
{ | ||
return Some(Diagnostic::new( | ||
BannedImportAlias(name.to_string(), asname.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
78 changes: 78 additions & 0 deletions
78
...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,78 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_import_conventions/mod.rs | ||
--- | ||
custom_banned.py:1:1: ICN002 `typing` should not be imported as `t` | ||
| | ||
1 | import typing as t # banned | ||
| ^^^^^^^^^^^^^^^^^^ ICN002 | ||
2 | import typing as ty # banned | ||
| | ||
|
||
custom_banned.py:2:1: ICN002 `typing` should not be imported as `ty` | ||
| | ||
2 | import typing as t # banned | ||
3 | import typing as ty # banned | ||
| ^^^^^^^^^^^^^^^^^^^ ICN002 | ||
4 | | ||
5 | import numpy as nmp # banned | ||
| | ||
|
||
custom_banned.py:4:1: ICN002 `numpy` should not be imported as `nmp` | ||
| | ||
4 | import typing as ty # banned | ||
5 | | ||
6 | import numpy as nmp # banned | ||
| ^^^^^^^^^^^^^^^^^^^ ICN002 | ||
7 | import numpy as npy # banned | ||
8 | import tensorflow.keras.backend as K # banned | ||
| | ||
|
||
custom_banned.py:5:1: ICN002 `numpy` should not be imported as `npy` | ||
| | ||
5 | import numpy as nmp # banned | ||
6 | import numpy as npy # banned | ||
| ^^^^^^^^^^^^^^^^^^^ ICN002 | ||
7 | import tensorflow.keras.backend as K # banned | ||
8 | import torch.nn.functional as F # banned | ||
| | ||
|
||
custom_banned.py:6:1: ICN002 `tensorflow.keras.backend` should not be imported as `K` | ||
| | ||
6 | import numpy as nmp # banned | ||
7 | import numpy as npy # banned | ||
8 | import tensorflow.keras.backend as K # banned | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ICN002 | ||
9 | import torch.nn.functional as F # banned | ||
10 | from tensorflow.keras import backend as K # banned | ||
| | ||
|
||
custom_banned.py:7:1: ICN002 `torch.nn.functional` should not be imported as `F` | ||
| | ||
7 | import numpy as npy # banned | ||
8 | import tensorflow.keras.backend as K # banned | ||
9 | import torch.nn.functional as F # banned | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ICN002 | ||
10 | from tensorflow.keras import backend as K # banned | ||
11 | from torch.nn import functional as F # banned | ||
| | ||
|
||
custom_banned.py:8:1: ICN002 `tensorflow.keras.backend` should not be imported as `K` | ||
| | ||
8 | import tensorflow.keras.backend as K # banned | ||
9 | import torch.nn.functional as F # banned | ||
10 | from tensorflow.keras import backend as K # banned | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ICN002 | ||
11 | from torch.nn import functional as F # banned | ||
| | ||
|
||
custom_banned.py:9:1: ICN002 `torch.nn.functional` should not be imported as `F` | ||
| | ||
9 | import torch.nn.functional as F # banned | ||
10 | from tensorflow.keras import backend as K # banned | ||
11 | from torch.nn import functional as F # banned | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ICN002 | ||
12 | | ||
13 | from typing import Any # 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tweaked the logic here a bit, I think it was unintentionally flagging any non-aliased import.