@@ -475,19 +475,27 @@ def add(val: str) -> None:
475
475
return result
476
476
477
477
478
- def format_default (app : Sphinx , default : Any ) -> str | None :
478
+ def format_default (app : Sphinx , default : Any , is_annotated : bool ) -> str | None :
479
479
if default is inspect .Parameter .empty :
480
480
return None
481
481
formatted = repr (default ).replace ("\\ " , "\\ \\ " )
482
- if app .config .typehints_defaults .startswith ("braces" ):
483
- return f" (default: ``{ formatted } ``)"
482
+
483
+ if is_annotated :
484
+ if app .config .typehints_defaults .startswith ("braces" ):
485
+ return f" (default: ``{ formatted } ``)"
486
+ else : # other option is comma
487
+ return f", default: ``{ formatted } ``"
484
488
else :
485
- return f", default: ``{ formatted } ``"
489
+ if app .config .typehints_defaults == "braces-after" :
490
+ return f" (default: ``{ formatted } ``)"
491
+ else :
492
+ return f"default: ``{ formatted } ``"
486
493
487
494
488
495
def process_docstring (
489
496
app : Sphinx , what : str , name : str , obj : Any , options : Options | None , lines : list [str ] # noqa: U100
490
497
) -> None :
498
+
491
499
original_obj = obj
492
500
obj = obj .fget if isinstance (obj , property ) else obj
493
501
if not callable (obj ):
@@ -561,41 +569,45 @@ def _inject_types_to_docstring(
561
569
name : str ,
562
570
lines : list [str ],
563
571
) -> None :
564
- for arg_name , annotation in type_hints .items ():
565
- if arg_name == "return" :
566
- continue # this is handled separately later
567
- if signature is None or arg_name not in signature .parameters :
568
- default = inspect .Parameter .empty
569
- else :
572
+
573
+ if signature is not None :
574
+ for arg_name in signature .parameters :
575
+ annotation = type_hints .get (arg_name , None )
576
+
570
577
default = signature .parameters [arg_name ].default
571
- if arg_name .endswith ("_" ):
572
- arg_name = f"{ arg_name [:- 1 ]} \\ _"
573
578
574
- formatted_annotation = format_annotation (annotation , app .config )
579
+ if arg_name .endswith ("_" ):
580
+ arg_name = f"{ arg_name [:- 1 ]} \\ _"
575
581
576
- insert_index = None
577
- for at , line in enumerate (lines ):
578
- if _line_is_param_line_for_arg (line , arg_name ):
579
- # Get the arg_name from the doc to match up for type in case it has a star prefix.
580
- # Line is in the correct format so this is guaranteed to return tuple[str, str].
581
- _ , arg_name = _get_sphinx_line_keyword_and_argument (line ) # type: ignore[assignment, misc]
582
- insert_index = at
583
- break
582
+ insert_index = None
583
+ for at , line in enumerate (lines ):
584
+ if _line_is_param_line_for_arg (line , arg_name ):
585
+ # Get the arg_name from the doc to match up for type in case it has a star prefix.
586
+ # Line is in the correct format so this is guaranteed to return tuple[str, str].
587
+ _ , arg_name = _get_sphinx_line_keyword_and_argument (line ) # type: ignore[assignment, misc]
588
+ insert_index = at
589
+ break
590
+
591
+ if annotation is not None and insert_index is None and app .config .always_document_param_types :
592
+ lines .append (f":param { arg_name } :" )
593
+ insert_index = len (lines )
584
594
585
- if insert_index is None and app .config .always_document_param_types :
586
- lines .append (f":param { arg_name } :" )
587
- insert_index = len (lines )
588
-
589
- if insert_index is not None :
590
- type_annotation = f":type { arg_name } : { formatted_annotation } "
591
- if app .config .typehints_defaults :
592
- formatted_default = format_default (app , default )
593
- if formatted_default :
594
- if app .config .typehints_defaults .endswith ("after" ):
595
- lines [insert_index ] += formatted_default
596
- else : # add to last param doc line
597
- type_annotation += formatted_default
598
- lines .insert (insert_index , type_annotation )
595
+ if insert_index is not None :
596
+ if annotation is None :
597
+ type_annotation = f":type { arg_name } : "
598
+ else :
599
+ formatted_annotation = format_annotation (annotation , app .config )
600
+ type_annotation = f":type { arg_name } : { formatted_annotation } "
601
+
602
+ if app .config .typehints_defaults :
603
+ formatted_default = format_default (app , default , annotation is not None )
604
+ if formatted_default :
605
+ if app .config .typehints_defaults .endswith ("after" ):
606
+ lines [insert_index ] += formatted_default
607
+ else : # add to last param doc line
608
+ type_annotation += formatted_default
609
+
610
+ lines .insert (insert_index , type_annotation )
599
611
600
612
if "return" in type_hints and not inspect .isclass (original_obj ):
601
613
if what == "method" and name .endswith (".__init__" ): # avoid adding a return type for data class __init__
0 commit comments