Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions importscan/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,18 +186,21 @@ def is_nonstr_iter(v: object) -> TypeIs[Iterable[IgnoreModule]]:
return hasattr(v, "__iter__")

if ignore is None:
ignore = []
ignores: list[IgnoreModule] = []
elif not is_nonstr_iter(ignore):
ignore = [ignore]
ignores = [ignore]
else:
# materialize one-shot iterables: the rules are inspected twice below
ignores = list(ignore)

# non-leading-dotted name absolute object name
str_ignores = [ign for ign in ignore if isinstance(ign, str)]
str_ignores = [ign for ign in ignores if isinstance(ign, str)]
# leading dotted name relative to scanned package
rel_ignores = [ign for ign in str_ignores if ign.startswith(".")]
# non-leading dotted names
abs_ignores = [ign for ign in str_ignores if not ign.startswith(".")]
# functions, e.g. re.compile('pattern').search
callable_ignores = [ign for ign in ignore if callable(ign)]
callable_ignores = [ign for ign in ignores if callable(ign)]

def is_ignored(fullname: str) -> bool:
for ign in rel_ignores:
Expand Down
8 changes: 8 additions & 0 deletions importscan/tests/test_importscan.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ def test_ignore_module_function() -> None:
assert fixtures.calls == 0


def test_ignore_module_function_in_iterator() -> None:
from .fixtures import ignore_module_function

scan(ignore_module_function, ignore=iter([re.compile("module$").search]))

assert fixtures.calls == 0


def test_ignore_subpackage_relative() -> None:
from .fixtures import ignore_subpackage

Expand Down