Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add N810 specifically for package or module imports #203

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add N819 specifically for package or module imports
ericbn committed Jun 9, 2022
commit bc2b690551f4eeb6c105cbb2bbbecdfae5d802b2
7 changes: 5 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
@@ -56,7 +56,7 @@ These error codes are emitted:
+---------+-----------------------------------------------------------------+
| _`N811` | constant imported as non constant (`constants`_) |
+---------+-----------------------------------------------------------------+
| _`N812` | lowercase imported as non-lowercase |
| _`N812` | lowercase imported as non lowercase |
+---------+-----------------------------------------------------------------+
| _`N813` | camelcase imported as lowercase |
+---------+-----------------------------------------------------------------+
@@ -73,14 +73,17 @@ These error codes are emitted:
+---------+-----------------------------------------------------------------+
| _`N818` | error suffix in exception names (`exceptions`_) |
+---------+-----------------------------------------------------------------+
| _`N819` | package or module imported as non lowercase |
| | (`package and module names`_) |
+---------+-----------------------------------------------------------------+

.. _class names: https://www.python.org/dev/peps/pep-0008/#class-names
.. _constants: https://www.python.org/dev/peps/pep-0008/#constants
.. _exceptions: https://www.python.org/dev/peps/pep-0008/#exception-names
.. _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
-------

9 changes: 8 additions & 1 deletion src/pep8ext_naming.py
Original file line number Diff line number Diff line change
@@ -390,6 +390,7 @@ class ImportAsCheck(BaseASTCheck):
N813 = "camelcase '{name}' imported as lowercase '{asname}'"
N814 = "camelcase '{name}' imported as constant '{asname}'"
N817 = "camelcase '{name}' imported as acronym '{asname}'"
N819 = "package or module '{name}' imported as non lowercase '{asname}'"

def visit_importfrom(self, node, parents, ignore=None):
for name in node.names:
@@ -412,7 +413,13 @@ def visit_importfrom(self, node, parents, ignore=None):
else:
yield self.err(node, 'N814', **err_kwargs)

visit_import = visit_importfrom
def visit_import(self, node, parents, ignore=None):
for name in node.names:
asname = name.asname
if not asname:
continue
if asname.lower() != asname:
yield self.err(node, 'N819', name=name.name, asname=asname)


class VariablesCheck(BaseASTCheck):
18 changes: 18 additions & 0 deletions testsuite/N819.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#: N819:1:1
import os as OS
#: Okay
import os as myos
#: Okay
import good as good
#: Okay
import GOOD as good
#: N819:1:1
import good as BAD
#: N819:1:1
import good as Bad
#: Okay
import underscore as _
#: Okay
import good as γ
#: N819:1:1
import GOOD as Γ
14 changes: 3 additions & 11 deletions testsuite/N81x.py
Original file line number Diff line number Diff line change
@@ -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