Skip to content

Instantly share code, notes, and snippets.

@LouisdeBruijn
Last active June 15, 2021 12:03
Show Gist options
  • Save LouisdeBruijn/425f01423bfa6000687a78f07c9d9e67 to your computer and use it in GitHub Desktop.
Save LouisdeBruijn/425f01423bfa6000687a78f07c9d9e67 to your computer and use it in GitHub Desktop.
if type_hints:
docstring = f'"""{ast.get_docstring(child, clean=True)}\n"""'
docstring_lines = docstring.split("\n")
if docstring:
args = re.search(
r'Args:(.*?)(Example[s]?:|Return[s]?:|""")',
docstring,
re.DOTALL,
)
new_arguments = {}
if args:
arguments = args.group()
argument_lines = arguments.split("\n")
exclude = [
"Args:",
"Example:",
"Examples:",
"Returns:",
'"""',
]
argument_lines = [arg for arg in argument_lines if arg]
argument_lines = [arg for arg in argument_lines if not any(x in arg for x in exclude)]
for argument in argument_lines:
arg_name = argument.split()[0]
if arg_name in argument:
if argument.split(":"):
if "(" and ")" in argument.split(":")[0]:
variable_type = str(type_hints[arg_name])
class_type = re.search(r"(<class ')(.*)('>)", variable_type)
if class_type:
variable_type = class_type.group(2)
new_argument_docstring = re.sub(
r"\(.*?\)",
f"({variable_type})",
argument,
)
idx = docstring_lines.index(f"{argument}")
new_arguments[idx] = f"{new_argument_docstring}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment