Skip to content

Commit

Permalink
[pep8-naming] Don't flag from imports following conventional impo…
Browse files Browse the repository at this point in the history
…rt names (`N817`) (#12946)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
  • Loading branch information
MichaReiser and AlexWaygood authored Aug 17, 2024
1 parent dd0a7ec commit 96802d6
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@

# 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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
|
Original file line number Diff line number Diff line change
Expand Up @@ -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
|

0 comments on commit 96802d6

Please sign in to comment.