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

autodoc_type_aliases does not support dotted name #8588

Closed
tk0miya opened this issue Dec 26, 2020 · 1 comment
Closed

autodoc_type_aliases does not support dotted name #8588

tk0miya opened this issue Dec 26, 2020 · 1 comment

Comments

@tk0miya
Copy link
Member

tk0miya commented Dec 26, 2020

Describe the bug
autodoc_type_aliases does not support dotted name

To Reproduce

# helloworld.py
from __future__ import annotations  # important!
import struct

def consume_struct(_: struct.Struct) -> None:
    pass
# conf.py
autodoc_type_aliases = {
    'struct.Struct': 'struct.Struct',
}

The autodoc_type_aliases entry does not work as expected.
(refs: #8315)

Expected behavior
Supporting

Your project
No

Screenshots
No

Environment info

  • OS: Mac
  • Python version: 3.8.2
  • Sphinx version: HEAD of 3.x
  • Sphinx extensions: sphinx.ext.autodoc
  • Extra tools: No

Additional context
No

@tk0miya tk0miya added this to the 3.5.0 milestone Dec 26, 2020
@tk0miya tk0miya modified the milestones: 3.5.0, 4.0.0 Feb 4, 2021
@shimizukawa
Copy link
Member

quick hacking...

>>> class ModuleProxy:
...     def __init__(self, module_name, mapper):
...         self._mod = __import__(module_name)
...         self._map = mapper
...     def __getattr__(self, key):
...         if key in self._map:
...             return self._map[key]
...         return getattr(self._mod, key)
...
>>> autodoc_type_aliases = {
...    'struct': ModuleProxy('struct', {'Struct': 'struct.Struct'}),
... }
>>>
>>> from __future__ import annotations  # important!
>>> import struct
>>>
>>> def consume_struct(_: struct.Struct) -> struct.error:
...     pass
...
>>> typing.get_type_hints(consume_struct, localns=magical)
{'_': ForwardRef('struct.Struct'), 'return': <class 'struct.error'>}

... this can be applied if Sphinx knows that struct is a module.

@tk0miya tk0miya modified the milestones: 4.0.0, 4.1.0 Apr 17, 2021
tk0miya added a commit to tk0miya/sphinx that referenced this issue May 3, 2021
…name

It allows users to define an alias for a class with module name
like `foo.bar.BazClass`.
tk0miya added a commit to tk0miya/sphinx that referenced this issue May 3, 2021
…name

It allows users to define an alias for a class with module name
like `foo.bar.BazClass`.
@tk0miya tk0miya closed this as completed in 28ab5f2 May 3, 2021
tk0miya added a commit that referenced this issue May 3, 2021
Fix #8588: autodoc_type_aliases does not support dotted name
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants