From 2ff0b74c38e4fa26d54c6be64a1025676f250f31 Mon Sep 17 00:00:00 2001 From: Felix von Drigalski Date: Thu, 28 Sep 2023 15:53:57 +0900 Subject: [PATCH 1/2] Add rule for imports --- flake8_builtins.py | 14 ++++++++++++-- run_tests.py | 15 ++++++++++++--- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/flake8_builtins.py b/flake8_builtins.py index 441fd7c..8753eed 100644 --- a/flake8_builtins.py +++ b/flake8_builtins.py @@ -17,6 +17,7 @@ class BuiltinsChecker: 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 = { @@ -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: diff --git a/run_tests.py b/run_tests.py index f96297f..d665529 100644 --- a/run_tests.py +++ b/run_tests.py @@ -147,7 +147,6 @@ def bla(dict=3): b = 4""" check_code(source, 'A002') - def test_kwonly_argument_message(): source = """ def bla(*, list): @@ -357,14 +356,19 @@ 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(): @@ -372,6 +376,11 @@ def test_import_as_nothing(): 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) From dfbdca6eea1360c6e793e6fd605a6266ebde33b8 Mon Sep 17 00:00:00 2001 From: Felix von Drigalski Date: Thu, 28 Sep 2023 15:54:10 +0900 Subject: [PATCH 2/2] Add rules to Readme, fix capitalization --- README.rst | 15 +++++++++++++++ flake8_builtins.py | 6 +++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index 07787f0..1cc3c7e 100644 --- a/README.rst +++ b/README.rst @@ -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 diff --git a/flake8_builtins.py b/flake8_builtins.py index 8753eed..d178daf 100644 --- a/flake8_builtins.py +++ b/flake8_builtins.py @@ -14,9 +14,9 @@ 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 = []