Skip to content

Commit

Permalink
Unify get_full_modname
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner committed Jul 28, 2023
1 parent 762ed85 commit 58585a7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
22 changes: 18 additions & 4 deletions sphinx/ext/viewcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import posixpath
import traceback
from importlib import import_module
from os import path
from typing import TYPE_CHECKING, Any, cast

Expand All @@ -19,7 +20,7 @@
from sphinx.locale import _, __
from sphinx.pycode import ModuleAnalyzer
from sphinx.transforms.post_transforms import SphinxPostTransform
from sphinx.util import get_full_modname, logging
from sphinx.util import logging
from sphinx.util.display import status_iterator
from sphinx.util.nodes import make_refnode

Expand All @@ -41,9 +42,22 @@ class viewcode_anchor(Element):
"""


def _get_full_modname(app: Sphinx, modname: str, attribute: str) -> str | None:
def _get_full_modname(modname: str, attribute: str) -> str | None:
try:
return get_full_modname(modname, attribute)
if modname is None:
# Prevents a TypeError: if the last getattr() call will return None
# then it's better to return it directly
return None
module = import_module(modname)

# Allow an attribute to have multiple parts and incidentally allow
# repeated .s in the attribute.
value = module
for attr in attribute.split('.'):
if attr:
value = getattr(value, attr)

return getattr(value, '__module__', None)
except AttributeError:
# sphinx.ext.viewcode can't follow class instance attribute
# then AttributeError logging output only verbose mode.
Expand Down Expand Up @@ -118,7 +132,7 @@ def has_tag(modname: str, fullname: str, docname: str, refname: str) -> bool:
'viewcode-follow-imported', modname, fullname,
)
if not new_modname:
new_modname = _get_full_modname(app, modname, fullname)
new_modname = _get_full_modname(modname, fullname)
modname = new_modname
if not modname:
continue
Expand Down
17 changes: 0 additions & 17 deletions sphinx/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,23 +148,6 @@ def merge_other(self, docnames: set[str], other: dict[str, tuple[set[str], Any]]
self.add_file(docname, filename)


def get_full_modname(modname: str, attribute: str) -> str | None:
if modname is None:
# Prevents a TypeError: if the last getattr() call will return None
# then it's better to return it directly
return None
module = import_module(modname)

# Allow an attribute to have multiple parts and incidentally allow
# repeated .s in the attribute.
value = module
for attr in attribute.split('.'):
if attr:
value = getattr(value, attr)

return getattr(value, '__module__', None)


# a regex to recognize coding cookies
_coding_re = re.compile(r'coding[:=]\s*([-\w.]+)')

Expand Down

0 comments on commit 58585a7

Please sign in to comment.