22
33from __future__ import annotations
44
5+ import inspect
56from ast import literal_eval
67from collections import defaultdict
78from typing import TYPE_CHECKING , Any , Sequence
1415if TYPE_CHECKING :
1516 import ast
1617
17- from typing_extensions import Annotated , doc # type: ignore[attr-defined]
18+ from typing_extensions import Annotated , Doc # type: ignore[attr-defined]
1819
1920
2021class TypingDocExtension (Extension ):
21- """Griffe extension that reads documentation from `typing.doc `."""
22+ """Griffe extension that reads documentation from `typing.Doc `."""
2223
2324 def on_function_instance (
2425 self ,
2526 node : Annotated [
2627 ast .AST | ObjectNode ,
27- doc ("The object/AST node describing the function or its definition." ),
28+ Doc ("The object/AST node describing the function or its definition." ),
2829 ],
2930 func : Annotated [
3031 Function ,
31- doc ("The Griffe function just instantiated." ),
32+ Doc (
33+ # Multiline docstring to test de-indentation.
34+ """
35+ The Griffe function just instantiated.
36+ """ ,
37+ ),
3238 ],
3339 ) -> None :
3440 """Post-process Griffe functions to add a parameters section."""
@@ -56,10 +62,10 @@ def on_function_instance(
5662 doc = None
5763 for data in metadata :
5864 if isinstance (data , ExprCall ) and data .function .canonical_path in {
59- "typing.doc " ,
60- "typing_extensions.doc " ,
65+ "typing.Doc " ,
66+ "typing_extensions.Doc " ,
6167 }:
62- doc = literal_eval (str (data .arguments [0 ]))
68+ doc = inspect . cleandoc ( literal_eval (str (data .arguments [0 ]) ))
6369 params_doc [parameter .name ]["annotation" ] = annotation
6470 if doc :
6571 params_doc [parameter .name ]["description" ] = doc
@@ -72,7 +78,7 @@ def on_function_instance(
7278 [
7379 DocstringParameter (
7480 name = param_name ,
75- description = param_doc [ "description" ] ,
81+ description = param_doc . get ( "description" ) or "" ,
7682 annotation = param_doc ["annotation" ],
7783 value = func .parameters [param_name ].default , # type: ignore[arg-type]
7884 )
0 commit comments