Skip to content

Update sageinspect.getargspec to handle keyword only arguments #39774

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

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
17 changes: 14 additions & 3 deletions src/sage/misc/sageinspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1600,6 +1600,19 @@ def foo(x, a='\')"', b={not (2+1==3):'bar'}): return
sage: shell.run_cell('f = Foo()')
sage: shell.run_cell('f??')
...the source code string...

Test that :issue:`39627` is fixed:
sage: from sage.misc.sageinspect import sage_formatargspec
sage: def afunc(a, b=1, *, x=2, y, z=3):
....: return
....:
sage: sage_getargspec(afunc)
FullArgSpec(args=['a', 'b'], varargs=None, varkw=None, defaults=(1,), kwonlyargs=['x', 'y', 'z'], kwonlydefaults={'x': 2, 'z': 3}, annotations={})
sage: sage_formatargspec(*sage_getargspec(afunc))
'(a, b=1, *, x=2, y, z=3)'
sage: sage_getargspec(sage.categories.enumerated_sets.EnumeratedSets.ParentMethods.map)
FullArgSpec(args=['self', 'f', 'name'], varargs=None, varkw=None, defaults=(None,), kwonlyargs=['is_injective'], kwonlydefaults={'is_injective': True}, annotations={})

"""
from sage.misc.lazy_attribute import lazy_attribute
from sage.misc.abstract_method import AbstractMethod
Expand All @@ -1626,9 +1639,7 @@ def foo(x, a='\')"', b={not (2+1==3):'bar'}): return
if hasattr(obj, '__code__'):
# Note that this may give a wrong result for the constants!
try:
args, varargs, varkw = inspect.getargs(obj.__code__)
return inspect.FullArgSpec(args, varargs, varkw, obj.__defaults__,
kwonlyargs=[], kwonlydefaults=None, annotations={})
return inspect.getfullargspec(obj)
except (TypeError, AttributeError):
pass
if isclassinstance(obj):
Expand Down