Skip to content

Commit bfaf30d

Browse files
committed
fix(docs): remove typehints_formatter.
By removing the `typehints_formatter` we are able to resolve issues #570 and #734. The former issue is resolved by removing the non-picklable function, which enables build caching, and the second is solved by removing the `TypeVar` bound replacement. While the `TypeVar` replacement does provide more concise documentation, it removes a key piece of information, which the reader must be aware of. For example, in: ```python T = TypeVar("T", bound=Data) def typevar(a: T, b: T) -> None: ... def bound(a: Data, b: Data): ... def valid(a: SupervisedData, b: SupervisedData): ... def invalid(a: Data, b: SupervisedData): ... ``` the `typevar` function is the actual specification, `bound` is the documented specification, `valid` is a valid specification, and `invalid` is an invalid specification which is presented as valid in the documentation (if we replace the `TypeVar` with the bound). As an additional comment, we do now lose the functionality of the `autodoc_custom_types` -- it should be noted that the `ArrayLike` custom type (the only listed custom type) doesn't appear to be used anywhere in the codebase. Refs: #570, #734
1 parent 94fc03c commit bfaf30d

File tree

1 file changed

+1
-46
lines changed

1 file changed

+1
-46
lines changed

documentation/source/conf.py

+1-46
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,11 @@
2323
import sys
2424
from pathlib import Path
2525
from types import ModuleType
26-
from typing import Any, Optional, TypeVar, Union
26+
from typing import Optional
2727
from unittest import mock
2828

29-
import sphinx.config
3029
import sphobjinv
3130
import tqdm
32-
from jax.typing import ArrayLike
33-
from sphinx_autodoc_typehints import format_annotation as default_format_annotation
3431

3532
# https://docs.github.com/en/actions/learn-github-actions/variables,
3633
# see the "Default environment variables" section
@@ -78,11 +75,6 @@
7875
# -- General configuration ---------------------------------------------------
7976
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
8077

81-
# pylint: disable=invalid-name
82-
# Fixes ISSUE #561 pending a fix for ISSUE #570
83-
show_warning_types = True
84-
suppress_warnings = ["config.cache"]
85-
# pylint: enable=invalid-name
8678

8779
# sphinx extensions
8880
extensions = [
@@ -216,50 +208,13 @@
216208
("py:class", r"jaxtyping\.Shaped\[Array, '.*']"),
217209
]
218210

219-
autodoc_custom_types: dict[Any, str] = { # Specify custom types for autodoc_type_hints
220-
ArrayLike: ":data:`~jax.typing.ArrayLike`",
221-
}
222-
223211
# custom references for tqdm, which does not support intersphinx
224212
tqdm_refs: dict[str, dict[str, str]] = {
225213
"py:class": {
226214
"tqdm.tqdm": "tqdm/#tqdm-objects",
227215
}
228216
}
229217

230-
231-
def typehints_formatter(
232-
annotation: Any, config: sphinx.config.Config
233-
) -> Union[str, None]:
234-
"""
235-
Properly replace custom type aliases.
236-
237-
:param annotation: The type annotation to be processed.
238-
:param config: The current configuration being used.
239-
:returns: A string of reStructured text (e.g. :py:class:`something`) or None to fall
240-
back to the default.
241-
242-
This function is called on each type annotation that Sphinx processes.
243-
The following steps occur:
244-
245-
1. Check if the annotation is a TypeVar. If so, replace it with its "bound" type
246-
for clarity in the docs. If not, then replace it with typing.Any.
247-
2. Check whether a specific Sphinx string has been defined in autodoc_custom_types.
248-
If so, return that.
249-
3. If not, then return None, which uses thee default formatter.
250-
251-
See https://github.com/tox-dev/sphinx-autodoc-typehints?tab=readme-ov-file#options
252-
for specification.
253-
"""
254-
if isinstance(annotation, TypeVar):
255-
if annotation.__bound__ is None: # when a generic TypeVar has been used.
256-
return default_format_annotation(Any, config)
257-
return default_format_annotation(
258-
annotation.__bound__, config
259-
) # get the annotation for the bound type
260-
return autodoc_custom_types.get(annotation)
261-
262-
263218
# -- Options for HTML output -------------------------------------------------
264219
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output
265220

0 commit comments

Comments
 (0)