mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-27 13:54:50 +00:00
ICN001 check from imports that have no alias (#2072)
Add tests. Ensure that these cases are caught by ICN001: ```python from xml.dom import minidom from xml.dom.minidom import parseString ``` with config: ```toml [tool.ruff.flake8-import-conventions.extend-aliases] "dask.dataframe" = "dd" "xml.dom.minidom" = "md" "xml.dom.minidom.parseString" = "pstr" ```
This commit is contained in:
parent
39aed6f11d
commit
08fc9b8095
3 changed files with 96 additions and 26 deletions
|
@ -1,11 +1,21 @@
|
|||
# Test absolute imports
|
||||
# Violation cases
|
||||
import xml.dom.minidom
|
||||
import xml.dom.minidom as wrong
|
||||
from xml.dom import minidom as wrong
|
||||
from xml.dom import minidom
|
||||
from xml.dom.minidom import parseString as wrong # Ensure ICN001 throws on function import.
|
||||
from xml.dom.minidom import parseString
|
||||
from xml.dom.minidom import parse, parseString
|
||||
from xml.dom.minidom import parse as ps, parseString as wrong
|
||||
|
||||
# No ICN001 violations
|
||||
import xml.dom.minidom as md
|
||||
from xml.dom import minidom as md
|
||||
from xml.dom.minidom import parseString as pstr
|
||||
from xml.dom.minidom import parse, parseString as pstr
|
||||
from xml.dom.minidom import parse as ps, parseString as pstr
|
||||
|
||||
|
||||
# Test relative imports
|
||||
from ..xml.dom import minidom as okay # Ensure config "xml.dom.minidom" doesn't catch relative imports
|
||||
|
|
|
@ -1230,29 +1230,29 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
if let Some(asname) = &alias.node.asname {
|
||||
if self
|
||||
.settings
|
||||
.rules
|
||||
.enabled(&Rule::ImportAliasIsNotConventional)
|
||||
if self
|
||||
.settings
|
||||
.rules
|
||||
.enabled(&Rule::ImportAliasIsNotConventional)
|
||||
{
|
||||
let full_name = helpers::format_import_from_member(
|
||||
level.as_ref(),
|
||||
module.as_deref(),
|
||||
&alias.node.name,
|
||||
);
|
||||
if let Some(diagnostic) =
|
||||
flake8_import_conventions::rules::check_conventional_import(
|
||||
stmt,
|
||||
&full_name,
|
||||
alias.node.asname.as_deref(),
|
||||
&self.settings.flake8_import_conventions.aliases,
|
||||
)
|
||||
{
|
||||
let full_name = helpers::format_import_from_member(
|
||||
level.as_ref(),
|
||||
module.as_deref(),
|
||||
&alias.node.name,
|
||||
);
|
||||
if let Some(diagnostic) =
|
||||
flake8_import_conventions::rules::check_conventional_import(
|
||||
stmt,
|
||||
&full_name,
|
||||
alias.node.asname.as_deref(),
|
||||
&self.settings.flake8_import_conventions.aliases,
|
||||
)
|
||||
{
|
||||
self.diagnostics.push(diagnostic);
|
||||
}
|
||||
self.diagnostics.push(diagnostic);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(asname) = &alias.node.asname {
|
||||
if self
|
||||
.settings
|
||||
.rules
|
||||
|
|
|
@ -7,10 +7,22 @@ expression: diagnostics
|
|||
- xml.dom.minidom
|
||||
- md
|
||||
location:
|
||||
row: 2
|
||||
row: 3
|
||||
column: 0
|
||||
end_location:
|
||||
row: 2
|
||||
row: 3
|
||||
column: 22
|
||||
fix: ~
|
||||
parent: ~
|
||||
- kind:
|
||||
ImportAliasIsNotConventional:
|
||||
- xml.dom.minidom
|
||||
- md
|
||||
location:
|
||||
row: 4
|
||||
column: 0
|
||||
end_location:
|
||||
row: 4
|
||||
column: 31
|
||||
fix: ~
|
||||
parent: ~
|
||||
|
@ -19,23 +31,71 @@ expression: diagnostics
|
|||
- xml.dom.minidom
|
||||
- md
|
||||
location:
|
||||
row: 3
|
||||
row: 5
|
||||
column: 0
|
||||
end_location:
|
||||
row: 3
|
||||
row: 5
|
||||
column: 36
|
||||
fix: ~
|
||||
parent: ~
|
||||
- kind:
|
||||
ImportAliasIsNotConventional:
|
||||
- xml.dom.minidom
|
||||
- md
|
||||
location:
|
||||
row: 6
|
||||
column: 0
|
||||
end_location:
|
||||
row: 6
|
||||
column: 27
|
||||
fix: ~
|
||||
parent: ~
|
||||
- kind:
|
||||
ImportAliasIsNotConventional:
|
||||
- xml.dom.minidom.parseString
|
||||
- pstr
|
||||
location:
|
||||
row: 4
|
||||
row: 7
|
||||
column: 0
|
||||
end_location:
|
||||
row: 4
|
||||
row: 7
|
||||
column: 48
|
||||
fix: ~
|
||||
parent: ~
|
||||
- kind:
|
||||
ImportAliasIsNotConventional:
|
||||
- xml.dom.minidom.parseString
|
||||
- pstr
|
||||
location:
|
||||
row: 8
|
||||
column: 0
|
||||
end_location:
|
||||
row: 8
|
||||
column: 39
|
||||
fix: ~
|
||||
parent: ~
|
||||
- kind:
|
||||
ImportAliasIsNotConventional:
|
||||
- xml.dom.minidom.parseString
|
||||
- pstr
|
||||
location:
|
||||
row: 9
|
||||
column: 0
|
||||
end_location:
|
||||
row: 9
|
||||
column: 46
|
||||
fix: ~
|
||||
parent: ~
|
||||
- kind:
|
||||
ImportAliasIsNotConventional:
|
||||
- xml.dom.minidom.parseString
|
||||
- pstr
|
||||
location:
|
||||
row: 10
|
||||
column: 0
|
||||
end_location:
|
||||
row: 10
|
||||
column: 61
|
||||
fix: ~
|
||||
parent: ~
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue