Skip to content

Commit

Permalink
Merge pull request #13 from IBM/GetTrackedImports
Browse files Browse the repository at this point in the history
GetTrackedImports: Add get_tracked_modules with tests
  • Loading branch information
gabe-l-hart authored Dec 23, 2021
2 parents d3fc980 + cfd2f8c commit 033dd0a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions import_tracker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
default_import_mode,
get_required_imports,
get_required_packages,
get_tracked_modules,
import_module,
set_static_tracker,
)
Expand Down
8 changes: 8 additions & 0 deletions import_tracker/import_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@ def get_required_packages(name: str) -> List[str]:
return sorted(list(required_pkgs))


def get_tracked_modules(prefix: str = "") -> List[str]:
"""Get all tracked modules whose name starts with the given prefix.
Libraries which implement static tracking can use this to determine the full
set of tracked modules in a central location.
"""
return [name for name in _module_dep_mapping if name.startswith(prefix)]


@contextmanager
def default_import_mode(import_mode: str):
"""This contextmanager will set the default import mode and then reset it on
Expand Down
29 changes: 29 additions & 0 deletions test/test_import_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,35 @@ def test_get_required_packages_no_package_lookup():
assert len(warns) == 1


## get_tracked_modules #########################################################


def test_get_tracked_modules_prefix(BEST_EFFORT_MODE):
"""Test that get_tracked_modules returns all tracked modules for the given
prefix
"""
# Local
import sample_lib

assert import_tracker.get_tracked_modules("sample_lib.nested") == [
"sample_lib.nested.submod3"
]


def test_get_tracked_modules_no_prefix(BEST_EFFORT_MODE):
"""Test that get_tracked_modules returns all tracked modules when no prefix
is given
"""
# Local
import sample_lib

assert import_tracker.get_tracked_modules() == [
"sample_lib.nested.submod3",
"sample_lib.submod1",
"sample_lib.submod2",
]


## import_module ###############################################################

#################
Expand Down

0 comments on commit 033dd0a

Please sign in to comment.