mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
refactor: Move _getannotations() to sphinx.util.inspect
This commit is contained in:
1
CHANGES
1
CHANGES
@@ -15,6 +15,7 @@ Deprecated
|
||||
|
||||
* The ``follow_wrapped`` argument of ``sphinx.util.inspect.signature()``
|
||||
* ``sphinx.ext.autodoc.DataDeclarationDocumenter``
|
||||
* ``sphinx.ext.autodoc.importer._getannotations()``
|
||||
* ``sphinx.pycode.ModuleAnalyzer.parse()``
|
||||
* ``sphinx.util.requests.is_ssl_error()``
|
||||
|
||||
|
@@ -36,6 +36,11 @@ The following is a list of deprecated interfaces.
|
||||
- 5.0
|
||||
- ``sphinx.ext.autodoc.DataDocumenter``
|
||||
|
||||
* - ``sphinx.ext.autodoc.importer._getannotations()``
|
||||
- 3.4
|
||||
- 4.0
|
||||
- ``sphinx.util.inspect.getannotations()``
|
||||
|
||||
* - ``sphinx.pycode.ModuleAnalyzer.parse()``
|
||||
- 3.4
|
||||
- 5.0
|
||||
|
@@ -16,7 +16,7 @@ from typing import Any, Callable, Dict, List, Mapping, NamedTuple, Optional, Tup
|
||||
from sphinx.deprecation import RemovedInSphinx40Warning, deprecated_alias
|
||||
from sphinx.pycode import ModuleAnalyzer
|
||||
from sphinx.util import logging
|
||||
from sphinx.util.inspect import getslots, isclass, isenumclass, safe_getattr
|
||||
from sphinx.util.inspect import getannotations, getslots, isclass, isenumclass, safe_getattr
|
||||
|
||||
if False:
|
||||
# For type annotation
|
||||
@@ -149,10 +149,12 @@ def get_module_members(module: Any) -> List[Tuple[str, Any]]:
|
||||
continue
|
||||
|
||||
# annotation only member (ex. attr: int)
|
||||
if hasattr(module, '__annotations__'):
|
||||
for name in module.__annotations__:
|
||||
try:
|
||||
for name in getannotations(module):
|
||||
if name not in members:
|
||||
members[name] = (name, INSTANCEATTR)
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
return sorted(list(members.values()))
|
||||
|
||||
@@ -172,12 +174,9 @@ def _getmro(obj: Any) -> Tuple["Type", ...]:
|
||||
|
||||
|
||||
def _getannotations(obj: Any) -> Mapping[str, Any]:
|
||||
"""Get __annotations__ from given *obj* safely."""
|
||||
__annotations__ = safe_getattr(obj, '__annotations__', None)
|
||||
if isinstance(__annotations__, Mapping):
|
||||
return __annotations__
|
||||
else:
|
||||
return {}
|
||||
warnings.warn('sphinx.ext.autodoc.importer._getannotations() is deprecated.',
|
||||
RemovedInSphinx40Warning)
|
||||
return getannotations(obj)
|
||||
|
||||
|
||||
def get_object_members(subject: Any, objpath: List[str], attrgetter: Callable,
|
||||
@@ -226,10 +225,13 @@ def get_object_members(subject: Any, objpath: List[str], attrgetter: Callable,
|
||||
|
||||
# annotation only member (ex. attr: int)
|
||||
for i, cls in enumerate(_getmro(subject)):
|
||||
for name in _getannotations(cls):
|
||||
name = unmangle(cls, name)
|
||||
if name and name not in members:
|
||||
members[name] = Attribute(name, i == 0, INSTANCEATTR)
|
||||
try:
|
||||
for name in getannotations(cls):
|
||||
name = unmangle(cls, name)
|
||||
if name and name not in members:
|
||||
members[name] = Attribute(name, i == 0, INSTANCEATTR)
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
if analyzer:
|
||||
# append instance attributes (cf. self.attr1) if analyzer knows
|
||||
|
@@ -154,6 +154,18 @@ def getall(obj: Any) -> Optional[Sequence[str]]:
|
||||
raise ValueError(__all__)
|
||||
|
||||
|
||||
def getannotations(obj: Any) -> Mapping[str, Any]:
|
||||
"""Get __annotations__ from given *obj* safely.
|
||||
|
||||
Raises AttributeError if given *obj* raises an error on accessing __attribute__.
|
||||
"""
|
||||
__annotations__ = safe_getattr(obj, '__annotations__', None)
|
||||
if isinstance(__annotations__, Mapping):
|
||||
return __annotations__
|
||||
else:
|
||||
return {}
|
||||
|
||||
|
||||
def getslots(obj: Any) -> Optional[Dict]:
|
||||
"""Get __slots__ attribute of the class as dict.
|
||||
|
||||
|
Reference in New Issue
Block a user