mitmproxy / mitmproxy/pdoc

Avoid expansion of typenames into long expressions, e.g., `numpy.typing.ArrayLike`

Open
#420 13 comments 8 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
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.NewType or typing.TypeVar because the Union[...] is not subclassable.

  • Assigning the type to a local constant unfortunately does not help:

ArrayLike = npt.ArrayLike
  • Similarly, declaring a local TypeAlias also 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.