Repository: vfdev-5/py_config_runner

Issue:
Simplify load_module method
Check if we could use 
```python
from importlib.machinery import SourceFileLoader

def load_module(fpath):
    return SourceFileLoader('mymodule', fpath).load_module()
```
instead of current implementation

Discussion:

Patch:
diff --git a/mypy.ini b/mypy.ini
index fa56557..53500c4 100644
--- a/mypy.ini
+++ b/mypy.ini
@@ -5,3 +5,6 @@ show_error_codes = True
 
 [mypy-py_config_runner.deprecated.*]
 ignore_errors = True
+
+[mypy-torch.*]
+ignore_missing_imports = True
diff --git a/py_config_runner/utils.py b/py_config_runner/utils.py
index 370c343..9d23fe7 100644
--- a/py_config_runner/utils.py
+++ b/py_config_runner/utils.py
@@ -1,4 +1,4 @@
-import importlib.util
+from importlib.machinery import SourceFileLoader
 import sys
 
 from collections.abc import MutableMapping
@@ -24,15 +24,10 @@ def load_module(filepath: Union[str, Path]) -> Any:
     if not filepath.exists():
         raise ValueError(f"File '{filepath.as_posix()}' is not found")
 
-    module_name = filepath.stem
-    if module_name in sys.modules:
-        module_name += "__py_config_runner"
-    spec = importlib.util.spec_from_file_location(module_name, filepath)
-    module = importlib.util.module_from_spec(spec)
-    # required by pydantic to work: https://github.com/samuelcolvin/pydantic/issues/2363
-    sys.modules[module_name] = module
-    spec.loader.exec_module(module)  # type: ignore[union-attr]
-    return module
+    if not filepath.is_file():
+        raise ValueError(f"Path '{filepath.as_posix()}' should be a file")
+
+    return SourceFileLoader(filepath.stem, filepath.as_posix()).load_module()  # type: ignore[call-arg]
 
 
 class ConfigObject(MutableMapping):


Test patch:
diff --git a/tests/test_runner.py b/tests/test_runner.py
index e6d618d..24b473a 100644
--- a/tests/test_runner.py
+++ b/tests/test_runner.py
@@ -4,8 +4,8 @@ from py_config_runner.runner import run_script, _check_script
 import pytest
 
 
-def test_run_script_script_file_no_run(dirname):  # noqa: F811
-    script_fp = dirname / "script.py"
+def test_run_script_script_file_no_run(dirname, config_filepath):  # noqa: F811
+    script_fp = dirname / "script_no_run.py"
 
     s = """
 import numpy as np
@@ -19,11 +19,11 @@ b = np.array([1, 2, 3])
         h.write(s)
 
     with pytest.raises(RuntimeError, match="should contain a method run"):
-        run_script(script_fp, "")
+        run_script(script_fp, config_filepath)
 
 
-def test_run_script_script_file_run_not_callable(dirname):  # noqa: F811
-    script_fp = dirname / "script.py"
+def test_run_script_script_file_run_not_callable(dirname, config_filepath):  # noqa: F811
+    script_fp = dirname / "script_run_not_callable.py"
 
     s = """
 run = 0
@@ -33,11 +33,11 @@ run = 0
         h.write(s)
 
     with pytest.raises(RuntimeError, match="should be a callable function"):
-        run_script(script_fp, "")
+        run_script(script_fp, config_filepath)
 
 
 def test_run_script_script_file_run_wrong_signature(dirname):  # noqa: F811
-    script_fp = dirname / "script.py"
+    script_fp = dirname / "script_wrong_signature.py"
 
     s = """
 def run():
diff --git a/tests/test_utils.py b/tests/test_utils.py
index ef5572a..3016fd1 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -68,14 +68,6 @@ def test_config_object_repr(config_filepath):
     assert "_data" in out
 
 
-def test_load_module_inexisting_file():
-
-    filepath = "/tmp/tmp.py"
-
-    with pytest.raises(ValueError, match="is not found"):
-        load_module(filepath)
-
-
 def test_load_module(dirname):
     import numpy as np
 
@@ -97,3 +89,11 @@ b = np.array([1, 2, 3])
 
     assert "b" in custom_module.__dict__
     assert np.all(custom_module.b == np.array([1, 2, 3]))
+
+
+def test_load_module_wrong_args():
+    with pytest.raises(ValueError, match=r"is not found"):
+        load_module("/tmp/abcdef")
+
+    with pytest.raises(ValueError, match=r"should be a file"):
+        load_module("/tmp/")