From 46b5cdefd21c58ef23d21eac97a150cd21432304 Mon Sep 17 00:00:00 2001 From: stancld Date: Mon, 10 Apr 2023 13:23:11 +0200 Subject: [PATCH 1/2] rule[flake8-import-conventions]: Add a rule for BannedImportAlias --- .../custom_banned.py | 15 ++++ crates/ruff/src/checkers/ast/mod.rs | 31 ++++++++ crates/ruff/src/codes.rs | 1 + crates/ruff/src/registry.rs | 1 + .../rules/flake8_import_conventions/mod.rs | 36 ++++++++++ .../rules/check_banned_import.rs | 70 +++++++++++++++++++ .../check_conventional_import.rs} | 0 .../flake8_import_conventions/rules/mod.rs | 5 ++ .../flake8_import_conventions/settings.rs | 28 +++++++- ...ort_conventions__tests__custom_banned.snap | 60 ++++++++++++++++ crates/ruff/src/settings/pyproject.rs | 1 + ruff.schema.json | 14 ++++ 12 files changed, 259 insertions(+), 3 deletions(-) create mode 100644 crates/ruff/resources/test/fixtures/flake8_import_conventions/custom_banned.py create mode 100644 crates/ruff/src/rules/flake8_import_conventions/rules/check_banned_import.rs rename crates/ruff/src/rules/flake8_import_conventions/{rules.rs => rules/check_conventional_import.rs} (100%) create mode 100644 crates/ruff/src/rules/flake8_import_conventions/rules/mod.rs create mode 100644 crates/ruff/src/rules/flake8_import_conventions/snapshots/ruff__rules__flake8_import_conventions__tests__custom_banned.snap diff --git a/crates/ruff/resources/test/fixtures/flake8_import_conventions/custom_banned.py b/crates/ruff/resources/test/fixtures/flake8_import_conventions/custom_banned.py new file mode 100644 index 0000000000000..4f19db9f3f73b --- /dev/null +++ b/crates/ruff/resources/test/fixtures/flake8_import_conventions/custom_banned.py @@ -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 \ No newline at end of file diff --git a/crates/ruff/src/checkers/ast/mod.rs b/crates/ruff/src/checkers/ast/mod.rs index 704cd9b8e54ba..1c9950f28409a 100644 --- a/crates/ruff/src/checkers/ast/mod.rs +++ b/crates/ruff/src/checkers/ast/mod.rs @@ -1077,6 +1077,19 @@ where } } + if self.settings.rules.enabled(Rule::BannedImportAlias) { + if let Some(diagnostic) = + flake8_import_conventions::rules::check_banned_import( + stmt, + &alias.node.name, + alias.node.asname.as_deref(), + &self.settings.flake8_import_conventions.banned_aliases, + ) + { + self.diagnostics.push(diagnostic); + } + } + if self .settings .rules @@ -1339,6 +1352,24 @@ where } } + if self.settings.rules.enabled(Rule::BannedImportAlias) { + let full_name = helpers::format_import_from_member( + *level, + module.as_deref(), + &alias.node.name, + ); + if let Some(diagnostic) = + flake8_import_conventions::rules::check_banned_import( + stmt, + &full_name, + alias.node.asname.as_deref(), + &self.settings.flake8_import_conventions.banned_aliases, + ) + { + self.diagnostics.push(diagnostic); + } + } + if let Some(asname) = &alias.node.asname { if self .settings diff --git a/crates/ruff/src/codes.rs b/crates/ruff/src/codes.rs index 5c50683568569..e170b3b4e1ed4 100644 --- a/crates/ruff/src/codes.rs +++ b/crates/ruff/src/codes.rs @@ -525,6 +525,7 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option { // flake8-import-conventions (Flake8ImportConventions, "001") => Rule::UnconventionalImportAlias, + (Flake8ImportConventions, "002") => Rule::BannedImportAlias, // flake8-datetimez (Flake8Datetimez, "001") => Rule::CallDatetimeWithoutTzinfo, diff --git a/crates/ruff/src/registry.rs b/crates/ruff/src/registry.rs index b585c733782c4..85265f044a211 100644 --- a/crates/ruff/src/registry.rs +++ b/crates/ruff/src/registry.rs @@ -482,6 +482,7 @@ ruff_macros::register_rules!( rules::flake8_unused_arguments::rules::UnusedLambdaArgument, // flake8-import-conventions rules::flake8_import_conventions::rules::UnconventionalImportAlias, + rules::flake8_import_conventions::rules::BannedImportAlias, // flake8-datetimez rules::flake8_datetimez::rules::CallDatetimeWithoutTzinfo, rules::flake8_datetimez::rules::CallDatetimeToday, diff --git a/crates/ruff/src/rules/flake8_import_conventions/mod.rs b/crates/ruff/src/rules/flake8_import_conventions/mod.rs index e057c52200bd8..ffc97dd5edb95 100644 --- a/crates/ruff/src/rules/flake8_import_conventions/mod.rs +++ b/crates/ruff/src/rules/flake8_import_conventions/mod.rs @@ -36,6 +36,7 @@ mod tests { ("dask.array".to_string(), "da".to_string()), ("dask.dataframe".to_string(), "dd".to_string()), ])), + banned_aliases: None, } .into(), ..Settings::for_rule(Rule::UnconventionalImportAlias) @@ -45,6 +46,38 @@ mod tests { Ok(()) } + #[test] + fn custom_banned() -> Result<()> { + let diagnostics = test_path( + Path::new("flake8_import_conventions/custom_banned.py"), + &Settings { + flake8_import_conventions: super::settings::Options { + aliases: None, + extend_aliases: None, + banned_aliases: Some(FxHashMap::from_iter([ + ( + "typing".to_string(), + vec!["t".to_string(), "ty".to_string()], + ), + ( + "numpy".to_string(), + vec!["nmp".to_string(), "npy".to_string()], + ), + ( + "tensorflow.keras.backend".to_string(), + vec!["K".to_string()], + ), + ("torch.nn.functional".to_string(), vec!["F".to_string()]), + ])), + } + .into(), + ..Settings::for_rule(Rule::BannedImportAlias) + }, + )?; + assert_messages!("custom_banned", diagnostics); + Ok(()) + } + #[test] fn remove_defaults() -> Result<()> { let diagnostics = test_path( @@ -58,6 +91,7 @@ mod tests { ("seaborn".to_string(), "sns".to_string()), ])), extend_aliases: None, + banned_aliases: None, } .into(), ..Settings::for_rule(Rule::UnconventionalImportAlias) @@ -78,6 +112,7 @@ mod tests { "numpy".to_string(), "nmp".to_string(), )])), + banned_aliases: None, } .into(), ..Settings::for_rule(Rule::UnconventionalImportAlias) @@ -101,6 +136,7 @@ mod tests { "pstr".to_string(), ), ])), + banned_aliases: None, } .into(), ..Settings::for_rule(Rule::UnconventionalImportAlias) diff --git a/crates/ruff/src/rules/flake8_import_conventions/rules/check_banned_import.rs b/crates/ruff/src/rules/flake8_import_conventions/rules/check_banned_import.rs new file mode 100644 index 0000000000000..aeab1537d84f8 --- /dev/null +++ b/crates/ruff/src/rules/flake8_import_conventions/rules/check_banned_import.rs @@ -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>, +) -> Option { + 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 +} diff --git a/crates/ruff/src/rules/flake8_import_conventions/rules.rs b/crates/ruff/src/rules/flake8_import_conventions/rules/check_conventional_import.rs similarity index 100% rename from crates/ruff/src/rules/flake8_import_conventions/rules.rs rename to crates/ruff/src/rules/flake8_import_conventions/rules/check_conventional_import.rs diff --git a/crates/ruff/src/rules/flake8_import_conventions/rules/mod.rs b/crates/ruff/src/rules/flake8_import_conventions/rules/mod.rs new file mode 100644 index 0000000000000..51837aa86ae90 --- /dev/null +++ b/crates/ruff/src/rules/flake8_import_conventions/rules/mod.rs @@ -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; diff --git a/crates/ruff/src/rules/flake8_import_conventions/settings.rs b/crates/ruff/src/rules/flake8_import_conventions/settings.rs index abc187b146b74..e3cd4e1589da0 100644 --- a/crates/ruff/src/rules/flake8_import_conventions/settings.rs +++ b/crates/ruff/src/rules/flake8_import_conventions/settings.rs @@ -59,11 +59,23 @@ pub struct Options { /// A mapping of modules to their conventional import aliases. These aliases /// will be added to the `aliases` mapping. pub extend_aliases: Option>, + #[option( + default = r#"{}"#, + value_type = "dict[str, list[str]]", + example = r#" + [tool.ruff.flake8-import-conventions.banned-aliases] + # Declare the banned aliases. + "tensorflow.keras.backend" = ["K"] + "# + )] + /// A mapping of modules to their banned import aliases. + pub banned_aliases: Option>>, } #[derive(Debug, CacheKey)] pub struct Settings { pub aliases: FxHashMap, + pub banned_aliases: FxHashMap>, } fn default_aliases() -> FxHashMap { @@ -73,7 +85,9 @@ fn default_aliases() -> FxHashMap { .collect::>() } -fn resolve_aliases(options: Options) -> FxHashMap { +fn resolve_aliases( + options: Options, +) -> (FxHashMap, FxHashMap>) { let mut aliases = match options.aliases { Some(options_aliases) => options_aliases, None => default_aliases(), @@ -81,21 +95,28 @@ fn resolve_aliases(options: Options) -> FxHashMap { if let Some(extend_aliases) = options.extend_aliases { aliases.extend(extend_aliases); } - aliases + let banned_aliases = match options.banned_aliases { + Some(options_banned_aliases) => options_banned_aliases, + None => FxHashMap::default(), + }; + (aliases, banned_aliases) } impl Default for Settings { fn default() -> Self { Self { aliases: default_aliases(), + banned_aliases: FxHashMap::default(), } } } impl From for Settings { fn from(options: Options) -> Self { + let (aliases, banned_aliases) = resolve_aliases(options); Self { - aliases: resolve_aliases(options), + aliases, + banned_aliases, } } } @@ -105,6 +126,7 @@ impl From for Options { Self { aliases: Some(settings.aliases), extend_aliases: None, + banned_aliases: None, } } } diff --git a/crates/ruff/src/rules/flake8_import_conventions/snapshots/ruff__rules__flake8_import_conventions__tests__custom_banned.snap b/crates/ruff/src/rules/flake8_import_conventions/snapshots/ruff__rules__flake8_import_conventions__tests__custom_banned.snap new file mode 100644 index 0000000000000..25e87026924e1 --- /dev/null +++ b/crates/ruff/src/rules/flake8_import_conventions/snapshots/ruff__rules__flake8_import_conventions__tests__custom_banned.snap @@ -0,0 +1,60 @@ +--- +source: crates/ruff/src/rules/flake8_import_conventions/mod.rs +--- +custom_banned.py:1:1: ICN002 `typing` should not be imported as `t, ty` + | +1 | import typing as t # banned + | ^^^^^^^^^^^^^^^^^^ ICN002 +2 | import typing as ty # banned + | + +custom_banned.py:4:1: ICN002 `numpy` should not be imported as `nmp, npy` + | +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: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 + | + + diff --git a/crates/ruff/src/settings/pyproject.rs b/crates/ruff/src/settings/pyproject.rs index 765939a8030d4..5bd94cd1bbcc2 100644 --- a/crates/ruff/src/settings/pyproject.rs +++ b/crates/ruff/src/settings/pyproject.rs @@ -378,6 +378,7 @@ other-attribute = 1 "dask.dataframe".to_string(), "dd".to_string(), )])), + banned_aliases: None, }), mccabe: Some(mccabe::settings::Options { max_complexity: Some(10), diff --git a/ruff.schema.json b/ruff.schema.json index dd424e6bad665..ea4a85125df93 100644 --- a/ruff.schema.json +++ b/ruff.schema.json @@ -755,6 +755,19 @@ "type": "string" } }, + "banned-aliases": { + "description": "A mapping of modules to their banned import aliases.", + "type": [ + "object", + "null" + ], + "additionalProperties": { + "type": "array", + "items": { + "type": "string" + } + } + }, "extend-aliases": { "description": "A mapping of modules to their conventional import aliases. These aliases will be added to the `aliases` mapping.", "type": [ @@ -1775,6 +1788,7 @@ "ICN0", "ICN00", "ICN001", + "ICN002", "INP", "INP0", "INP00", From caa77560ffb71500c1cdb4086a5cd24ed693bea3 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Tue, 11 Apr 2023 23:24:01 -0400 Subject: [PATCH 2/2] Require alias --- .../custom_banned.py | 3 +- crates/ruff/src/checkers/ast/mod.rs | 50 ++++++++++--------- .../rules/check_banned_import.rs | 38 ++++++-------- .../flake8_import_conventions/settings.rs | 6 +-- ...ort_conventions__tests__custom_banned.snap | 22 +++++++- ruff.schema.json | 4 +- 6 files changed, 68 insertions(+), 55 deletions(-) diff --git a/crates/ruff/resources/test/fixtures/flake8_import_conventions/custom_banned.py b/crates/ruff/resources/test/fixtures/flake8_import_conventions/custom_banned.py index 4f19db9f3f73b..0c74a57c42c5f 100644 --- a/crates/ruff/resources/test/fixtures/flake8_import_conventions/custom_banned.py +++ b/crates/ruff/resources/test/fixtures/flake8_import_conventions/custom_banned.py @@ -12,4 +12,5 @@ import numpy as np # ok import tensorflow as tf # ok -import torch.nn as nn # ok \ No newline at end of file +import torch.nn as nn # ok +from tensorflow.keras import backend # ok diff --git a/crates/ruff/src/checkers/ast/mod.rs b/crates/ruff/src/checkers/ast/mod.rs index 1c9950f28409a..eeb2cd3597bbf 100644 --- a/crates/ruff/src/checkers/ast/mod.rs +++ b/crates/ruff/src/checkers/ast/mod.rs @@ -1078,15 +1078,17 @@ where } if self.settings.rules.enabled(Rule::BannedImportAlias) { - if let Some(diagnostic) = - flake8_import_conventions::rules::check_banned_import( - stmt, - &alias.node.name, - alias.node.asname.as_deref(), - &self.settings.flake8_import_conventions.banned_aliases, - ) - { - self.diagnostics.push(diagnostic); + if let Some(asname) = &alias.node.asname { + if let Some(diagnostic) = + flake8_import_conventions::rules::check_banned_import( + stmt, + &alias.node.name, + asname, + &self.settings.flake8_import_conventions.banned_aliases, + ) + { + self.diagnostics.push(diagnostic); + } } } @@ -1353,20 +1355,22 @@ where } if self.settings.rules.enabled(Rule::BannedImportAlias) { - let full_name = helpers::format_import_from_member( - *level, - module.as_deref(), - &alias.node.name, - ); - if let Some(diagnostic) = - flake8_import_conventions::rules::check_banned_import( - stmt, - &full_name, - alias.node.asname.as_deref(), - &self.settings.flake8_import_conventions.banned_aliases, - ) - { - self.diagnostics.push(diagnostic); + if let Some(asname) = &alias.node.asname { + let full_name = helpers::format_import_from_member( + *level, + module.as_deref(), + &alias.node.name, + ); + if let Some(diagnostic) = + flake8_import_conventions::rules::check_banned_import( + stmt, + &full_name, + asname, + &self.settings.flake8_import_conventions.banned_aliases, + ) + { + self.diagnostics.push(diagnostic); + } } } diff --git a/crates/ruff/src/rules/flake8_import_conventions/rules/check_banned_import.rs b/crates/ruff/src/rules/flake8_import_conventions/rules/check_banned_import.rs index aeab1537d84f8..bdb9894f166c2 100644 --- a/crates/ruff/src/rules/flake8_import_conventions/rules/check_banned_import.rs +++ b/crates/ruff/src/rules/flake8_import_conventions/rules/check_banned_import.rs @@ -6,25 +6,25 @@ 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. +/// 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 convention for imports violating -/// PEP 8 principle to make your code more readable idiomatic. +/// Consistency is good. Avoid using a non-standard naming convention for +/// imports, and, in particular, choosing import aliases that violate PEP 8. /// -/// 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. +/// 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, for example,: +/// Use instead: /// ```python /// import tensorflow as tf -/// ... +/// /// tf.keras.backend /// ``` #[violation] @@ -42,26 +42,16 @@ impl Violation for BannedImportAlias { pub fn check_banned_import( import_from: &Stmt, name: &str, - asname: Option<&str>, + asname: &str, banned_conventions: &FxHashMap>, ) -> Option { 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 { + if banned_aliases + .iter() + .any(|banned_alias| banned_alias == asname) + { return Some(Diagnostic::new( - BannedImportAlias(name.to_string(), banned_aliases.join(", ")), + BannedImportAlias(name.to_string(), asname.to_string()), Range::from(import_from), )); } diff --git a/crates/ruff/src/rules/flake8_import_conventions/settings.rs b/crates/ruff/src/rules/flake8_import_conventions/settings.rs index e3cd4e1589da0..51b0665df292d 100644 --- a/crates/ruff/src/rules/flake8_import_conventions/settings.rs +++ b/crates/ruff/src/rules/flake8_import_conventions/settings.rs @@ -56,8 +56,8 @@ pub struct Options { "dask.dataframe" = "dd" "# )] - /// A mapping of modules to their conventional import aliases. These aliases - /// will be added to the `aliases` mapping. + /// A mapping from module to conventional import alias. These aliases will + /// be added to the `aliases` mapping. pub extend_aliases: Option>, #[option( default = r#"{}"#, @@ -68,7 +68,7 @@ pub struct Options { "tensorflow.keras.backend" = ["K"] "# )] - /// A mapping of modules to their banned import aliases. + /// A mapping from module to its banned import aliases. pub banned_aliases: Option>>, } diff --git a/crates/ruff/src/rules/flake8_import_conventions/snapshots/ruff__rules__flake8_import_conventions__tests__custom_banned.snap b/crates/ruff/src/rules/flake8_import_conventions/snapshots/ruff__rules__flake8_import_conventions__tests__custom_banned.snap index 25e87026924e1..0936d38db8d16 100644 --- a/crates/ruff/src/rules/flake8_import_conventions/snapshots/ruff__rules__flake8_import_conventions__tests__custom_banned.snap +++ b/crates/ruff/src/rules/flake8_import_conventions/snapshots/ruff__rules__flake8_import_conventions__tests__custom_banned.snap @@ -1,14 +1,23 @@ --- source: crates/ruff/src/rules/flake8_import_conventions/mod.rs --- -custom_banned.py:1:1: ICN002 `typing` should not be imported as `t, ty` +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:4:1: ICN002 `numpy` should not be imported as `nmp, npy` +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 | @@ -18,6 +27,15 @@ custom_banned.py:4:1: ICN002 `numpy` should not be imported as `nmp, npy` 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 diff --git a/ruff.schema.json b/ruff.schema.json index ea4a85125df93..a6a16502f40b7 100644 --- a/ruff.schema.json +++ b/ruff.schema.json @@ -756,7 +756,7 @@ } }, "banned-aliases": { - "description": "A mapping of modules to their banned import aliases.", + "description": "A mapping from module to its banned import aliases.", "type": [ "object", "null" @@ -769,7 +769,7 @@ } }, "extend-aliases": { - "description": "A mapping of modules to their conventional import aliases. These aliases will be added to the `aliases` mapping.", + "description": "A mapping from module to conventional import alias. These aliases will be added to the `aliases` mapping.", "type": [ "object", "null"