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

Fix issue #481 #533

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/sphinx_autodoc_typehints/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
import textwrap
import types
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any, AnyStr, ForwardRef, NewType, TypeVar, get_type_hints
from typing import TYPE_CHECKING, Any, AnyStr, ForwardRef, NewType, TypeVar, Union, get_type_hints

from docutils import nodes
from docutils.frontend import get_default_settings
from sphinx.ext.autodoc.mock import mock
from sphinx.parsers import RSTParser
from sphinx.util import logging, rst
from sphinx.util.inspect import TypeAliasForwardRef, TypeAliasNamespace, stringify_signature
from sphinx.util.inspect import TypeAliasForwardRef, stringify_signature
from sphinx.util.inspect import signature as sphinx_signature

from ._parser import parse
Expand Down Expand Up @@ -61,6 +61,11 @@
_TYPES_DICT[types.FunctionType] = "FunctionType"


class MyTypeAliasForwardRef(TypeAliasForwardRef):
def __or__(self, value: Any) -> Any:
return Union[self, value] # noqa: UP007


def _get_types_type(obj: Any) -> str | None:
try:
return _TYPES_DICT.get(obj)
Expand Down Expand Up @@ -446,7 +451,7 @@ def _future_annotations_imported(obj: Any) -> bool:


def get_all_type_hints(
autodoc_mock_imports: list[str], obj: Any, name: str, localns: TypeAliasNamespace
autodoc_mock_imports: list[str], obj: Any, name: str, localns: dict[Any, MyTypeAliasForwardRef]
) -> dict[str, Any]:
result = _get_type_hint(autodoc_mock_imports, name, obj, localns)
if not result:
Expand Down Expand Up @@ -517,7 +522,9 @@ def _resolve_type_guarded_imports(autodoc_mock_imports: list[str], obj: Any) ->
_execute_guarded_code(autodoc_mock_imports, obj, module_code)


def _get_type_hint(autodoc_mock_imports: list[str], name: str, obj: Any, localns: TypeAliasNamespace) -> dict[str, Any]:
def _get_type_hint(
autodoc_mock_imports: list[str], name: str, obj: Any, localns: dict[Any, MyTypeAliasForwardRef]
) -> dict[str, Any]:
_resolve_type_guarded_imports(autodoc_mock_imports, obj)
try:
result = get_type_hints(obj, None, localns)
Expand Down Expand Up @@ -689,7 +696,7 @@ def process_docstring( # noqa: PLR0913, PLR0917
except (ValueError, TypeError):
signature = None

localns = TypeAliasNamespace(app.config["autodoc_type_aliases"])
localns = {key: MyTypeAliasForwardRef(value) for key, value in app.config["autodoc_type_aliases"].items()}
type_hints = get_all_type_hints(app.config.autodoc_mock_imports, obj, name, localns)
app.config._annotation_globals = getattr(obj, "__globals__", {}) # noqa: SLF001
try:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_integration_autodoc_type_aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def g(s: AliasedClass) -> AliasedClass:
Function docstring.

Parameters:
* **x** (Array) -- foo
* **x** ("Optional"[Array]) -- foo

* **y** ("Schema") -- boo

Expand All @@ -115,7 +115,7 @@ def g(s: AliasedClass) -> AliasedClass:

""",
)
def function(x: ArrayLike, y: Schema) -> str:
def function(x: ArrayLike | None, y: Schema) -> str:
"""
Function docstring.

Expand Down