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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 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 @@ -91,6 +91,7 @@ docs = [
# Sphinx 2024.2.4 deprecated support for Python 3.8
"sphinx-autobuild == 2021.3.14 ; python_version < '3.9'",
"sphinx-autobuild == 2024.4.16 ; python_version >= '3.9'",
"sphinx-autodoc-typehints == 2.1.1",
"sphinx-csv-filter == 0.4.1",
"sphinx-copybutton == 0.5.2",
"sphinx-toolbox == 3.5.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
21 changes: 21 additions & 0 deletions 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 @@ -372,3 +373,23 @@ 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 pathlib import Path # noqa: E402
from typing import Union # noqa: E402

import toga # noqa: E402


def typehints_formatter(annotation, config):
# print(f"({type(annotation)}) {str(annotation)=}")
if annotation == Union[str, Path, toga.Icon, None]:
return ":any:`IconContentT <IconContentT>`"
elif annotation == toga.widgets.base.StyleT:
return "``StyleT``"
return None
Loading