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

Merged
merged 38 commits into from
Jan 5, 2025

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/master/sphinx/ext/viewcode.py#L50

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
@AA-Turner AA-Turner added the extensions:viewcode The `sphinx.ext.linkcode` or `sphinx.ext.viewcode` extensions label Jan 5, 2025
@ProGamerGov ProGamerGov requested a review from AA-Turner January 5, 2025 03:05
@AA-Turner
Copy link
Member

Hi Ben,

I've had to make several changes here. I've split into individual commits so hopefully you can follow what has happened & it makes sense. Thank you for re-making this from #10766 (which I have closed).

A

@AA-Turner AA-Turner added this to the 8.2.0 milestone Jan 5, 2025
@AA-Turner AA-Turner merged commit fec4d7c into sphinx-doc:master Jan 5, 2025
22 checks passed
@ProGamerGov
Copy link
Contributor Author

@AA-Turner Awesome, your changes look good to me!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 3, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
extensions:viewcode The `sphinx.ext.linkcode` or `sphinx.ext.viewcode` extensions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants