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 isn't able to resolve struct.Struct type annotations #8315

Closed
anselor opened this issue Oct 14, 2020 · 2 comments
Closed

autodoc isn't able to resolve struct.Struct type annotations #8315

anselor opened this issue Oct 14, 2020 · 2 comments

Comments

@anselor
Copy link

anselor commented Oct 14, 2020

Describe the bug
If struct.Struct is declared in any type annotations, I get class reference target not found: Struct

To Reproduce
Simple index.rst

Hello World
===========

code docs
=========

.. automodule:: helloworld.helloworld

Simple helloworld.py

import struct
import pathlib

def consume_struct(_: struct.Struct) -> None:
    pass

def make_struct() -> struct.Struct:
    mystruct = struct.Struct('HH')
    return mystruct

def make_path() -> pathlib.Path:
    return pathlib.Path()

Command line:

python3 -m sphinx -b html docs/ doc-out -nvWT

Expected behavior
If you comment out the 2 functions that have Struct type annotations, you'll see that pathlib.Path resolves fine and shows up in the resulting documentation. I'd expect that Struct would also resolve correctly.

Your project
n/a

Screenshots
n/a

Environment info

  • OS: Ubuntu 18.04, 20.04
  • Python version: 3.8.2
  • Sphinx version: 3.2.1
  • Sphinx extensions: 'sphinx.ext.autodoc',
    'sphinx.ext.autosectionlabel',
    'sphinx.ext.intersphinx',
    'sphinx.ext.doctest',
    'sphinx.ext.todo'
  • Extra tools:

Additional context

  • [e.g. URL or Ticket]
@tk0miya
Copy link
Member

tk0miya commented Dec 26, 2020

Unfortunately, the struct.Struct class does not have the correct module-info. So it is difficult to support.

Python 3.8.2 (default, Mar  2 2020, 00:44:41)
[Clang 11.0.0 (clang-1100.0.33.17)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import struct
>>> struct.Struct.__module__
'builtins'

Note: In python3.9, it returns the correct module-info. But it answers the internal module name: _struct.

Python 3.9.1 (default, Dec 18 2020, 00:18:40)
[Clang 11.0.3 (clang-1103.0.32.59)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import struct
>>> struct.Struct.__module__
'_struct'

So it would better to use autodoc_type_aliases to correct it forcedly.

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

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

Then, it working fine.

tk0miya added a commit to tk0miya/sphinx that referenced this issue Dec 31, 2020
…notation

The builtin module, ``struct.Struct`` does not have correct module
name since Python 3.8.  This allows to refer it automatically.
@tk0miya tk0miya added this to the 3.5.0 milestone Dec 31, 2020
@tk0miya
Copy link
Member

tk0miya commented Dec 31, 2020

I think it is not good to do a workaround because of a bug of pythons. I think it's better to add a workaround to Sphinx side. Then, nobody will do it on each project. #8627 will resolve this automatically.

@tk0miya tk0miya closed this as completed in f1a051f Jan 1, 2021
tk0miya added a commit that referenced this issue Jan 1, 2021
Fix #8315: autodoc: Failed to resolve struct.Struct type annotation
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 16, 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