Skip to content

import_global does not count multiple leading dots in relative module names #73

Open

Description

@hakarrr

AI-assisted source investigation, reproduced on Linux / CPython 3.12.15 against current develop at d5cfbc599fd76ed749d50fc37ba41aa5da8d2efb. This is a local reproducer, not a reported production incident.

Relative import names with two or more leading dots fail, although one-dot imports work:

import sys
import tempfile
from pathlib import Path
from python_utils.import_ import import_global

with tempfile.TemporaryDirectory() as directory:
    package = Path(directory) / 'testrel'
    (package / 'nested').mkdir(parents=True)
    (package / '__init__.py').write_text('')
    (package / 'nested' / '__init__.py').write_text('')
    (package / 'helper.py').write_text('value = 42')
    sys.path.insert(0, directory)
    try:
        scope = {'__package__': 'testrel.nested'}
        import_global('..helper', globals_=scope, locals_={})
        # ModuleNotFoundError: No module named 'testrel.nested.'
    finally:
        sys.path.remove(directory)

I expected this to import from testrel.helper and set scope['value'] to 42. The implementation removes exactly one leading empty name part and sets level = 1, leaving an extra leading dot for deeper relative names.

A private candidate counts all leading dots and removes the corresponding name parts. Its two-dot and three-dot regression rows fail on the original source; the one-dot control passes. Absolute imports and the existing explicit-level behavior are left unchanged. I checked the related #29/#46/#52/#54/#66 discussions and did not find this case covered by their compatibility decisions or the current open work.

Would a focused fix for deeper relative import names be welcome? The contribution guide recommends discussing behavior changes, so I am asking before publishing a code PR. No implementation or PR has been published, this is not an assignment claim, and no production/security impact is asserted.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions