105
105
By :trac:`9976` and :trac:`14017`, introspection also works for interactively
106
106
defined Cython code, and with rather tricky argument lines::
107
107
108
- sage: cython('def foo(unsigned int x=1, a=\')"\', b={not (2+1==3):\'bar\'}, *args, **kwds): return') # needs sage.misc.cython
109
- sage: print(sage_getsource(foo)) # needs sage.misc.cython
108
+ sage: # needs sage.misc.cython
109
+ sage: cython('def foo(unsigned int x=1, a=\')"\', b={not (2+1==3):\'bar\'}, *args, **kwds): return')
110
+ sage: print(sage_getsource(foo))
110
111
def foo(unsigned int x=1, a=')"', b={not (2+1==3):'bar'}, *args, **kwds): return
111
- sage: sage_getargspec(foo) # needs sage.misc.cython
112
+ sage: sage_getargspec(foo)
112
113
FullArgSpec(args=['x', 'a', 'b'], varargs='args', varkw='kwds', defaults=(1, ')"', {False: 'bar'}), kwonlyargs=[], kwonlydefaults=None, annotations={})
113
114
114
115
"""
@@ -1473,27 +1474,20 @@ def sage_getargspec(obj):
1473
1474
1474
1475
INPUT:
1475
1476
1476
- ``obj``, any callable object
1477
+ - ``obj`` -- any callable object
1477
1478
1478
1479
OUTPUT:
1479
1480
1480
- An ``ArgSpec`` is returned. This is a named tuple
1481
- ``(args, varargs, keywords, defaults)``.
1482
-
1483
- - ``args`` is a list of the argument names (it may contain nested lists).
1484
-
1485
- - ``varargs`` and ``keywords`` are the names of the ``*`` and ``**``
1486
- arguments or ``None``.
1487
-
1488
- - ``defaults`` is an `n`-tuple of the default values of the last `n` arguments.
1481
+ A named tuple :class:`FullArgSpec` is returned, as specified by the
1482
+ Python library function :func:`inspect.getfullargspec`.
1489
1483
1490
1484
NOTE:
1491
1485
1492
- If the object has a method ``_sage_argspec_`` then the output of
1486
+ If the object has a method ``_sage_argspec_``, then the output of
1493
1487
that method is transformed into a named tuple and then returned.
1494
1488
1495
- If a class instance has a method ``_sage_src_`` then its output
1496
- is studied to determine the argspec. This is because currently
1489
+ If a class instance has a method ``_sage_src_``, then its output
1490
+ is studied to determine the argspec. This is because currently
1497
1491
the :class:`~sage.misc.cachefunc.CachedMethod` decorator has
1498
1492
no ``_sage_argspec_`` method.
1499
1493
@@ -1503,23 +1497,28 @@ def sage_getargspec(obj):
1503
1497
sage: def f(x, y, z=1, t=2, *args, **keywords):
1504
1498
....: pass
1505
1499
sage: sage_getargspec(f)
1506
- FullArgSpec(args=['x', 'y', 'z', 't'], varargs='args', varkw='keywords', defaults=(1, 2), kwonlyargs=[], kwonlydefaults=None, annotations={})
1500
+ FullArgSpec(args=['x', 'y', 'z', 't'], varargs='args', varkw='keywords',
1501
+ defaults=(1, 2), kwonlyargs=[], kwonlydefaults=None, annotations={})
1507
1502
1508
1503
We now run sage_getargspec on some functions from the Sage library::
1509
1504
1510
1505
sage: sage_getargspec(identity_matrix) # needs sage.modules
1511
- FullArgSpec(args=['ring', 'n', 'sparse'], varargs=None, varkw=None, defaults=(0, False),
1512
- kwonlyargs=[], kwonlydefaults=None, annotations={})
1506
+ FullArgSpec(args=['ring', 'n', 'sparse'], varargs=None, varkw=None,
1507
+ defaults=(0, False), kwonlyargs=[], kwonlydefaults=None,
1508
+ annotations={})
1513
1509
sage: sage_getargspec(factor)
1514
- FullArgSpec(args=['n', 'proof', 'int_', 'algorithm', 'verbose'], varargs=None, varkw='kwds', defaults=(None, False, 'pari', 0), kwonlyargs=[], kwonlydefaults=None, annotations={})
1510
+ FullArgSpec(args=['n', 'proof', 'int_', 'algorithm', 'verbose'],
1511
+ varargs=None, varkw='kwds', defaults=(None, False, 'pari', 0),
1512
+ kwonlyargs=[], kwonlydefaults=None, annotations={})
1515
1513
1516
- In the case of a class or a class instance, the ``ArgSpec` ` of the
1514
+ In the case of a class or a class instance, the :class:`FullArgSpec ` of the
1517
1515
``__new__``, ``__init__`` or ``__call__`` method is returned::
1518
1516
1519
1517
sage: P.<x,y> = QQ[]
1520
1518
sage: sage_getargspec(P) # needs sage.libs.singular
1521
- FullArgSpec(args=['base_ring', 'n', 'names', 'order'], varargs=None, varkw=None,
1522
- defaults=('degrevlex',), kwonlyargs=[], kwonlydefaults=None, annotations={})
1519
+ FullArgSpec(args=['base_ring', 'n', 'names', 'order'],
1520
+ varargs=None, varkw=None, defaults=('degrevlex',),
1521
+ kwonlyargs=[], kwonlydefaults=None, annotations={})
1523
1522
sage: sage_getargspec(P.__class__) # needs sage.libs.singular
1524
1523
FullArgSpec(args=['self', 'x'], varargs='args', varkw='kwds', defaults=(0,),
1525
1524
kwonlyargs=[], kwonlydefaults=None, annotations={})
@@ -1545,13 +1544,13 @@ def sage_getargspec(obj):
1545
1544
FullArgSpec(args=['x', 'y'], varargs=None, varkw=None, defaults=None,
1546
1545
kwonlyargs=[], kwonlydefaults=None, annotations={})
1547
1546
1548
- If a `` functools.partial` ` instance is involved, we see no other meaningful solution
1547
+ If a :func:` functools.partial` instance is involved, we see no other meaningful solution
1549
1548
than to return the argspec of the underlying function::
1550
1549
1551
- sage: def f(a,b,c, d=1):
1552
- ....: return a+b+c+ d
1550
+ sage: def f(a, b, c, d=1):
1551
+ ....: return a + b + c + d
1553
1552
sage: import functools
1554
- sage: f1 = functools.partial(f, 1,c=2)
1553
+ sage: f1 = functools.partial(f, 1, c=2)
1555
1554
sage: sage_getargspec(f1)
1556
1555
FullArgSpec(args=['a', 'b', 'c', 'd'], varargs=None, varkw=None, defaults=(1,),
1557
1556
kwonlyargs=[], kwonlydefaults=None, annotations={})
@@ -1565,7 +1564,7 @@ def sage_getargspec(obj):
1565
1564
an instance of that class does not coincide with the argspec
1566
1565
of its call method. That behaviour is intended, since a
1567
1566
decorated method appears to have the generic signature
1568
- ``*args,**kwds``, but in fact it is only supposed to be called
1567
+ ``*args, **kwds``, but in fact it is only supposed to be called
1569
1568
with the arguments requested by the underlying undecorated
1570
1569
method. We saw an easy example above, namely ``I.groebner_basis``.
1571
1570
Here is a more difficult one::
@@ -1605,13 +1604,14 @@ def foo(x, a='\')"', b={not (2+1==3):'bar'}): return
1605
1604
The following produced a syntax error before the patch at :trac:`11913`,
1606
1605
see also :trac:`26906`::
1607
1606
1608
- sage: sage.misc.sageinspect.sage_getargspec(r.lm) # optional - rpy2
1607
+ sage: sage.misc.sageinspect.sage_getargspec(r.lm) # optional - rpy2
1609
1608
FullArgSpec(args=['self'], varargs='args', varkw='kwds', defaults=None,
1610
1609
kwonlyargs=[], kwonlydefaults=None, annotations={})
1611
1610
1612
1611
The following was fixed in :trac:`16309`::
1613
1612
1614
- sage: cython(''' # optional - sage.misc.cython
1613
+ sage: cython( # needs sage.misc.cython
1614
+ ....: '''
1615
1615
....: class Foo:
1616
1616
....: @staticmethod
1617
1617
....: def join(categories, bint as_list = False, tuple ignore_axioms=(), tuple axioms=()): pass
0 commit comments