@@ -232,36 +232,41 @@ def _parse_parameters_section(self, lines: List[str], start_index: int) -> Tuple
232
232
block , i = self .read_block_items (lines , start_index )
233
233
234
234
for param_line in block :
235
+
236
+ # Check that there is an annotation in the docstring
235
237
try :
236
238
name_with_type , description = param_line .split (":" , 1 )
237
239
except ValueError :
238
240
self .error (f"Failed to get 'name: description' pair from '{ param_line } '" )
239
241
continue
240
242
243
+ # Setting defaults
244
+ default = empty
245
+ annotation = empty
246
+ kind = None
247
+ # Can only get description from docstring - keep if no type was given
241
248
description = description .lstrip ()
242
249
250
+ # If we have managed to find a type in the docstring use this
243
251
if " " in name_with_type :
244
252
name , type_ = name_with_type .split (" " , 1 )
245
- type_ = type_ .strip ("()" )
246
- if type_ .endswith (", optional" ):
247
- type_ = type_ [:- 10 ]
253
+ annotation = type_ .strip ("()" )
254
+ if annotation .endswith (", optional" ): # type: ignore
255
+ annotation = annotation [:- 10 ] # type: ignore
256
+ # Otherwise try to use the signature as `annotation` would still be empty
248
257
else :
249
258
name = name_with_type
250
- type_ = empty
251
-
252
- default = empty
253
- annotation = type_
254
- kind = None
255
259
260
+ # Check in the signature to get extra details
256
261
try :
257
262
signature_param = self .context ["signature" ].parameters [name .lstrip ("*" )] # type: ignore
258
263
except (AttributeError , KeyError ):
259
264
self .error (f"No type annotation for parameter '{ name } '" )
260
265
else :
261
- if signature_param .annotation is not empty :
266
+ # If signature_param.X are empty it doesnt matter as defaults are empty anyway
267
+ if annotation is empty :
262
268
annotation = signature_param .annotation
263
- if signature_param .default is not empty :
264
- default = signature_param .default
269
+ default = signature_param .default
265
270
kind = signature_param .kind
266
271
267
272
parameters .append (
@@ -388,29 +393,29 @@ def read_return_section(self, lines: List[str], start_index: int) -> Tuple[Optio
388
393
A tuple containing a `Section` (or `None`) and the index at which to continue parsing.
389
394
"""
390
395
text , i = self .read_block (lines , start_index )
396
+ annotation = self .context ["annotation" ]
397
+ description = ""
391
398
392
- if self .context ["signature" ]:
393
- annotation = self .context ["signature" ].return_annotation
394
- else :
395
- annotation = self .context ["annotation" ]
396
-
397
- if annotation is empty :
398
- if text :
399
- try :
400
- type_ , text = text .split (":" , 1 )
401
- except ValueError :
402
- self .error ("No type in return description" )
403
- else :
404
- annotation = type_ .lstrip ()
405
- text = text .lstrip ()
399
+ # First try to get the annotation and description from the docstring
400
+ if text :
401
+ try :
402
+ type_ , text = text .split (":" , 1 )
403
+ except ValueError :
404
+ self .error ("No type in return description" )
406
405
else :
407
- self .error ("No return type annotation" )
406
+ annotation = type_ .lstrip ()
407
+ description = text .lstrip ()
408
+
409
+ # If there was no annotation in the docstring then move to signature
410
+ if annotation is empty and self .context ["signature" ]:
411
+ annotation = self .context ["signature" ].return_annotation
408
412
413
+ # Early exit if there was no annotation in the docstring or the signature
409
414
if annotation is empty and not text :
410
415
self .error (f"Empty return section at line { start_index } " )
411
416
return None , i
412
417
413
- return Section (Section .Type .RETURN , AnnotatedObject (annotation , text )), i
418
+ return Section (Section .Type .RETURN , AnnotatedObject (annotation , description )), i
414
419
415
420
def read_examples_section (self , lines : List [str ], start_index : int ) -> Tuple [Optional [Section ], int ]:
416
421
"""
0 commit comments