Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Viewcode: Fix issue with import paths that differ from the directory structure #13195

Open
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

ProGamerGov
Copy link
Contributor

@ProGamerGov ProGamerGov commented Dec 26, 2024

This PR fixes an issue that occurs when, the module import path used by users does not match the directory / file structure. In this case, the import_lib's import_module function cannot find the correct path.

This issue results in code docs missing the associated viewcode link when the extension is used.

My simple solution utilizes importlib's __import__ function to find the actual directory module path to the import.

Feature / Bugfix

  • Bugfix

Purpose

If the module import path used by users does not match the directory / file structure, import_lib's import_module function cannot find it.

Below is a basic example of where this failure occurs:

package/
    `__init__.py` # import package.subpackage1 as subpackage1
    subpackage1/
        `__init__.py` # from package.subpackage1.subpackage2 import module_1
        subpackge2/
            `__init__.py`
            module_1.py

Using the above directory / file structure, users import module1 via:

import package.subpackage1.module1

Sphinx is generally able to handle this sort of structure, except for the viewcode extension that uses the get_full_modname function from here: https://github.com/sphinx-doc/sphinx/blob/5.x/sphinx/util/__init__.py

That function specifically causes an error with the above example when importlib's import_module is run:

from importlib import import_module
module = import_module("package.subpackage1.module1") # Fails
# ModuleNotFoundError: No module named 'package.subpackage1.module1'

The exact path to the module however has no error:

from importlib import import_module
module = import_module("package.subpackage1.subpackage2.module1") # Works

Relates

A remake of this PR:
#10766

CC: @AA-Turner

@ProGamerGov ProGamerGov changed the title Viewcode: Fix import paths that differ from the directory structure Viewcode: Fix issue with import paths that differ from the directory structure Dec 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant