Skip to content

Commit

Permalink
Fix #10305: autodoc: Failed to extract optional forwardrefs
Browse files Browse the repository at this point in the history
Autodoc fails to extract optional forwardrefs (ex. `Optional[MyClass]`)
even if `MyClass` is declared in `autodoc_type_aliases`.
  • Loading branch information
tk0miya committed Apr 16, 2022
1 parent 301c7bd commit a66adb1
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Expand Up @@ -79,6 +79,8 @@ Bugs fixed
mixture of keyword only arguments with/without defaults
* #10310: autodoc: class methods are not documented when decorated with mocked
function
* #10305: autodoc: Failed to extract optional forward-ref'ed typehints correctly
via :confval:`autodoc_type_aliases`
* #10214: html: invalid language tag was generated if :confval:`language`
contains a country code (ex. zh_CN)
* #10236: html search: objects are duplicated in search result
Expand Down
6 changes: 6 additions & 0 deletions sphinx/util/inspect.py
Expand Up @@ -487,6 +487,12 @@ def __call__(self) -> None:
def __eq__(self, other: Any) -> bool:
return self.name == other

def __hash__(self) -> int:
return hash(self.name)

def __repr__(self) -> str:
return self.name


class TypeAliasModule:
"""Pseudo module class for autodoc_type_aliases."""
Expand Down
5 changes: 4 additions & 1 deletion tests/roots/test-ext-autodoc/target/autodoc_type_aliases.py
@@ -1,7 +1,7 @@
from __future__ import annotations

import io
from typing import overload
from typing import Optional, overload

myint = int

Expand All @@ -11,6 +11,9 @@
#: docstring
variable2 = None # type: myint

#: docstring
variable3: Optional[myint]


def read(r: io.BytesIO) -> io.StringIO:
"""docstring"""
Expand Down
14 changes: 14 additions & 0 deletions tests/test_ext_autodoc_configs.py
Expand Up @@ -1144,6 +1144,13 @@ def test_autodoc_type_aliases(app):
'',
' docstring',
'',
'',
'.. py:data:: variable3',
' :module: target.autodoc_type_aliases',
' :type: Optional[int]',
'',
' docstring',
'',
]

# define aliases
Expand Down Expand Up @@ -1208,6 +1215,13 @@ def test_autodoc_type_aliases(app):
'',
' docstring',
'',
'',
'.. py:data:: variable3',
' :module: target.autodoc_type_aliases',
' :type: Optional[myint]',
'',
' docstring',
'',
]


Expand Down
12 changes: 11 additions & 1 deletion tests/test_util_inspect.py
Expand Up @@ -7,11 +7,21 @@
import sys
import types
from inspect import Parameter
from typing import Optional

import pytest

from sphinx.util import inspect
from sphinx.util.inspect import TypeAliasNamespace, stringify_signature
from sphinx.util.inspect import TypeAliasForwardRef, TypeAliasNamespace, stringify_signature
from sphinx.util.typing import stringify


def test_TypeAliasForwardRef():
alias = TypeAliasForwardRef('example')
assert stringify(alias) == 'example'

alias = Optional[alias]
assert stringify(alias) == 'Optional[example]'


def test_TypeAliasNamespace():
Expand Down

0 comments on commit a66adb1

Please sign in to comment.