From 9e98278398d9a4b325e1b45f9f2dc42c2c0e3022 Mon Sep 17 00:00:00 2001 From: generaltony007 <191973953+hakarrr@users.noreply.github.com> Date: Mon, 9 Nov 2026 00:59:26 +0530 Subject: [PATCH 1/2] Fix import_global for multi-dot relative imports (closes #73) Count every leading dot to derive the relative import level, so deeper relative names such as ..helper and ...helper resolve correctly instead of raising ModuleNotFoundError. Absolute imports and the explicit-level path are unchanged. Regression tests cover two-dot, three-dot, one-dot control and the dots-only edge. Co-authored-by: generaltony007 <144252170+GeneralTony007@users.noreply.github.com> --- python_utils/import_.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/python_utils/import_.py b/python_utils/import_.py index 642b4ae..5223f93 100644 --- a/python_utils/import_.py +++ b/python_utils/import_.py @@ -113,10 +113,14 @@ def import_global( # noqa: C901 globals_ = frame.f_globals try: - # Relative imports are supported (from .spam import eggs) + # Relative imports are supported (from .spam import eggs). Each + # leading dot counts as one level, so ``..spam`` goes up two. if not name_parts[0]: - name_parts = name_parts[1:] - level = 1 + dots: int = 0 + while dots < len(name_parts) and not name_parts[dots]: + dots += 1 + level = dots + name_parts = name_parts[dots:] or [''] # raise IOError((name, level)) module = __import__( From 6637846fa4658b90ac2d13ce9198a19ec17e076e Mon Sep 17 00:00:00 2001 From: generaltony007 <191973953+hakarrr@users.noreply.github.com> Date: Mon, 9 Nov 2026 00:59:53 +0530 Subject: [PATCH 2/2] Add regression tests for multi-dot relative imports (#73) Two-dot and three-dot relative imports fail before the fix and pass after; a one-dot control and the dots-only edge keep their behavior. Uses a tmp_path package fixture, matching the existing nested_module pattern. Co-authored-by: generaltony007 <144252170+GeneralTony007@users.noreply.github.com> --- _python_utils_tests/test_import.py | 61 ++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/_python_utils_tests/test_import.py b/_python_utils_tests/test_import.py index b8e1dea..0227034 100644 --- a/_python_utils_tests/test_import.py +++ b/_python_utils_tests/test_import.py @@ -299,3 +299,64 @@ def lookup(name: str) -> str: import_.import_global(module_name, ['eggs'], locals_={}, globals_=scope) assert scope == {} assert 'eggs' not in calls + + +@pytest.fixture +def deep_package( + tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch +) -> collections.abc.Iterator[str]: + """Create ``testrel.nested.deeper`` with importable helper modules.""" + package: pathlib.Path = tmp_path / 'testrel' + nested: pathlib.Path = package / 'nested' + deeper: pathlib.Path = nested / 'deeper' + deeper.mkdir(parents=True) + (package / '__init__.py').write_text('', encoding='utf-8') + (nested / '__init__.py').write_text('', encoding='utf-8') + (deeper / '__init__.py').write_text('', encoding='utf-8') + (package / 'helper.py').write_text('value = 42\n', encoding='utf-8') + (nested / 'inner.py').write_text( + "inner_value = 'inner'\n", encoding='utf-8' + ) + monkeypatch.setattr(sys, 'path', [str(tmp_path), *sys.path]) + + yield 'testrel' + + imported: list[str] = [ + name for name in sys.modules if name.split('.')[0] == 'testrel' + ] + for name in imported: + del sys.modules[name] + + +def test_import_globals_relative_import_two_dots(deep_package: str) -> None: + """Resolve ``..helper`` two levels up, to ``testrel.helper`` (#73).""" + locals_: types.Dict[str, types.Any] = {} + globals_: types.Dict[str, types.Any] = {'__package__': 'testrel.nested'} + import_.import_global('..helper', locals_=locals_, globals_=globals_) + assert globals_['value'] == 42 + + +def test_import_globals_relative_import_three_dots(deep_package: str) -> None: + """Resolve ``...helper`` three levels up, to ``testrel.helper`` (#73).""" + locals_: types.Dict[str, types.Any] = {} + globals_: types.Dict[str, types.Any] = { + '__package__': 'testrel.nested.deeper' + } + import_.import_global('...helper', locals_=locals_, globals_=globals_) + assert globals_['value'] == 42 + + +def test_import_globals_relative_import_one_dot(deep_package: str) -> None: + """Keep resolving a single leading dot one level up (#73 control).""" + locals_: types.Dict[str, types.Any] = {} + globals_: types.Dict[str, types.Any] = {'__package__': 'testrel.nested'} + import_.import_global('.inner', locals_=locals_, globals_=globals_) + assert globals_['inner_value'] == 'inner' + + +def test_import_globals_relative_import_dots_only(deep_package: str) -> None: + """Reject a dots-only name, which names no module to import (#73).""" + locals_: types.Dict[str, types.Any] = {} + globals_: types.Dict[str, types.Any] = {'__package__': 'testrel.nested'} + with pytest.raises(ImportError): + import_.import_global('..', locals_=locals_, globals_=globals_)