mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-28 12:55:05 +00:00
[flake8-import-conventions
] Improve syntax check for aliases supplied in configuration for unconventional-import-alias (ICN001)
(#14745)
This PR improves on #14477 by: - Ensuring user's do not require the module alias "__debug__", which is unassignable - Validating the linter settings for `lint.flake8-import-conventions.extend-aliases` (whereas previously we only did this for `lint.flake8-import-conventions.aliases`). Closes #14662
This commit is contained in:
parent
246a6df87d
commit
10fef8bd5d
3 changed files with 52 additions and 3 deletions
|
@ -1342,7 +1342,7 @@ pub struct Flake8ImportConventionsOptions {
|
|||
"dask.dataframe" = "dd"
|
||||
"#
|
||||
)]
|
||||
pub extend_aliases: Option<FxHashMap<String, String>>,
|
||||
pub extend_aliases: Option<FxHashMap<ModuleName, Alias>>,
|
||||
|
||||
/// A mapping from module to its banned import aliases.
|
||||
#[option(
|
||||
|
@ -1415,6 +1415,15 @@ impl<'de> Deserialize<'de> for Alias {
|
|||
D: Deserializer<'de>,
|
||||
{
|
||||
let name = String::deserialize(deserializer)?;
|
||||
// Assigning to "__debug__" is a SyntaxError
|
||||
// see the note here:
|
||||
// https://docs.python.org/3/library/constants.html#debug__
|
||||
if &*name == "__debug__" {
|
||||
return Err(de::Error::invalid_value(
|
||||
de::Unexpected::Str(&name),
|
||||
&"an assignable Python identifier",
|
||||
));
|
||||
}
|
||||
if is_identifier(&name) {
|
||||
Ok(Self(name))
|
||||
} else {
|
||||
|
@ -1436,7 +1445,11 @@ impl Flake8ImportConventionsOptions {
|
|||
None => flake8_import_conventions::settings::default_aliases(),
|
||||
};
|
||||
if let Some(extend_aliases) = self.extend_aliases {
|
||||
aliases.extend(extend_aliases);
|
||||
aliases.extend(
|
||||
extend_aliases
|
||||
.into_iter()
|
||||
.map(|(module, alias)| (module.into_string(), alias.into_string())),
|
||||
);
|
||||
}
|
||||
|
||||
flake8_import_conventions::settings::Settings {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue