@@ -147,7 +147,7 @@ def no_doc_run(self):
147
147
148
148
149
149
# This is what sphinx considers to be a signature
150
- signature_re = re .compile (
150
+ custom_signature_re = re .compile (
151
151
r""":sig=([\w.]+::)? # explicit module name
152
152
([\w.]+\.)? # module and/or class name(s)
153
153
(?:(\w+) \s*)? # thing name
@@ -157,6 +157,16 @@ def no_doc_run(self):
157
157
re .VERBOSE ,
158
158
)
159
159
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
+
160
170
161
171
def sub_if_not_none (pattern , repl , * strings ):
162
172
"""Make regex replacement on inputs that are not None"""
@@ -182,7 +192,7 @@ def sig_alternative(doc, signature, return_annotation):
182
192
"""
183
193
if not doc :
184
194
return signature , return_annotation
185
- m = set (re .findall (signature_re , doc ))
195
+ m = set (re .findall (custom_signature_re , doc ))
186
196
if len (m ) != 1 :
187
197
return signature , return_annotation
188
198
@@ -323,7 +333,7 @@ def fix_overloads(app, what, name, obj, options, lines):
323
333
# Capture the initial indent and the function signature
324
334
new_sig = False
325
335
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 ])
327
337
if m is not None :
328
338
new_sig = True
329
339
_ , _ , _ , _ , args , return_annotation = m .groups ()
@@ -358,6 +368,17 @@ def fix_overloads(app, what, name, obj, options, lines):
358
368
359
369
def remove_doc_annotations (app , what , name , obj , options , lines ):
360
370
"""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
+
361
382
for i in range (len (lines ) - 1 , - 1 , - 1 ):
362
383
for bad , good in docstring_replacements .items ():
363
384
lines [i ], n = re .subn (bad , good , lines [i ])
0 commit comments