-
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.
- Loading branch information
Showing
12 changed files
with
275 additions
and
6 deletions.
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.