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

autodoc.typehints can accurately represent overloads #9185

Merged
merged 2 commits into from May 11, 2021

Conversation

AWhetter
Copy link
Contributor

@AWhetter AWhetter commented May 9, 2021

Feature or Bugfix

  • Feature
  • Bugfix

Purpose

Let's say we have the following method (adapted from https://www.python.org/dev/peps/pep-0484/#function-method-overloading):

from typing import Callable, Iterable, Iterator, Tuple, TypeVar, overload

T1 = TypeVar('T1')
T2 = TypeVar('T2')
S = TypeVar('S')

@overload
def map(func: Callable[[T1], S], iter1: Iterable[T1]) -> Iterator[S]: ...
@overload
def map(func: Callable[[T1, T2], S],
        iter1: Iterable[T1], iter2: Iterable[T2]) -> Iterator[S]: ...
def map(func, *iterables):
    """Do a map.

    :param func: The function.
    :param iter1: An iterable.
    :param iter2: Another iterable.

    :returns: A new iterable.
    """

Currently, when autotype_typehints is set to description, this method would be displayed as though the following ReST were typed.

.. py::method: map(func, *iterables)

      Do a map.

      :param func: The function.
      :param iter1: An iterable.
      :param iter2: Another iterable.
      :param iterables:

      :returns: A new iterable.

Note the addition of the new parameter, and the lack of parameter types and return types. However it is impossible to represent this overload purely as a list of parameters. So there is no behaviour that we can give to 'description' so that it can accurately represent all overloads. Trying to display any type (even of Union of all possible types) would misguide a user.

Therefore this pull request introduces two changes to allow users to choose how to solve this problem. Firstly, overloads will never try to be displayed in the description, even when autodoc_typehints is set to 'description', because we cannot accurately represent overloads in this way. This is mentioned in the documentation to make it clear why we have to do this.

The second change is that autodoc_typehints can be set to 'both' to allow type hints to be included both in the signature and in the description. The typehints for overloads will still never try to be displayed in the description.

When autodoc_typehints is set to 'both', 'description', or 'signature' the above function will now display as:

.. py::method: map(func: Callable[[T1], S], iter1: Iterable[T1]) -> Iterator[S]
               map(func: Callable[[T1, T2], S], iter1: Iterable[T1], iter2: Iterable[T2]) -> Iterator[S]

      Do a map.

      :param func: The function.
      :param iter1: An iterable.
      :param iter2: Another iterable.

      :returns: A new iterable.

@tk0miya tk0miya added this to the 4.1.0 milestone May 11, 2021
Copy link
Member

@tk0miya tk0miya left a comment

Choose a reason for hiding this comment

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

LGTM!

@tk0miya tk0miya merged commit c341807 into sphinx-doc:4.x May 11, 2021
@AWhetter AWhetter deleted the autodoc_typehints_both branch May 11, 2021 15:28
tk0miya added a commit that referenced this pull request May 11, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants