Currently save_precompiled gathers all modules in sys.modules and puts them in the repo folder. This causes irrelevant files to be pushed to hub just because they were imported by the script that called PrecompiledAgent.push_to_hub. This behavior is not ideal and can lead to unexpected behavior when called push_to_hub or save_precompiled from different scripts.
Proposed Solution
- We can use module finder by defining and
ModuleFinder that only looks at internal modules and ignores third party ones
class InternalOnlyFinder(ModuleFinder):
def __init__(self, root, *args, **kwargs):
super().__init__(*args, **kwargs)
self.root = Path(root).resolve()
def import_hook(self, name, caller=None, fromlist=None, level=None):
"""Only import modules whose source file lives inside self.root."""
...
...
and place a run_script in save_precompiled that inspects the file containing the subclass definition. This should find all modules used to import the script
def save_precompiled(path)
file_path = inspect.getfile(self.__class__)
finder = InternalOnlyFinder(project_root)
finder.run_script(file_path)
...
Caveat
While this solves the previous issue, it introduces another issue, dynamic importing. Any modules not imported at the top level won't run when the script is imported and therefore won't be included. However, this is a worthwhile sacrifice especially because third party packages are handled with an entirely different mechanism (pyproject.toml). Also not sure why anyone would dynamically import an internal module.
Currently
save_precompiledgathers all modules in sys.modules and puts them in the repo folder. This causes irrelevant files to be pushed to hub just because they were imported by the script that called PrecompiledAgent.push_to_hub. This behavior is not ideal and can lead to unexpected behavior when called push_to_hub or save_precompiled from different scripts.Proposed Solution
ModuleFinderthat only looks at internal modules and ignores third party onesand place a run_script in
save_precompiledthat inspects the file containing the subclass definition. This should find all modules used to import the scriptCaveat
While this solves the previous issue, it introduces another issue, dynamic importing. Any modules not imported at the top level won't run when the script is imported and therefore won't be included. However, this is a worthwhile sacrifice especially because third party packages are handled with an entirely different mechanism (pyproject.toml). Also not sure why anyone would dynamically import an internal module.