mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-27 22:04:45 +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
|
# Test absolute imports
|
||||||
|
# Violation cases
|
||||||
|
import xml.dom.minidom
|
||||||
import xml.dom.minidom as wrong
|
import xml.dom.minidom as wrong
|
||||||
from xml.dom import 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 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
|
import xml.dom.minidom as md
|
||||||
from xml.dom import minidom as md
|
from xml.dom import minidom as md
|
||||||
from xml.dom.minidom import parseString as pstr
|
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
|
# Test relative imports
|
||||||
from ..xml.dom import minidom as okay # Ensure config "xml.dom.minidom" doesn't catch 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
|
||||||
if self
|
.settings
|
||||||
.settings
|
.rules
|
||||||
.rules
|
.enabled(&Rule::ImportAliasIsNotConventional)
|
||||||
.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(
|
self.diagnostics.push(diagnostic);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(asname) = &alias.node.asname {
|
||||||
if self
|
if self
|
||||||
.settings
|
.settings
|
||||||
.rules
|
.rules
|
||||||
|
|
|
@ -7,10 +7,22 @@ expression: diagnostics
|
||||||
- xml.dom.minidom
|
- xml.dom.minidom
|
||||||
- md
|
- md
|
||||||
location:
|
location:
|
||||||
row: 2
|
row: 3
|
||||||
column: 0
|
column: 0
|
||||||
end_location:
|
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
|
column: 31
|
||||||
fix: ~
|
fix: ~
|
||||||
parent: ~
|
parent: ~
|
||||||
|
@ -19,23 +31,71 @@ expression: diagnostics
|
||||||
- xml.dom.minidom
|
- xml.dom.minidom
|
||||||
- md
|
- md
|
||||||
location:
|
location:
|
||||||
row: 3
|
row: 5
|
||||||
column: 0
|
column: 0
|
||||||
end_location:
|
end_location:
|
||||||
row: 3
|
row: 5
|
||||||
column: 36
|
column: 36
|
||||||
fix: ~
|
fix: ~
|
||||||
parent: ~
|
parent: ~
|
||||||
|
- kind:
|
||||||
|
ImportAliasIsNotConventional:
|
||||||
|
- xml.dom.minidom
|
||||||
|
- md
|
||||||
|
location:
|
||||||
|
row: 6
|
||||||
|
column: 0
|
||||||
|
end_location:
|
||||||
|
row: 6
|
||||||
|
column: 27
|
||||||
|
fix: ~
|
||||||
|
parent: ~
|
||||||
- kind:
|
- kind:
|
||||||
ImportAliasIsNotConventional:
|
ImportAliasIsNotConventional:
|
||||||
- xml.dom.minidom.parseString
|
- xml.dom.minidom.parseString
|
||||||
- pstr
|
- pstr
|
||||||
location:
|
location:
|
||||||
row: 4
|
row: 7
|
||||||
column: 0
|
column: 0
|
||||||
end_location:
|
end_location:
|
||||||
row: 4
|
row: 7
|
||||||
column: 48
|
column: 48
|
||||||
fix: ~
|
fix: ~
|
||||||
parent: ~
|
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