in packages/python-packages/apiview-stub-generator/apistub/nodes/_function_node.py [0:0]
def _parse_docstring(self):
# Parse docstring to get list of keyword args, type and default value for both positional and
# kw args and return type( if not already found in signature)
docstring = ""
if hasattr(self.obj, "__doc__"):
docstring = getattr(self.obj, "__doc__")
# Refer docstring at class if this is constructor and docstring is missing for __init__
if (
not docstring
and self.name == "__init__"
and hasattr(self.parent_node.obj, "__doc__")
):
docstring = getattr(self.parent_node.obj, "__doc__")
if docstring:
# Parse doc string to find missing types, kwargs and return type
parsed_docstring = DocstringParser(docstring, apiview=self.apiview)
# Set return type if not already set
if not self.return_type and parsed_docstring.ret_type:
logging.debug(
"Setting return type from docstring for method {}".format(self.name)
)
self.return_type = parsed_docstring.ret_type
# if something is missing from the signature parsing, update it from the
# docstring, if available
for argname, signature_arg in {**self.args, **self.posargs}.items():
signature_arg.argtype = (
signature_arg.argtype
if signature_arg.argtype is not None
else parsed_docstring.type_for(argname)
)
signature_arg.default = (
signature_arg.default
if signature_arg.default is not None
else parsed_docstring.default_for(argname)
)
# if something is missing from the signature parsing, update it from the
# docstring, if available
remaining_docstring_kwargs = set(parsed_docstring.kwargs.keys())
for argname, kw_arg in self.kwargs.items():
docstring_match = parsed_docstring.kwargs.get(argname, None)
if not docstring_match:
continue
remaining_docstring_kwargs.remove(argname)
if not kw_arg.is_required:
kw_arg.argtype = (
kw_arg.argtype
if kw_arg.argtype is not None
else parsed_docstring.type_for(argname)
)
kw_arg.default = (
kw_arg.default
if kw_arg.default is not None
else parsed_docstring.default_for(argname)
)
# ensure any kwargs described only in the docstrings are added
for argname in remaining_docstring_kwargs:
self.kwargs[argname] = parsed_docstring.kwargs[argname]
# retrieve the special *args type from docstrings
if self.special_kwarg and not self.special_kwarg.argtype:
match = parsed_docstring.pos_args.get(self.special_kwarg.argname, None)
if match:
self.special_kwarg.argtype = match.argtype
# retrieve the special **kwargs type from docstrings
if self.special_vararg and not self.special_vararg.argtype:
match = parsed_docstring.pos_args.get(self.special_vararg.argname, None)
if match:
self.special_vararg.argtype = match.argtype