pep8-naming icon indicating copy to clipboard operation
pep8-naming copied to clipboard

Add N810 specifically for package or module imports

Open ericbn opened this issue 3 years ago • 2 comments

These are differences in behavior based on existing and new test cases:

statement before now
import os as OS N812 lowercase 'os' imported as non lowercase 'OS' N810 package or module 'os' imported as non lowercase 'OS'
import GOOD as good N811 constant 'GOOD' imported as non constant 'good' Okay (because good is an alias of a package or module, not an alias of a constant)
import good as BAD N812 lowercase 'good' imported as non lowercase 'BAD' N810 package or module 'good' imported as non lowercase 'BAD'
import good as Bad N812 lowercase 'good' imported as non lowercase 'Bad' N810 package or module 'good' imported as non lowercase 'Bad'
import GOOD as Γ Okay N810 package or module 'GOOD' imported as non lowercase 'Γ'

Hope it makes sense based on the discussion in #201.

ericbn avatar Jun 09 '22 21:06 ericbn

EDIT: Updated the rule code to N810, as it brings it closer to the other related rules.

ericbn avatar Jun 10 '22 01:06 ericbn

Users will only get the N810 error if they're importing a package or module with a non-lowercase alias name with the import ... as ... syntax specifically, which can only be used to import packages or modules. This is arguably not the most common error users will have in their import statements. If they upgrade pep8-naming to a version with the new N810 rule they'll start getting a message that they were violating PEP 8 in those situations, which I hope will mean an improvement.

This also fixes an error when trying to correctly alias a non-lowercase package or module name with a lowercase alias name following PEP 8. I've encountered SalesforcePy in my daily work and their documentation recommends import SalesforcePy as sfdc. This currently fails with N813 camelcase 'SalesforcePy' imported as lowercase 'sfdc' and won't fail anymore with the changes being proposed here (because sfdc is clearly an alias of a package, not an alias of a class name).

ericbn avatar Mar 06 '23 12:03 ericbn