Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update docstring parser to work with Python 3.11 #105

Merged
merged 4 commits into from
Apr 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions core/python/docparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ def get_doc_for_module(module_path, include_link_list = True):

def format_object(modname, x):
name = '%s.%s' % (modname, x)
return '\n.. _%s:\n\n%s\n%s\n' % (name, name, '_' * len(name))
return '\n.. _%s:\n\n%s\n%s\n' % (name, name, '-' * len(name))
def format_name(modname, x):
return '\n.. _%s.%s:\n\n**%s.%s**\n' % (modname, x, modname, x)
def format_arguments(s0,s1):
# remove object hashes
s1 = re.sub('<(.*) at (.*)>', '<\\1>', s1)
argdef = '%s%s'%(s0,s1)
def format_signature(obj):
return re.sub('<(.*) at (.*)>', '<\\1>', str(inspect.signature(obj)))
def format_definition(name, obj):
argdef = '%s%s' % (name, format_signature(obj))
return '\n\n*Definition:*\n ``%s``\n' % argdef.strip()
def add_str(s0, s1):
return s0 + s1 + '\n'
Expand Down Expand Up @@ -99,10 +99,10 @@ def iterate_through_mod(mod, modname):
# in py3, all functions have an __init__, so check if we are dealing with a function first
# this logic should be backward-compatible with py2
if inspect.isfunction(obj):
out_str = add_str(out_str, format_arguments(x, inspect.formatargspec(*inspect.getargspec(obj))))
out_str = add_str(out_str, format_definition(x, obj))
else:
out_str = add_str(out_str, '\n*Constructor:*\n\t``%s%s``\n' %
((x, inspect.formatargspec(*inspect.getargspec(obj.__init__))) ) )
((x, format_signature(obj.__init__)) ) )
if format_doc(obj.__init__):
con_str = '\n*Constructor:*\n\t%s\n' % format_doc(obj.__init__).replace('\n', '\n\t')
con_str = con_str.replace(' -> None', ' -> None``')
Expand All @@ -122,7 +122,7 @@ def iterate_through_mod(mod, modname):
out_str = add_str(out_str, baredoc)
else:
out_str = add_str(out_str, '\nNo documentation\n')
out_str = add_str(out_str, format_arguments(x, inspect.formatargspec(*inspect.getargspec(obj))))
out_str = add_str(out_str, format_definition(x, obj))
mod_dict[itemname] = out_str
elif hasattr(obj, '__dict__'):
mod_dict.update(iterate_through_mod(obj, itemname))
Expand Down Expand Up @@ -166,7 +166,7 @@ def iterate_through_func(mod, modname):
if subclasstest:
out_str = out_str
try:
out_str = add_str(out_str, format_arguments(x, inspect.formatargspec(*inspect.getargspec(obj))))
out_str = add_str(out_str, format_definition(x, obj))
except:
pass
out_str = add_str(out_str, '')
Expand Down
49 changes: 13 additions & 36 deletions core/python/g3decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,22 @@
import textwrap
import re

def get_function_signature(f, replacement_kwargs = None):
def get_function_signature(f, replacement_kwargs=None):
'''
gets the function signature of f.

if replacement_kwargs is supplied replaces the default values with
the ones in replacement_kwargs

Returns the function signature of f. If replacement_kwargs is supplied
replaces the default values with the ones in replacement_kwargs.
'''
args = inspect.getargspec(f)
name = f.__name__
#ereh sdrawkcab si gnihtyreve
if not args.args is None:
ar = list(args.args)
else:
ar = []
if not args.defaults is None:
d = list(args.defaults)
else:
d = []
ar.reverse()
d.reverse()

if not replacement_kwargs is None:
for i, a in enumerate(ar):
if a in replacement_kwargs and i < len(d) :
d[i] = replacement_kwargs[a]
func_string = ')'
for i, a in enumerate(ar):
if i != 0:
func_string += ' ,'
if i < len(d):
# remove object hashes
s = re.sub('<(.*) at (.*)>', '<\\1>', repr(d[i]))
func_string += s[::-1]+'='
func_string += str(a)[::-1]
#and now things are forwards
func_string = name+'('+func_string[::-1]
return func_string
sig = inspect.signature(f)

if replacement_kwargs is not None:
params = []
for name, par in sig.parameters.items():
if name in replacement_kwargs:
par = par.replace(default=replacement_kwargs[name])
params.append(par)
sig = sig.replace(parameters=params)

return f.__name__ + re.sub('<(.*) at (.*)>', '<\\1>', str(sig))


class cache_frame_data(object):
Expand Down