Avoid expansion of typenames into long expressions, e.g., `numpy.typing.ArrayLike`
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 2.5k
- Forks
- 228
- PR merge metrics
- No merged PRs in 30d
Description
Packages often include typenames that expand to long Union[...] definitions.
Examples include ArrayLike and DTypeLike from numpy.typing (see here)
For this sample program
from typing import Any
import numpy as np
import numpy.typing as npt
def f1(a: npt.ArrayLike, dtype: npt.DTypeLike) -> np.ndarray[Any, Any]:
return np.asarray(a).astype(dtype)
the pdoc output looks awful:
def f1(
array: Union[numpy._array_like._SupportsArray[numpy.dtype], numpy._nested_sequence._NestedSequence[numpy._array_like._SupportsArray[numpy.dtype]], bool, int, float, complex, str, bytes, numpy._nested_sequence._NestedSequence[Union[bool, int, float, complex, str, bytes]]],
dtype: Union[numpy.dtype[Any], NoneType, Type[Any], numpy._dtype_like._SupportsDType[numpy.dtype[Any]], str, Tuple[Any, int], Tuple[Any, Union[SupportsIndex, Sequence[SupportsIndex]]], List[Any], numpy._dtype_like._DTypeDict, Tuple[Any, Any]]
) -> numpy.ndarray[typing.Any, typing.Any]:
Is there any way to direct pdoc to show shorter typenames?
-
We cannot use
typing.NewTypeortyping.TypeVarbecause theUnion[...]is not subclassable. -
Assigning the type to a local constant unfortunately does not help:
ArrayLike = npt.ArrayLike
- Similarly, declaring a local
TypeAliasalso does not help:
import typing
ArrayLike: typing.TypeAlias = npt.ArrayLike
- Even the ugly approach of deferring the type using a string does not help:
def f2(a: 'npt.ArrayLike', dtype: 'npt.DTypeLike') -> np.ndarray[Any, Any]: ...
Can you think of any other workaround or solution? Ideally, any local typename constant could remain unexpanded.
(TypeAlias is not ideal because it only appears in Python 3.10.)
Possibly look for pdoc metadata within typing.Annotated?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the provided NumPy typing example and inspect how pdoc expands annotations, especially local aliases, TypeAlias, strings, and typing.Annotated metadata. Define the desired output as preserving short local typenames such as ArrayLike and DTypeLike instead of rendering their long Union expressions, then verify the behavior against the sample program.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- documentation
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100