Skip to content

Commit e74cc58

Browse files
committed
Add module name rewrite configuration option
This adds a configuration option that allows the user to rewrite module names if needed. Resolves #473. Note that we also rewrite the names of `_io` and `typing_extensions` internally. One benefit of this hook is that if other similar rewrites are needed, users will be able to immediately add them without having to patch sphinx_autodoc_typehints itself. One disadvantage is that by default, having a function in the config prevents caching. I think this can be handled with the following slightly inelegant hack: ```py class ModuleNameRewriteHook: version: int def __eq__(self, other): return type(self) == type(other) and self.version == other.version def __init__(self): self.version = 2 def __call__(self, module): # logic here # Make sure to bump version if you edit this so that sphinx will rerun. return module typehints_fixup_module_name = ModuleNameRewriteHook ``` See sphinx-doc/sphinx#12300.
1 parent fdad0d7 commit e74cc58

11 files changed

+66
-7
lines changed

src/sphinx_autodoc_typehints/__init__.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,18 @@ def format_internal_tuple(t: tuple[Any, ...], config: Config) -> str:
169169
return f"({', '.join(fmt)})"
170170

171171

172+
def fixup_module_name(config: Config, module: str) -> str:
173+
if config.typehints_fixup_module_name:
174+
module = config.typehints_fixup_module_name(module)
175+
176+
if module == "typing_extensions":
177+
module = "typing"
178+
179+
if module == "_io":
180+
module = "io"
181+
return module
182+
183+
172184
def format_annotation(annotation: Any, config: Config) -> str: # noqa: C901, PLR0911, PLR0912, PLR0915, PLR0914
173185
"""
174186
Format the annotation.
@@ -204,13 +216,7 @@ def format_annotation(annotation: Any, config: Config) -> str: # noqa: C901, PL
204216
except ValueError:
205217
return str(annotation).strip("'")
206218

207-
# Redirect all typing_extensions types to the stdlib typing module
208-
if module == "typing_extensions":
209-
module = "typing"
210-
211-
if module == "_io":
212-
module = "io"
213-
219+
module = fixup_module_name(config, module)
214220
full_name = f"{module}.{class_name}" if module != "builtins" else class_name
215221
fully_qualified: bool = getattr(config, "typehints_fully_qualified", False)
216222
prefix = "" if fully_qualified or full_name == class_name else "~"
@@ -967,6 +973,7 @@ def setup(app: Sphinx) -> dict[str, bool]:
967973
app.add_config_value("typehints_formatter", None, "env")
968974
app.add_config_value("typehints_use_signature", False, "env") # noqa: FBT003
969975
app.add_config_value("typehints_use_signature_return", False, "env") # noqa: FBT003
976+
app.add_config_value("typehints_fixup_module_name", None, "env")
970977
app.add_role("sphinx_autodoc_typehints_type", sphinx_autodoc_typehints_type_role)
971978
app.connect("env-before-read-docs", validate_config) # config may be changed after “config-inited” event
972979
app.connect("autodoc-process-signature", process_signature)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from __future__ import annotations
2+
3+
from wrong_module_path import A, f
4+
5+
__all__ = ["A", "f"]

tests/roots/test-dummy/future_annotations.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
:orphan:
2+
13
Dummy Module
24
============
35

tests/roots/test-dummy/simple.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
:orphan:
2+
13
Simple Module
24
=============
35

tests/roots/test-dummy/simple_default_role.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
:orphan:
2+
13
Simple Module
24
=============
35

tests/roots/test-dummy/simple_no_use_rtype.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
:orphan:
2+
13
Simple Module
24
=============
35

tests/roots/test-dummy/without_complete_typehints.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
:orphan:
2+
13
Simple Module
24
=============
35

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from __future__ import annotations
2+
3+
4+
class A:
5+
pass
6+
7+
8+
def f() -> A:
9+
pass
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
:orphan:
2+
3+
.. class:: export_module.A
4+
5+
.. autofunction:: export_module.f

tests/test_integration.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,6 +1373,7 @@ def test_integration(
13731373
(Path(app.srcdir) / "index.rst").write_text(template.format(val.__name__))
13741374
app.config.__dict__.update(configs[conf_run])
13751375
app.config.__dict__.update(val.OPTIONS)
1376+
app.config.nitpicky = True
13761377
monkeypatch.setitem(sys.modules, "mod", sys.modules[__name__])
13771378
app.build()
13781379
assert "build succeeded" in status.getvalue() # Build succeeded

0 commit comments

Comments
 (0)