From 1d027d256bae747c393ad110790260207443c0a1 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Sat, 4 Jan 2020 17:52:24 +0900 Subject: [PATCH] dep: Add ``cached-property`` package (for py37 or older) --- CHANGES | 1 + setup.py | 4 ++++ sphinx/pycode/function.py | 10 ++++++++-- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/CHANGES b/CHANGES index ef1ccf6f075..356cf05c961 100644 --- a/CHANGES +++ b/CHANGES @@ -4,6 +4,7 @@ Release 3.0.0 (in development) Dependencies ------------ +* Add ``cached-property`` package (for py37 or older) * LaTeX: drop dependency on :program:`extractbb` for image inclusion in Japanese documents as ``.xbb`` files are unneeded by :program:`dvipdfmx` since TeXLive2015 (refs: #6189) diff --git a/setup.py b/setup.py index a5dd04b0a71..3f8b399006a 100644 --- a/setup.py +++ b/setup.py @@ -31,6 +31,7 @@ 'requests>=2.5.0', 'setuptools', 'packaging', + 'cached-property', ] extras_require = { @@ -38,6 +39,9 @@ ':sys_platform=="win32"': [ 'colorama>=0.3.5', ], + ':python_version<"3.8"': [ + 'cached-property', + ], 'docs': [ 'sphinxcontrib-websupport', ], diff --git a/sphinx/pycode/function.py b/sphinx/pycode/function.py index 962ac200ba2..bb5060c15cc 100644 --- a/sphinx/pycode/function.py +++ b/sphinx/pycode/function.py @@ -9,11 +9,17 @@ """ import ast +import sys from collections import OrderedDict from inspect import Parameter from typing import cast from typing import Mapping +if sys.version_info > (3, 8): + from functools import cached_property +else: + from cached_property import cached_property + def stringify(node: ast.AST) -> str: """Stringify an AST node.""" @@ -74,7 +80,7 @@ def __init__(self, signature: str) -> None: module = ast.parse('def func' + signature + ': pass') self.definition = cast(ast.FunctionDef, module.body[0]) - @property + @cached_property def parameters(self) -> Mapping: args = self.definition.args params = [] @@ -113,6 +119,6 @@ def parameters(self) -> Mapping: return OrderedDict((p.name, p) for p in params) - @property + @cached_property def return_annotation(self) -> str: return stringify(self.definition.returns)