Skip to content

Commit

Permalink
Merge pull request #111 from felixvd/add-import-shadowing
Browse files Browse the repository at this point in the history
Add rule for import shadowing
  • Loading branch information
gforcada committed Nov 1, 2023
2 parents ef3335f + dfbdca6 commit d9246da
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
15 changes: 15 additions & 0 deletions README.rst
Expand Up @@ -86,6 +86,21 @@ Requirements
- Python 3.8, 3.9, 3.10, 3.11, and pypy3
- flake8

Rules
-----

A001:
A variable is shadowing a Python builtin.

A002:
An argument is shadowing a Python builtin.

A003:
A class attribute is shadowing a Python builtin.

A004:
An import statement is shadowing a Python builtin.

License
-------
GPL 2.0
20 changes: 15 additions & 5 deletions flake8_builtins.py
Expand Up @@ -14,9 +14,10 @@
class BuiltinsChecker:
name = 'flake8_builtins'
version = '1.5.2'
assign_msg = 'A001 variable "{0}" is shadowing a python builtin'
argument_msg = 'A002 argument "{0}" is shadowing a python builtin'
class_attribute_msg = 'A003 class attribute "{0}" is shadowing a python builtin'
assign_msg = 'A001 variable "{0}" is shadowing a Python builtin'
argument_msg = 'A002 argument "{0}" is shadowing a Python builtin'
class_attribute_msg = 'A003 class attribute "{0}" is shadowing a Python builtin'
import_msg = 'A004 import statement "{0}" is shadowing a Python builtin'

names = []
ignore_list = {
Expand Down Expand Up @@ -223,8 +224,17 @@ def check_comprehension(self, statement):

def check_import(self, statement):
for name in statement.names:
if name.asname in self.names:
yield self.error(statement, variable=name.asname)
collision = None
if name.name in self.names and name.asname is None:
collision = name.name
elif name.asname in self.names:
collision = name.asname
if collision:
yield self.error(
statement,
message=self.import_msg,
variable=collision,
)

def check_class(self, statement):
if statement.name in self.names:
Expand Down
15 changes: 12 additions & 3 deletions run_tests.py
Expand Up @@ -147,7 +147,6 @@ def bla(dict=3):
b = 4"""
check_code(source, 'A002')


def test_kwonly_argument_message():
source = """
def bla(*, list):
Expand Down Expand Up @@ -357,21 +356,31 @@ def test_list_comprehension_multiple_as_list():
check_code(source, 'A001')


def test_import():
source = """from numpy import max"""
check_code(source, 'A004')


def test_import_as():
source = 'import zope.component.getSite as int'
check_code(source, 'A001')
check_code(source, 'A004')


def test_import_from_as():
source = 'from zope.component import getSite as int'
check_code(source, 'A001')
check_code(source, 'A004')


def test_import_as_nothing():
source = 'import zope.component.getSite as something_else'
check_code(source)


def test_import_collision_as_nothing():
source = """from numpy import max as non_shadowing_max"""
check_code(source)


def test_import_from_as_nothing():
source = 'from zope.component import getSite as something_else'
check_code(source)
Expand Down

0 comments on commit d9246da

Please sign in to comment.