@@ -808,43 +808,63 @@ def _inject_signature(
808808 lines : list [str ],
809809) -> None :
810810 for arg_name in signature .parameters :
811- annotation = type_hints .get (arg_name )
812-
813- default = signature .parameters [arg_name ].default
814-
815- if arg_name .endswith ("_" ):
816- arg_name = f"{ arg_name [:- 1 ]} \\ _" # noqa: PLW2901
817-
818- insert_index = None
819- for at , line in enumerate (lines ):
820- if _line_is_param_line_for_arg (line , arg_name ):
821- # Get the arg_name from the doc to match up for type in case it has a star prefix.
822- # Line is in the correct format so this is guaranteed to return tuple[str, str].
823- func = _get_sphinx_line_keyword_and_argument
824- _ , arg_name = func (line ) # type: ignore[assignment, misc] # noqa: PLW2901
825- insert_index = at
826- break
827-
828- if annotation is not None and insert_index is None and app .config .always_document_param_types :
829- lines .append (f":param { arg_name } :" )
830- insert_index = len (lines )
831-
832- if insert_index is not None :
833- if annotation is None :
834- type_annotation = f":type { arg_name } : "
835- else :
836- short_literals = app .config .python_display_short_literal_types
837- formatted_annotation = add_type_css_class (
838- format_annotation (annotation , app .config , short_literals = short_literals )
839- )
840- type_annotation = f":type { arg_name } : { formatted_annotation } "
811+ _inject_arg_signature (type_hints , signature , app , lines , arg_name )
812+
813+
814+ def _inject_arg_signature (
815+ type_hints : dict [str , Any ],
816+ signature : inspect .Signature ,
817+ app : Sphinx ,
818+ lines : list [str ],
819+ arg_name : str ,
820+ ) -> None :
821+ annotation = type_hints .get (arg_name )
822+
823+ default = signature .parameters [arg_name ].default
824+
825+ if arg_name .endswith ("_" ):
826+ arg_name = f"{ arg_name [:- 1 ]} \\ _"
827+
828+ insert_index = None
829+ for at , line in enumerate (lines ):
830+ if _line_is_param_line_for_arg (line , arg_name ):
831+ # Get the arg_name from the doc to match up for type in case it has a star prefix.
832+ # Line is in the correct format so this is guaranteed to return tuple[str, str].
833+ _ , arg_name = _get_sphinx_line_keyword_and_argument (line ) # type: ignore[assignment, misc]
834+ insert_index = at
835+ break
836+
837+ if annotation is not None and insert_index is None and app .config .always_document_param_types :
838+ lines .append (f":param { arg_name } :" )
839+ insert_index = len (lines )
840+
841+ if insert_index is not None :
842+ has_preexisting_annotation = False
843+
844+ if annotation is None :
845+ type_annotation , has_preexisting_annotation = _find_preexisting_type_annotation (lines , arg_name )
846+ else :
847+ short_literals = app .config .python_display_short_literal_types
848+ formatted_annotation = add_type_css_class (
849+ format_annotation (annotation , app .config , short_literals = short_literals )
850+ )
851+ type_annotation = f":type { arg_name } : { formatted_annotation } "
852+
853+ if app .config .typehints_defaults :
854+ formatted_default = format_default (app , default , annotation is not None or has_preexisting_annotation )
855+ if formatted_default :
856+ type_annotation = _append_default (app , lines , insert_index , type_annotation , formatted_default )
857+
858+ lines .insert (insert_index , type_annotation )
841859
842- if app .config .typehints_defaults :
843- formatted_default = format_default (app , default , annotation is not None )
844- if formatted_default :
845- type_annotation = _append_default (app , lines , insert_index , type_annotation , formatted_default )
846860
847- lines .insert (insert_index , type_annotation )
861+ def _find_preexisting_type_annotation (lines : list [str ], arg_name : str ) -> tuple [str , bool ]:
862+ """Find a type entry in the input docstring that matches the given arg name."""
863+ type_annotation = f":type { arg_name } : "
864+ for line in lines :
865+ if line .startswith (type_annotation ):
866+ return line , True
867+ return type_annotation , False
848868
849869
850870def _append_default (
0 commit comments