Merge pull request #10353 from tk0miya/10305_optional_forwardref_annotations

Fix #10305: autodoc: Failed to extract optional forwardrefs
This commit is contained in:
Takeshi KOMIYA
2022-05-03 02:03:28 +09:00
committed by GitHub
5 changed files with 37 additions and 2 deletions

View File

@@ -85,6 +85,8 @@ Bugs fixed
mixture of keyword only arguments with/without defaults mixture of keyword only arguments with/without defaults
* #10310: autodoc: class methods are not documented when decorated with mocked * #10310: autodoc: class methods are not documented when decorated with mocked
function 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` * #10214: html: invalid language tag was generated if :confval:`language`
contains a country code (ex. zh_CN) contains a country code (ex. zh_CN)
* #10236: html search: objects are duplicated in search result * #10236: html search: objects are duplicated in search result

View File

@@ -487,6 +487,12 @@ class TypeAliasForwardRef:
def __eq__(self, other: Any) -> bool: def __eq__(self, other: Any) -> bool:
return self.name == other return self.name == other
def __hash__(self) -> int:
return hash(self.name)
def __repr__(self) -> str:
return self.name
class TypeAliasModule: class TypeAliasModule:
"""Pseudo module class for autodoc_type_aliases.""" """Pseudo module class for autodoc_type_aliases."""

View File

@@ -1,7 +1,7 @@
from __future__ import annotations from __future__ import annotations
import io import io
from typing import overload from typing import Optional, overload
myint = int myint = int
@@ -11,6 +11,9 @@ variable: myint
#: docstring #: docstring
variable2 = None # type: myint variable2 = None # type: myint
#: docstring
variable3: Optional[myint]
def read(r: io.BytesIO) -> io.StringIO: def read(r: io.BytesIO) -> io.StringIO:
"""docstring""" """docstring"""

View File

@@ -1150,6 +1150,13 @@ def test_autodoc_type_aliases(app):
'', '',
' docstring', ' docstring',
'', '',
'',
'.. py:data:: variable3',
' :module: target.autodoc_type_aliases',
' :type: Optional[int]',
'',
' docstring',
'',
] ]
# define aliases # define aliases
@@ -1214,6 +1221,13 @@ def test_autodoc_type_aliases(app):
'', '',
' docstring', ' docstring',
'', '',
'',
'.. py:data:: variable3',
' :module: target.autodoc_type_aliases',
' :type: Optional[myint]',
'',
' docstring',
'',
] ]

View File

@@ -7,11 +7,21 @@ import functools
import sys import sys
import types import types
from inspect import Parameter from inspect import Parameter
from typing import Optional
import pytest import pytest
from sphinx.util import inspect 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(): def test_TypeAliasNamespace():