Skip to content

Commit ddd3631

Browse files
authored
Rely on sphinx-autodoc-typehints 1.14 (#43)
1 parent f8365fa commit ddd3631

File tree

4 files changed

+12
-16
lines changed

4 files changed

+12
-16
lines changed

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ test = [
3333
'typing_extensions>=3.10; python_version<"3.8"',
3434
]
3535
doc = [
36-
'sphinx-autodoc-typehints>=1.12',
36+
'scanpydoc[typehints]',
3737
'sphinx-rtd-theme',
3838
]
39-
typehints = ['sphinx-autodoc-typehints>=1.12']
39+
typehints = ['sphinx-autodoc-typehints>=1.14']
4040
theme = ['sphinx-rtd-theme']
4141

4242
[tool.flit.entrypoints.'sphinx.html_themes']

scanpydoc/elegant_typehints/__init__.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
this part of the functionality will no longer be necessary.
2323
#. The config value ``annotate_defaults`` (default: :data:`True`) controls if rST code
2424
like ``(default: `42`)`` is added after the type.
25+
It sets sphinx-autodoc-typehints’s option ``typehints_defaults`` to ``'braces'``
2526
#. Type annotations for :class:`tuple` return types are added::
2627
2728
def x() -> Tuple[int, float]:
@@ -72,13 +73,17 @@ def x() -> Tuple[int, float]:
7273
"scipy.sparse.csc.csc_matrix": "scipy.sparse.csc_matrix",
7374
}
7475
qualname_overrides = ChainMap({}, qualname_overrides_default)
75-
annotate_defaults = True
7676

7777

7878
def _init_vars(app: Sphinx, config: Config):
79-
global annotate_defaults
8079
qualname_overrides.update(config.qualname_overrides)
81-
annotate_defaults = config.annotate_defaults
80+
if "sphinx_autodoc_typehints" not in config.extensions and config.annotate_defaults:
81+
raise ValueError(
82+
"Can only use annotate_defaults option when using sphinx-autodoc-typehints"
83+
)
84+
if config.typehints_defaults is None and config.annotate_defaults:
85+
# override default for “typehints_defaults”
86+
config.typehints_defaults = "braces"
8287
config.html_static_path.append(str(HERE / "static"))
8388

8489

scanpydoc/elegant_typehints/example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
def example_func(a: Optional[str], b: Union[str, int, None] = None) -> Dict[str, int]:
55
"""Example function
66
7-
Hover over the paramter and return type annotations to see the long versions.
7+
Hover over the parameter and return type annotations to see the long versions.
88
99
Args:
1010
a: An example parameter

scanpydoc/elegant_typehints/formatting.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,7 @@ def format_annotation(
118118
curframe = inspect.currentframe()
119119
calframe = inspect.getouterframes(curframe, 2)
120120
if calframe[1][3] == "process_docstring":
121-
annot_fmt = format_both(annotation, fully_qualified, simplify_optional_unions)
122-
if elegant_typehints.annotate_defaults:
123-
variables = calframe[1].frame.f_locals
124-
sig = inspect.signature(variables["obj"])
125-
arg_name = variables["argname"].replace(r"\_", "_")
126-
if arg_name != "return":
127-
default = sig.parameters[arg_name].default
128-
if default is not inspect.Parameter.empty:
129-
annot_fmt += f" (default: ``{_escape(repr(default))}``)"
130-
return annot_fmt
121+
return format_both(annotation, fully_qualified, simplify_optional_unions)
131122
else: # recursive use
132123
return _format_full(annotation, fully_qualified, simplify_optional_unions)
133124

0 commit comments

Comments
 (0)