From 770dba5506ecd96bf52ca47cd880b7b2ca62c8c7 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Wed, 14 Aug 2024 11:28:50 +0300 Subject: [PATCH] core.common: move away import related stuff to my.core.utils.imports moving without backward compatibility, since it's extremely unlikely they are used for any external modules in fact, unclear if these methods still have much value at all, but keeping for now just in case --- my/core/common.py | 31 ------------------------------- my/core/compat.py | 10 ++-------- my/core/hpi_compat.py | 2 +- my/core/utils/imports.py | 37 +++++++++++++++++++++++++++++++++++++ my/core/utils/itertools.py | 3 ++- my/demo.py | 2 +- tests/extra/polar.py | 2 +- 7 files changed, 44 insertions(+), 43 deletions(-) create mode 100644 my/core/utils/imports.py diff --git a/my/core/common.py b/my/core/common.py index 920657a..389dedc 100644 --- a/my/core/common.py +++ b/my/core/common.py @@ -34,37 +34,6 @@ from .compat import deprecated # some helper functions PathIsh = Union[Path, str] -# TODO only used in tests? not sure if useful at all. -def import_file(p: PathIsh, name: Optional[str] = None) -> types.ModuleType: - p = Path(p) - if name is None: - name = p.stem - import importlib.util - spec = importlib.util.spec_from_file_location(name, p) - assert spec is not None, f"Fatal error; Could not create module spec from {name} {p}" - foo = importlib.util.module_from_spec(spec) - loader = spec.loader; assert loader is not None - loader.exec_module(foo) - return foo - - -def import_from(path: PathIsh, name: str) -> types.ModuleType: - path = str(path) - try: - sys.path.append(path) - import importlib - return importlib.import_module(name) - finally: - sys.path.remove(path) - - -def import_dir(path: PathIsh, extra: str='') -> types.ModuleType: - p = Path(path) - if p.parts[0] == '~': - p = p.expanduser() # TODO eh. not sure about this.. - return import_from(p.parent, p.name + extra) - - from .logging import setup_logger, LazyLogger diff --git a/my/core/compat.py b/my/core/compat.py index d73c60c..7bbe509 100644 --- a/my/core/compat.py +++ b/my/core/compat.py @@ -131,12 +131,6 @@ else: if sys.version_info[:2] >= (3, 11): - from typing import assert_never + from typing import assert_never, assert_type, Never else: - from typing_extensions import assert_never - - -if sys.version_info[:2] >= (3, 11): - from typing import Never -else: - from typing_extensions import Never + from typing_extensions import assert_never, assert_type, Never diff --git a/my/core/hpi_compat.py b/my/core/hpi_compat.py index 61121de..3c567d9 100644 --- a/my/core/hpi_compat.py +++ b/my/core/hpi_compat.py @@ -101,7 +101,7 @@ Please install {' '.join(requires)} as PIP packages (see the corresponding READM def _get_dal(cfg, module_name: str): mpath = getattr(cfg, module_name, None) if mpath is not None: - from .common import import_dir + from .utils.imports import import_dir return import_dir(mpath, '.dal') else: diff --git a/my/core/utils/imports.py b/my/core/utils/imports.py new file mode 100644 index 0000000..efd8e9a --- /dev/null +++ b/my/core/utils/imports.py @@ -0,0 +1,37 @@ +import importlib +import importlib.util +from pathlib import Path +import sys +from typing import Optional +from types import ModuleType + +from ..common import PathIsh + + +# TODO only used in tests? not sure if useful at all. +def import_file(p: PathIsh, name: Optional[str] = None) -> ModuleType: + p = Path(p) + if name is None: + name = p.stem + spec = importlib.util.spec_from_file_location(name, p) + assert spec is not None, f"Fatal error; Could not create module spec from {name} {p}" + foo = importlib.util.module_from_spec(spec) + loader = spec.loader; assert loader is not None + loader.exec_module(foo) + return foo + + +def import_from(path: PathIsh, name: str) -> ModuleType: + path = str(path) + sys.path.append(path) + try: + return importlib.import_module(name) + finally: + sys.path.remove(path) + + +def import_dir(path: PathIsh, extra: str = '') -> ModuleType: + p = Path(path) + if p.parts[0] == '~': + p = p.expanduser() # TODO eh. not sure about this.. + return import_from(p.parent, p.name + extra) diff --git a/my/core/utils/itertools.py b/my/core/utils/itertools.py index cab4b2c..7046acf 100644 --- a/my/core/utils/itertools.py +++ b/my/core/utils/itertools.py @@ -105,12 +105,13 @@ else: def test_listify() -> None: + from ..compat import assert_type + @listify def it() -> Iterator[int]: yield 1 yield 2 res = it() - from typing_extensions import assert_type # TODO move to compat? assert_type(res, List[int]) assert res == [1, 2] diff --git a/my/demo.py b/my/demo.py index 75954d6..645be4f 100644 --- a/my/demo.py +++ b/my/demo.py @@ -23,7 +23,7 @@ class demo(user_config): def external_module(self): rpath = self.external if rpath is not None: - from .core.common import import_dir + from .core.utils.imports import import_dir return import_dir(rpath) import my.config.repos.external as m # type: ignore diff --git a/tests/extra/polar.py b/tests/extra/polar.py index b2bc562..b5858b6 100644 --- a/tests/extra/polar.py +++ b/tests/extra/polar.py @@ -15,7 +15,7 @@ def test_hpi(prepare: str) -> None: assert len(list(get_entries())) > 1 def test_orger(prepare: str, tmp_path: Path) -> None: - from my.core.common import import_from, import_file + from my.core.utils.imports import import_from, import_file om = import_file(ROOT / 'orger/modules/polar.py') # reload(om)