autodoc.typehints can accurately represent overloads #9185
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Feature or Bugfix
Purpose
Let's say we have the following method (adapted from https://www.python.org/dev/peps/pep-0484/#function-method-overloading):
Currently, when
autotype_typehints
is set todescription
, this method would be displayed as though the following ReST were typed.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: