Skip to content

Commit

Permalink
fix: check modules only in python 3.10 and above
Browse files Browse the repository at this point in the history
  • Loading branch information
asfaltboy committed Mar 25, 2024
1 parent 33bc80d commit 246537c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
16 changes: 10 additions & 6 deletions flake8_builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,14 @@ def parse_options(cls, options):
if options.builtins_allowed_modules is not None:
cls.ignored_module_names.update(options.builtins_allowed_modules)

known_module_names = getattr(
sys, 'stdlib_module_names', sys.builtin_module_names
)
cls.module_names = {
m for m in known_module_names if m not in cls.ignored_module_names
}
if hasattr(sys, 'stdlib_module_names'):
# stdlib_module_names is only available in Python 3.10+
known_module_names = sys.stdlib_module_names
cls.module_names = {
m for m in known_module_names if m not in cls.ignored_module_names
}
else:
cls.module_names = set()

def run(self):
tree = self.tree
Expand Down Expand Up @@ -270,6 +272,8 @@ def error(self, statement=None, variable=None, message=None):
)

def check_module_name(self, filename: str):
if not self.module_names:
return
path = Path(filename)
module_name = path.name.removesuffix('.py')
if module_name in self.module_names:
Expand Down
4 changes: 4 additions & 0 deletions run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,10 @@ def test_tuple_unpacking():
check_code(source)


@pytest.mark.skipif(
sys.version_info < (3, 10),
reason='Skip A005, module testing is only supported in Python 3.10 and above',
)
def test_module_name():
source = ''
check_code(source, expected_codes='A005', filename='./temp/logging.py')
Expand Down

0 comments on commit 246537c

Please sign in to comment.