diff --git a/core/pyproject.toml b/core/pyproject.toml index 5eebb2668b..8bcd246f57 100644 --- a/core/pyproject.toml +++ b/core/pyproject.toml @@ -89,6 +89,7 @@ docs = [ "sphinx == 8.0.2", "sphinx_tabs == 3.4.5", "sphinx-autobuild == 2024.9.3", + "sphinx-autodoc-typehints == 2.1.1", "sphinx-csv-filter == 0.4.2", "sphinx-copybutton == 0.5.2", "sphinx-toolbox == 3.8.0", diff --git a/core/src/toga/types.py b/core/src/toga/types.py index b50553cb1f..bccea5e7b5 100644 --- a/core/src/toga/types.py +++ b/core/src/toga/types.py @@ -13,6 +13,10 @@ PositionT: TypeAlias = toga.Position | tuple[int, int] SizeT: TypeAlias = toga.Size | tuple[int, int] +else: + # sphinx-autodoc-typehints errors when these are gated with TYPE_CHECKING... + PositionT = None + SizeT = None class LatLng(NamedTuple): diff --git a/docs/conf.py b/docs/conf.py index 35a409a998..e544a0846a 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -32,6 +32,7 @@ "sphinx_tabs.tabs", "crate.sphinx.csv", "sphinx_copybutton", + "sphinx_autodoc_typehints", "sphinx_toolbox.more_autodoc.autonamedtuple", "sphinx_toolbox.more_autodoc.autoprotocol", "sphinx.ext.intersphinx", @@ -159,7 +160,7 @@ def autodoc_process_signature( linkcheck_ignore = [ # GitHub generates anchors in javascript r"https://github.com/.*#", - # References to Github issues/pulls should all be safe. + # References to GitHub issues/pulls should all be safe. r"^https://github.com/beeware/toga/issues/\d+$", r"^https://github.com/beeware/toga/pull/\d+$", ] @@ -377,3 +378,29 @@ def autodoc_process_signature( # If this is True, todolist produce output without file path and line, The default is False. # todo_link_only = False + + +# Disable WARNING: cannot cache unpickable configuration value: 'typehints_formatter' +suppress_warnings = ["config.cache"] + +always_use_bars_union = True + +from collections.abc import Iterable # noqa: E402 +from pathlib import Path # noqa: E402 +from typing import Optional, TypeVar, Union # noqa: E402 + +import toga # noqa: E402 + +md_none = "" + + +def typehints_formatter(annotation, config): + print(f"({type(annotation)}) {str(annotation)=}") + + return { + Union[str, Path, toga.Icon, None]: ":any:`IconContentT `", + toga.widgets.base.StyleT: ":any:`StyleT `", + Optional[toga.widgets.base.StyleT]: ":any:`StyleT ` | :any:`None`", + TypeVar("SourceT", bound=toga.sources.Source): "asdf", + Union[TypeVar("SourceT", bound=toga.sources.Source), Iterable, None]: "xcvb", + }.get(annotation, None)