[pep8-naming] Don't flag from imports following conventional import names (N817) (#12946)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
Micha Reiser
2024-08-17 14:05:42 +02:00
committed by GitHub
parent dd0a7ec73e
commit 96802d6a7f
4 changed files with 60 additions and 9 deletions

View File

@@ -4,3 +4,7 @@ from mod import CamelCase as CC
# OK depending on configured import convention
import xml.etree.ElementTree as ET
from xml.etree import ElementTree as ET
# Always an error (relative import)
from ..xml.eltree import ElementTree as ET

View File

@@ -74,14 +74,7 @@ pub(crate) fn camelcase_imported_as_acronym(
}
// Ignore names that follow a community-agreed import convention.
if checker
.settings
.flake8_import_conventions
.aliases
.get(&*alias.name)
.map(String::as_str)
== Some(asname)
{
if is_ignored_because_of_import_convention(asname, stmt, alias, checker) {
return None;
}
@@ -97,3 +90,34 @@ pub(crate) fn camelcase_imported_as_acronym(
}
None
}
fn is_ignored_because_of_import_convention(
asname: &str,
stmt: &Stmt,
alias: &Alias,
checker: &Checker,
) -> bool {
let full_name = if let Some(import_from) = stmt.as_import_from_stmt() {
// Never test relative imports for exclusion because we can't resolve the full-module name.
let Some(module) = import_from.module.as_ref() else {
return false;
};
if import_from.level != 0 {
return false;
}
std::borrow::Cow::Owned(format!("{module}.{}", alias.name))
} else {
std::borrow::Cow::Borrowed(&*alias.name)
};
// Ignore names that follow a community-agreed import convention.
checker
.settings
.flake8_import_conventions
.aliases
.get(&*full_name)
.map(String::as_str)
== Some(asname)
}

View File

@@ -15,4 +15,9 @@ N817.py:2:17: N817 CamelCase `CamelCase` imported as acronym `CC`
| ^^^^^^^^^^^^^^^ N817
|
N817.py:10:26: N817 CamelCase `ElementTree` imported as acronym `ET`
|
9 | # Always an error (relative import)
10 | from ..xml.eltree import ElementTree as ET
| ^^^^^^^^^^^^^^^^^ N817
|

View File

@@ -20,4 +20,22 @@ N817.py:6:8: N817 CamelCase `ElementTree` imported as acronym `ET`
5 | # OK depending on configured import convention
6 | import xml.etree.ElementTree as ET
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ N817
7 | from xml.etree import ElementTree as ET
|
N817.py:7:23: N817 CamelCase `ElementTree` imported as acronym `ET`
|
5 | # OK depending on configured import convention
6 | import xml.etree.ElementTree as ET
7 | from xml.etree import ElementTree as ET
| ^^^^^^^^^^^^^^^^^ N817
8 |
9 | # Always an error (relative import)
|
N817.py:10:26: N817 CamelCase `ElementTree` imported as acronym `ET`
|
9 | # Always an error (relative import)
10 | from ..xml.eltree import ElementTree as ET
| ^^^^^^^^^^^^^^^^^ N817
|