Update sphinx_autodoc_typehints to version 1.5.2
This commit is contained in:
parent
6212d22e26
commit
1ab9e44104
@ -20,6 +20,7 @@
|
|||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
import typing
|
||||||
|
|
||||||
import sphinx_bootstrap_theme
|
import sphinx_bootstrap_theme
|
||||||
|
|
||||||
@ -374,6 +375,7 @@ html_context = {'current_release': current_release, 'current_release_date': curr
|
|||||||
|
|
||||||
|
|
||||||
def setup(app):
|
def setup(app):
|
||||||
|
typing.TYPE_CHECKING = True
|
||||||
app.add_stylesheet("style.css")
|
app.add_stylesheet("style.css")
|
||||||
app.connect('autodoc-process-signature', sphinx_autodoc_typehints.process_signature)
|
app.connect('autodoc-process-signature', sphinx_autodoc_typehints.process_signature)
|
||||||
app.connect('autodoc-process-docstring', sphinx_autodoc_typehints.process_docstring)
|
app.connect('autodoc-process-docstring', sphinx_autodoc_typehints.process_docstring)
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
# sphinx_autodoc_typehints from Mar 18 2018.
|
# sphinx_autodoc_typehints from Nov 20 2018 (version 1.5.2).
|
||||||
# https://github.com/agronholm/sphinx-autodoc-typehints
|
# https://github.com/agronholm/sphinx-autodoc-typehints
|
||||||
# (slightly modified to fix class links, maybe related to agronholm/sphinx-autodoc-typehints#38)
|
# (slightly modified to fix class links, maybe related to agronholm/sphinx-autodoc-typehints#38)
|
||||||
#
|
#
|
||||||
@ -27,8 +27,8 @@
|
|||||||
import inspect
|
import inspect
|
||||||
from typing import get_type_hints, TypeVar, Any, AnyStr, Generic, Union
|
from typing import get_type_hints, TypeVar, Any, AnyStr, Generic, Union
|
||||||
|
|
||||||
from sphinx.util.inspect import getargspec
|
from sphinx.util import logging
|
||||||
from sphinx.ext.autodoc import formatargspec
|
from sphinx.util.inspect import Signature
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from inspect import unwrap
|
from inspect import unwrap
|
||||||
@ -51,6 +51,8 @@ except ImportError:
|
|||||||
memo.add(id_func)
|
memo.add(id_func)
|
||||||
return func
|
return func
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def format_annotation(annotation):
|
def format_annotation(annotation):
|
||||||
if inspect.isclass(annotation) and annotation.__module__ == 'builtins':
|
if inspect.isclass(annotation) and annotation.__module__ == 'builtins':
|
||||||
@ -162,23 +164,37 @@ def process_signature(app, what: str, name: str, obj, options, signature, return
|
|||||||
return
|
return
|
||||||
|
|
||||||
obj = unwrap(obj)
|
obj = unwrap(obj)
|
||||||
try:
|
signature = Signature(obj)
|
||||||
argspec = getargspec(obj)
|
parameters = [
|
||||||
except (TypeError, ValueError):
|
param.replace(annotation=inspect.Parameter.empty)
|
||||||
return
|
for param in signature.signature.parameters.values()
|
||||||
|
]
|
||||||
|
|
||||||
if argspec.args:
|
if parameters:
|
||||||
if what in ('class', 'exception'):
|
if what in ('class', 'exception'):
|
||||||
del argspec.args[0]
|
del parameters[0]
|
||||||
elif what == 'method':
|
elif what == 'method':
|
||||||
outer = inspect.getmodule(obj)
|
outer = inspect.getmodule(obj)
|
||||||
for clsname in obj.__qualname__.split('.')[:-1]:
|
for clsname in obj.__qualname__.split('.')[:-1]:
|
||||||
outer = getattr(outer, clsname)
|
outer = getattr(outer, clsname)
|
||||||
method_object = outer.__dict__[obj.__name__]
|
|
||||||
if not isinstance(method_object, (classmethod, staticmethod)):
|
|
||||||
del argspec.args[0]
|
|
||||||
|
|
||||||
return formatargspec(obj, *argspec[:-1]), None
|
method_name = obj.__name__
|
||||||
|
if method_name.startswith("__") and not method_name.endswith("__"):
|
||||||
|
# If the method starts with double underscore (dunder)
|
||||||
|
# Python applies mangling so we need to prepend the class name.
|
||||||
|
# This doesn't happen if it always ends with double underscore.
|
||||||
|
class_name = obj.__qualname__.split('.')[-2]
|
||||||
|
method_name = "_{c}{m}".format(c=class_name, m=method_name)
|
||||||
|
|
||||||
|
method_object = outer.__dict__[method_name]
|
||||||
|
if not isinstance(method_object, (classmethod, staticmethod)):
|
||||||
|
del parameters[0]
|
||||||
|
|
||||||
|
signature.signature = signature.signature.replace(
|
||||||
|
parameters=parameters,
|
||||||
|
return_annotation=inspect.Signature.empty)
|
||||||
|
|
||||||
|
return signature.format_args().replace('\\', '\\\\'), None
|
||||||
|
|
||||||
|
|
||||||
def process_docstring(app, what, name, obj, options, lines):
|
def process_docstring(app, what, name, obj, options, lines):
|
||||||
@ -195,8 +211,15 @@ def process_docstring(app, what, name, obj, options, lines):
|
|||||||
except (AttributeError, TypeError):
|
except (AttributeError, TypeError):
|
||||||
# Introspecting a slot wrapper will raise TypeError
|
# Introspecting a slot wrapper will raise TypeError
|
||||||
return
|
return
|
||||||
|
except NameError as exc:
|
||||||
|
logger.warning('Cannot resolve forward reference in type annotations of "%s": %s',
|
||||||
|
name, exc)
|
||||||
|
type_hints = obj.__annotations__
|
||||||
|
|
||||||
for argname, annotation in type_hints.items():
|
for argname, annotation in type_hints.items():
|
||||||
|
if argname.endswith('_'):
|
||||||
|
argname = '{}\\_'.format(argname[:-1])
|
||||||
|
|
||||||
formatted_annotation = format_annotation(annotation)
|
formatted_annotation = format_annotation(annotation)
|
||||||
|
|
||||||
if argname == 'return':
|
if argname == 'return':
|
||||||
|
Loading…
Reference in New Issue
Block a user