@@ -147,7 +147,7 @@ def no_doc_run(self):
147147
148148
149149# This is what sphinx considers to be a signature
150- signature_re = re.compile(
150+ custom_signature_re = re.compile(
151151 r""":sig=([\w.]+::)? # explicit module name
152152 ([\w.]+\.)? # module and/or class name(s)
153153 (?:(\w+) \s*)? # thing name
@@ -157,6 +157,16 @@ def no_doc_run(self):
157157 re.VERBOSE,
158158)
159159
160+ inserted_signature_re = re.compile(
161+ r"""^([\w.]+::)? # explicit module name
162+ ([\w.]+\.)? # module and/or class name(s)
163+ (?:(\w+) \s*)? # thing name
164+ (?: \[\s*(.*)\s*\])? # type parameters list
165+ (?: \((.*)\))? # arguments
166+ (?:\s* -> \s* (.*))?$""", # return annotation
167+ re.VERBOSE | re.MULTILINE,
168+ )
169+
160170
161171def sub_if_not_none(pattern, repl, *strings):
162172 """Make regex replacement on inputs that are not None"""
@@ -182,7 +192,7 @@ def sig_alternative(doc, signature, return_annotation):
182192 """
183193 if not doc:
184194 return signature, return_annotation
185- m = set(re.findall(signature_re , doc))
195+ m = set(re.findall(custom_signature_re , doc))
186196 if len(m) != 1:
187197 return signature, return_annotation
188198
@@ -323,7 +333,7 @@ def fix_overloads(app, what, name, obj, options, lines):
323333 # Capture the initial indent and the function signature
324334 new_sig = False
325335 if i + 3 < len(input_text):
326- m = re.match(signature_re , input_text[i + 3])
336+ m = re.match(custom_signature_re , input_text[i + 3])
327337 if m is not None:
328338 new_sig = True
329339 _, _, _, _, args, return_annotation = m.groups()
@@ -358,6 +368,17 @@ def fix_overloads(app, what, name, obj, options, lines):
358368
359369def remove_doc_annotations(app, what, name, obj, options, lines):
360370 """Remove any special decorations from the documentation"""
371+ if len(lines) == 0:
372+ return
373+
374+ # Remove inserted signatures if they have the wrong name
375+ m = re.match(inserted_signature_re, lines[0])
376+ if m:
377+ specified_name = m[3]
378+ short_name = name.split(".")[-1]
379+ if short_name != specified_name:
380+ del lines[0]
381+
361382 for i in range(len(lines) - 1, -1, -1):
362383 for bad, good in docstring_replacements.items():
363384 lines[i], n = re.subn(bad, good, lines[i])
0 commit comments