Skip to content

Commit 0170148

Browse files
authored
Introduce sphinx.ext.autodoc._dynamic subpackage (#14087)
1 parent 21e6a24 commit 0170148

22 files changed

+93
-80
lines changed

sphinx/ext/autodoc/__init__.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,12 @@
2323
members_option,
2424
merge_members_option,
2525
)
26+
from sphinx.ext.autodoc._dynamic._member_finder import ObjectMember, special_member_re
2627
from sphinx.ext.autodoc._event_listeners import between, cut_lines
27-
from sphinx.ext.autodoc._member_finder import ObjectMember, special_member_re
2828
from sphinx.ext.autodoc._names import py_ext_sig_re
2929
from sphinx.ext.autodoc._sentinels import ALL, EMPTY, SUPPRESS, UNINITIALIZED_ATTR
30-
from sphinx.ext.autodoc._sentinels import (
31-
INSTANCE_ATTR as INSTANCEATTR,
32-
)
33-
from sphinx.ext.autodoc._sentinels import (
34-
SLOTS_ATTR as SLOTSATTR,
35-
)
30+
from sphinx.ext.autodoc._sentinels import INSTANCE_ATTR as INSTANCEATTR
31+
from sphinx.ext.autodoc._sentinels import SLOTS_ATTR as SLOTSATTR
3632
from sphinx.ext.autodoc.directive import AutodocDirective
3733
from sphinx.ext.autodoc.typehints import _merge_typehints
3834

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""The dynamic (import-based) backend for autodoc."""

sphinx/ext/autodoc/_docstrings.py renamed to sphinx/ext/autodoc/_dynamic/_docstrings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from typing import TYPE_CHECKING, TypeVar
44

55
from sphinx.errors import PycodeError
6-
from sphinx.ext.autodoc._importer import (
6+
from sphinx.ext.autodoc._dynamic._importer import (
77
_get_attribute_comment,
88
_is_runtime_instance_attribute_not_commented,
99
)

sphinx/ext/autodoc/_importer.py renamed to sphinx/ext/autodoc/_dynamic/_importer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
from typing import TYPE_CHECKING
1616

1717
from sphinx.errors import PycodeError
18+
from sphinx.ext.autodoc._dynamic._mock import ismock, mock, undecorate
1819
from sphinx.ext.autodoc._sentinels import RUNTIME_INSTANCE_ATTRIBUTE, UNINITIALIZED_ATTR
19-
from sphinx.ext.autodoc.mock import ismock, mock, undecorate
2020
from sphinx.pycode import ModuleAnalyzer
2121
from sphinx.util import inspect, logging
2222
from sphinx.util.inspect import isclass, safe_getattr

sphinx/ext/autodoc/_loader.py renamed to sphinx/ext/autodoc/_dynamic/_loader.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,17 @@
88
from types import SimpleNamespace
99
from typing import TYPE_CHECKING, NewType, TypeVar
1010

11-
from sphinx.ext.autodoc._docstrings import (
11+
from sphinx.ext.autodoc._dynamic._docstrings import (
1212
_docstring_lines_for_props,
1313
_get_docstring_lines,
1414
)
15-
from sphinx.ext.autodoc._importer import _import_object
15+
from sphinx.ext.autodoc._dynamic._importer import _import_object
16+
from sphinx.ext.autodoc._dynamic._mock import ismock
17+
from sphinx.ext.autodoc._dynamic._signatures import _format_signatures
18+
from sphinx.ext.autodoc._dynamic._type_comments import (
19+
_ensure_annotations_from_type_comments,
20+
_update_annotations_using_type_comments,
21+
)
1622
from sphinx.ext.autodoc._names import _parse_name
1723
from sphinx.ext.autodoc._property_types import (
1824
_AssignStatementProperties,
@@ -28,12 +34,6 @@
2834
UNINITIALIZED_ATTR,
2935
)
3036
from sphinx.ext.autodoc._shared import _get_render_mode
31-
from sphinx.ext.autodoc._signatures import _format_signatures
32-
from sphinx.ext.autodoc._type_comments import (
33-
_ensure_annotations_from_type_comments,
34-
_update_annotations_using_type_comments,
35-
)
36-
from sphinx.ext.autodoc.mock import ismock
3737
from sphinx.locale import __
3838
from sphinx.util import inspect, logging
3939
from sphinx.util.inspect import safe_getattr
@@ -46,7 +46,7 @@
4646
from sphinx.environment import _CurrentDocument
4747
from sphinx.events import EventManager
4848
from sphinx.ext.autodoc._directive_options import _AutoDocumenterOptions
49-
from sphinx.ext.autodoc._importer import _ImportedObject
49+
from sphinx.ext.autodoc._dynamic._importer import _ImportedObject
5050
from sphinx.ext.autodoc._property_types import _AutodocFuncProperty, _AutodocObjType
5151
from sphinx.ext.autodoc._shared import _AttrGetter, _AutodocConfig
5252

sphinx/ext/autodoc/_member_finder.py renamed to sphinx/ext/autodoc/_dynamic/_member_finder.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
from sphinx.errors import PycodeError
99
from sphinx.events import EventManager
1010
from sphinx.ext.autodoc._directive_options import _AutoDocumenterOptions
11-
from sphinx.ext.autodoc._loader import _load_object_by_name
11+
from sphinx.ext.autodoc._dynamic._loader import _load_object_by_name
12+
from sphinx.ext.autodoc._dynamic._mock import ismock, undecorate
1213
from sphinx.ext.autodoc._property_types import _ClassDefProperties, _ModuleProperties
1314
from sphinx.ext.autodoc._sentinels import ALL, INSTANCE_ATTR, SLOTS_ATTR
14-
from sphinx.ext.autodoc.mock import ismock, undecorate
1515
from sphinx.locale import __
1616
from sphinx.pycode import ModuleAnalyzer
1717
from sphinx.util import inspect, logging
File renamed without changes.

sphinx/ext/autodoc/_signatures.py renamed to sphinx/ext/autodoc/_dynamic/_signatures.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
from typing import TYPE_CHECKING, NewType, TypeVar
88

99
from sphinx.errors import PycodeError
10+
from sphinx.ext.autodoc._dynamic._preserve_defaults import update_default_value
11+
from sphinx.ext.autodoc._dynamic._type_annotations import _record_typehints
12+
from sphinx.ext.autodoc._dynamic._type_comments import (
13+
_update_annotations_using_type_comments,
14+
)
1015
from sphinx.ext.autodoc._names import py_ext_sig_re
1116
from sphinx.ext.autodoc._property_types import _AssignStatementProperties
12-
from sphinx.ext.autodoc._type_comments import _update_annotations_using_type_comments
13-
from sphinx.ext.autodoc.preserve_defaults import update_default_value
14-
from sphinx.ext.autodoc.typehints import _record_typehints
1517
from sphinx.locale import __
1618
from sphinx.pycode import ModuleAnalyzer
1719
from sphinx.util import inspect, logging
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
4+
5+
from sphinx.util import inspect
6+
from sphinx.util.typing import stringify_annotation
7+
8+
if TYPE_CHECKING:
9+
from collections.abc import Mapping
10+
from typing import Any
11+
12+
from sphinx.util.typing import _StringifyMode
13+
14+
15+
def _record_typehints(
16+
*,
17+
autodoc_annotations: dict[str, dict[str, str]],
18+
name: str,
19+
obj: Any,
20+
short_literals: bool,
21+
type_aliases: Mapping[str, str] | None,
22+
unqualified_typehints: bool,
23+
) -> None:
24+
"""Record type hints to env object."""
25+
mode: _StringifyMode
26+
if unqualified_typehints:
27+
mode = 'smart'
28+
else:
29+
mode = 'fully-qualified'
30+
31+
try:
32+
if callable(obj):
33+
annotation = autodoc_annotations.setdefault(name, {})
34+
sig = inspect.signature(obj, type_aliases=type_aliases)
35+
for param in sig.parameters.values():
36+
if param.annotation is not param.empty:
37+
annotation[param.name] = stringify_annotation(
38+
param.annotation, mode, short_literals=short_literals
39+
)
40+
if sig.return_annotation is not sig.empty:
41+
annotation['return'] = stringify_annotation(
42+
sig.return_annotation, mode, short_literals=short_literals
43+
)
44+
except (TypeError, ValueError):
45+
pass

0 commit comments

Comments
 (0)