Skip to content

Commit 2a6ebee

Browse files
committed
Add new option to import_tools.discover to allow side effects to be considered.
1 parent f9ea24d commit 2a6ebee

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

domdf_python_tools/import_tools.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,21 @@
5858
def discover(
5959
package: ModuleType,
6060
match_func: Optional[Callable[[Any], bool]] = None,
61+
exclude_side_effects: bool = True,
6162
) -> List[Type[Any]]:
6263
"""
6364
Returns a list of objects in the directory matched by ``match_func``.
6465
6566
:param package: A Python package
6667
:param match_func: Function taking an object and returning true if the object is to be included in the output.
6768
:default match_func: :py:obj:`None`, which includes all objects.
69+
:param exclude_side_effects: Don't include objects that are only there because of an import side effect.
6870
6971
:return: List of matching objects.
72+
73+
.. versionchanged:: 1.0.0
74+
75+
Added the ``exclude_side_effects`` parameter.
7076
"""
7177

7278
matched_classes = list()
@@ -82,12 +88,12 @@ def discover(
8288

8389
# Check all the functions in that module
8490
for _, imported_objects in inspect.getmembers(module, match_func):
85-
if not hasattr(imported_objects, "__module__"):
86-
continue
8791

88-
# Don't include things that are only there due to a side effect of importing
89-
if imported_objects.__module__ != module.__name__:
90-
continue
92+
if exclude_side_effects:
93+
if not hasattr(imported_objects, "__module__"):
94+
continue
95+
if imported_objects.__module__ != module.__name__:
96+
continue
9197

9298
matched_classes.append(imported_objects)
9399

0 commit comments

Comments
 (0)