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

Custom type hint doc formatting for TypeAlias, TypeVar #2645

Closed
wants to merge 4 commits into from
Closed
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
1 change: 1 addition & 0 deletions core/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions core/src/toga/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any idea why it only affects PositionT and SizeT, and not any of the other type aliases?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't look in to it too far yet....but I think it could be related to the circular import



class LatLng(NamedTuple):
Expand Down
29 changes: 28 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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+$",
]
Expand Down Expand Up @@ -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 <IconContentT>`",
toga.widgets.base.StyleT: ":any:`StyleT <StyleT>`",
Optional[toga.widgets.base.StyleT]: ":any:`StyleT <StyleT>` | :any:`None`",
TypeVar("SourceT", bound=toga.sources.Source): "asdf",
Union[TypeVar("SourceT", bound=toga.sources.Source), Iterable, None]: "xcvb",
}.get(annotation, None)
Loading