diff --git a/README.rst b/README.rst index f6fcc16..b29f683 100644 --- a/README.rst +++ b/README.rst @@ -54,9 +54,12 @@ These error codes are emitted: +---------+-----------------------------------------------------------------+ | _`N807` | function name should not start and end with '__' | +---------+-----------------------------------------------------------------+ +| _`N810` | package or module imported as non lowercase | +| | (`package and module names`_) | ++---------+-----------------------------------------------------------------+ | _`N811` | constant imported as non constant (`constants`_) | +---------+-----------------------------------------------------------------+ -| _`N812` | lowercase imported as non-lowercase | +| _`N812` | lowercase imported as non lowercase | +---------+-----------------------------------------------------------------+ | _`N813` | camelcase imported as lowercase | +---------+-----------------------------------------------------------------+ @@ -80,7 +83,7 @@ These error codes are emitted: .. _function names: https://www.python.org/dev/peps/pep-0008/#function-and-variable-names .. _function arguments: https://www.python.org/dev/peps/pep-0008/#function-and-method-arguments .. _method names: https://www.python.org/dev/peps/pep-0008/#method-names-and-instance-variables - +.. _package and module names: https://peps.python.org/pep-0008/#package-and-module-names Options ------- diff --git a/src/pep8ext_naming.py b/src/pep8ext_naming.py index 2755aaa..0dc9220 100644 --- a/src/pep8ext_naming.py +++ b/src/pep8ext_naming.py @@ -385,12 +385,19 @@ class ImportAsCheck(BaseASTCheck): """ Don't change the naming convention via an import """ + N810 = "package or module '{name}' imported as non lowercase '{asname}'" N811 = "constant '{name}' imported as non constant '{asname}'" N812 = "lowercase '{name}' imported as non lowercase '{asname}'" N813 = "camelcase '{name}' imported as lowercase '{asname}'" N814 = "camelcase '{name}' imported as constant '{asname}'" N817 = "camelcase '{name}' imported as acronym '{asname}'" + def visit_import(self, node, parents, ignore=None): + for name in node.names: + asname = name.asname + if asname and asname.lower() != asname: + yield self.err(node, 'N810', name=name.name, asname=asname) + def visit_importfrom(self, node, parents, ignore=None): for name in node.names: asname = name.asname @@ -412,8 +419,6 @@ def visit_importfrom(self, node, parents, ignore=None): else: yield self.err(node, 'N814', **err_kwargs) - visit_import = visit_importfrom - class VariablesCheck(BaseASTCheck): """ diff --git a/testsuite/N810.py b/testsuite/N810.py new file mode 100644 index 0000000..18a7087 --- /dev/null +++ b/testsuite/N810.py @@ -0,0 +1,18 @@ +#: N810:1:1 +import os as OS +#: Okay +import os as myos +#: Okay +import good as good +#: Okay +import GOOD as good +#: N810:1:1 +import good as BAD +#: N810:1:1 +import good as Bad +#: Okay +import underscore as _ +#: Okay +import good as γ +#: N810:1:1 +import GOOD as Γ diff --git a/testsuite/N81x.py b/testsuite/N81x.py index 108d857..1552531 100644 --- a/testsuite/N81x.py +++ b/testsuite/N81x.py @@ -1,16 +1,12 @@ -#: N812:1:1 -import os as OS -#: Okay -import os as myos -#: Okay -import good as good #: Okay -import underscore as _ +from mod import underscore as _ #: Okay from mod import good as nice, NICE as GOOD, Camel as Memel #: N811:1:1 from mod import GOOD as bad #: N812:1:1 +from mod import good as BAD +#: N812:1:1 from mod import good as Bad #: N813:1:1 from mod import CamelCase as noncamle @@ -19,12 +15,8 @@ #: N817:1:1 from mod import CamelCase as CC #: Okay -import good as γ -#: Okay from mod import good as γ #: Okay -import GOOD as Γ -#: Okay from mod import GOOD as Γ #: Okay from mod import GOOD as Γ1