mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Merge pull request #8397 from tk0miya/apply_get_type_hints
autodoc: Add Optional[t] to annotation of function and method
This commit is contained in:
2
CHANGES
2
CHANGES
@@ -24,6 +24,8 @@ Features added
|
||||
* #8219: autodoc: Parameters for generic class are not shown when super class is
|
||||
a generic class and show-inheritance option is given (in Python 3.7 or above)
|
||||
* autodoc: Add ``Documenter.config`` as a shortcut to access the config object
|
||||
* autodoc: Add Optional[t] to annotation of function and method if a default
|
||||
value equal to None is set.
|
||||
* #6914: Add a new event :event:`warn-missing-reference` to custom warning
|
||||
messages when failed to resolve a cross-reference
|
||||
* #6914: Emit a detailed warning when failed to resolve a ``:ref:`` reference
|
||||
|
||||
@@ -470,10 +470,10 @@ def signature(subject: Callable, bound_method: bool = False, follow_wrapped: boo
|
||||
raise
|
||||
|
||||
try:
|
||||
# Update unresolved annotations using ``get_type_hints()``.
|
||||
# Resolve forwared reference annotations using ``get_type_hints()`` and type_aliases.
|
||||
annotations = typing.get_type_hints(subject, None, type_aliases)
|
||||
for i, param in enumerate(parameters):
|
||||
if isinstance(param.annotation, str) and param.name in annotations:
|
||||
if param.name in annotations:
|
||||
parameters[i] = param.replace(annotation=annotations[param.name])
|
||||
if 'return' in annotations:
|
||||
return_annotation = annotations['return']
|
||||
|
||||
@@ -490,7 +490,7 @@ def test_autodoc_typehints_signature(app):
|
||||
'.. py:module:: target.typehints',
|
||||
'',
|
||||
'',
|
||||
'.. py:class:: Math(s: str, o: Any = None)',
|
||||
'.. py:class:: Math(s: str, o: Optional[Any] = None)',
|
||||
' :module: target.typehints',
|
||||
'',
|
||||
'',
|
||||
|
||||
@@ -162,7 +162,7 @@ def test_signature_annotations():
|
||||
|
||||
# Space around '=' for defaults
|
||||
sig = inspect.signature(f7)
|
||||
assert stringify_signature(sig) == '(x: int = None, y: dict = {}) -> None'
|
||||
assert stringify_signature(sig) == '(x: Optional[int] = None, y: dict = {}) -> None'
|
||||
|
||||
# Callable types
|
||||
sig = inspect.signature(f8)
|
||||
@@ -226,7 +226,7 @@ def test_signature_annotations():
|
||||
assert stringify_signature(sig) == '(self) -> List[typing_test_data.Node]'
|
||||
|
||||
sig = inspect.signature(Node.__init__)
|
||||
assert stringify_signature(sig) == '(self, parent: Optional[Node]) -> None'
|
||||
assert stringify_signature(sig) == '(self, parent: Optional[typing_test_data.Node]) -> None'
|
||||
|
||||
# show_annotation is False
|
||||
sig = inspect.signature(f7)
|
||||
@@ -234,7 +234,7 @@ def test_signature_annotations():
|
||||
|
||||
# show_return_annotation is False
|
||||
sig = inspect.signature(f7)
|
||||
assert stringify_signature(sig, show_return_annotation=False) == '(x: int = None, y: dict = {})'
|
||||
assert stringify_signature(sig, show_return_annotation=False) == '(x: Optional[int] = None, y: dict = {})'
|
||||
|
||||
|
||||
@pytest.mark.skipif(sys.version_info < (3, 8), reason='python 3.8+ is required.')
|
||||
|
||||
Reference in New Issue
Block a user