Skip to content

Commit

Permalink
Merge pull request #155 from KozyrevIvan/exempt_modules_template
Browse files Browse the repository at this point in the history
Added the ability to skip a module by template
  • Loading branch information
sondrelg authored Mar 28, 2023
2 parents 33f0775 + 1b8c284 commit 5d261d4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
11 changes: 9 additions & 2 deletions flake8_type_checking/checker.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import ast
import fnmatch
import os
from ast import Index, literal_eval
from contextlib import suppress
Expand Down Expand Up @@ -547,6 +548,12 @@ def visit_If(self, node: ast.If) -> Any:

# -- Map imports -------------------------------

def is_exempt_module(self, module_name: str) -> bool:
"""Template module name check."""
return any(
{exempt_module for exempt_module in self.exempt_modules if fnmatch.fnmatch(module_name, exempt_module)}
)

def add_import(self, node: Import) -> None: # noqa: C901
"""Add relevant ast objects to import lists."""
if self.in_type_checking_block(node):
Expand All @@ -562,12 +569,12 @@ def add_import(self, node: Import) -> None: # noqa: C901
return None

# Skip checking the import if the module is passlisted.
if isinstance(node, ast.ImportFrom) and node.module in self.exempt_modules:
if isinstance(node, ast.ImportFrom) and node.module and self.is_exempt_module(node.module):
return

for name_node in node.names:
# Skip checking the import if the module is passlisted
if isinstance(node, ast.Import) and name_node.name in self.exempt_modules:
if isinstance(node, ast.Import) and self.is_exempt_module(name_node.name):
return

# Look for a TYPE_CHECKING import
Expand Down
20 changes: 20 additions & 0 deletions tests/test_exempt_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,23 @@ def test_exempt_modules_option():
)
assert _get_error(example3, error_code_filter='TC002') == {'2:0 ' + TC002.format(module='pandas')}
assert _get_error(example3, error_code_filter='TC002', type_checking_exempt_modules=['pandas']) == set()

# Check template Import
example4 = textwrap.dedent(
'''
from apps.app_1.choices import ExampleChoice
from apps.app_2.choices import Example2Choice
x: ExampleChoice
y: Example2Choice
'''
)
assert _get_error(example4, error_code_filter='TC002') == {
'2:0 ' + TC002.format(module='apps.app_1.choices.ExampleChoice'),
'3:0 ' + TC002.format(module='apps.app_2.choices.Example2Choice'),
}
assert _get_error(example4, error_code_filter='TC002', type_checking_exempt_modules=['apps.*.choices']) == set()
assert _get_error(example4, error_code_filter='TC002', type_checking_exempt_modules=['*.choices']) == set()
assert _get_error(example4, error_code_filter='TC002', type_checking_exempt_modules=['apps.app_1.*']) == {
'3:0 ' + TC002.format(module='apps.app_2.choices.Example2Choice'),
}

0 comments on commit 5d261d4

Please sign in to comment.