Skip to content

Commit

Permalink
Merge pull request #7937 from tk0miya/7935_signature_is_not_shown
Browse files Browse the repository at this point in the history
Fix #7935: autodoc: A default value inspect._empty conseals signatures
  • Loading branch information
tk0miya committed Jul 12, 2020
2 parents 1d6b311 + 17a5a29 commit 3022f72
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Expand Up @@ -27,6 +27,8 @@ Bugs fixed
----------

* #7886: autodoc: TypeError is raised on mocking generic-typed classes
* #7935: autodoc: function signature is not shown when the function has a
parameter having ``inspect._empty`` as its default value
* #7839: autosummary: cannot handle umlauts in function names
* #7865: autosummary: Failed to extract summary line when abbreviations found
* #7866: autosummary: Failed to extract correct summary line when docstring
Expand Down
8 changes: 7 additions & 1 deletion sphinx/util/inspect.py
Expand Up @@ -484,7 +484,13 @@ def signature(subject: Callable, bound_method: bool = False, follow_wrapped: boo
if len(parameters) > 0:
parameters.pop(0)

return inspect.Signature(parameters, return_annotation=return_annotation)
# To allow to create signature object correctly for pure python functions,
# pass an internal parameter __validate_parameters__=False to Signature
#
# For example, this helps a function having a default value `inspect._empty`.
# refs: https://github.com/sphinx-doc/sphinx/issues/7935
return inspect.Signature(parameters, return_annotation=return_annotation, # type: ignore
__validate_parameters__=False)


def stringify_signature(sig: inspect.Signature, show_annotation: bool = True,
Expand Down
6 changes: 5 additions & 1 deletion tests/test_util_inspect.py
Expand Up @@ -130,7 +130,7 @@ def meth2(self, arg1, arg2):

def test_signature_annotations():
from typing_test_data import (f0, f1, f2, f3, f4, f5, f6, f7, f8, f9, f10,
f11, f12, f13, f14, f15, f16, f17, f18, f19, f20, Node)
f11, f12, f13, f14, f15, f16, f17, f18, f19, f20, f21, Node)

# Class annotations
sig = inspect.signature(f0)
Expand Down Expand Up @@ -214,6 +214,10 @@ def test_signature_annotations():
sig = inspect.signature(f19)
assert stringify_signature(sig) == '(*args: int, **kwargs: str)'

# default value is inspect.Signature.empty
sig = inspect.signature(f21)
assert stringify_signature(sig) == "(arg1='whatever', arg2)"

# type hints by string
sig = inspect.signature(Node.children)
if (3, 5, 0) <= sys.version_info < (3, 5, 3):
Expand Down
4 changes: 4 additions & 0 deletions tests/typing_test_data.py
@@ -1,3 +1,4 @@
from inspect import Signature
from numbers import Integral
from typing import Any, Dict, List, TypeVar, Union, Callable, Tuple, Optional

Expand Down Expand Up @@ -100,6 +101,9 @@ def f20() -> Optional[Union[int, str]]:
pass


def f21(arg1='whatever', arg2=Signature.empty):
pass


class Node:
def __init__(self, parent: Optional['Node']) -> None:
Expand Down

0 comments on commit 3022f72

Please sign in to comment.