How to doc test tab completion?

6 views
Skip to first unread message

Simon King

unread,
Nov 30, 2009, 5:19:35 PM11/30/09
to sage-devel
Hi!

At #6854 (ready for review, I think), I implemented introspection and
tab completion for elements of Infinite Polynomial Rings. This became
necessary since they have a __getattr__ method that allows to use
methods of the underlying *finite* polynomial (e.g., _latex_) in an
easy way, to the expence of leaving these "pseudo-inherited" methods
invisible for introspection and tab completion.

It is clear how to doc test introspection: use dir() on an instance of
the class.

But how to doc test tab completion? Is there a better way than to call
the relevant underscore method (here: _getAttributeNames) directly?

Cheers,
Simon

Simon King

unread,
Dec 1, 2009, 3:26:47 AM12/1/09
to sage-devel
Hi!

Another question related with http://trac.sagemath.org/sage_trac/ticket/6854

Reading the documentation of "dir", I expected
"""
...
If the object supplies a method named __dir__, it will be used;
otherwise
the default dir() logic is used and returns:
...
"""

But in fact, __dir__ is ignored:
{{{
sage: class FOO:
....: def __init__(self,x):
....: self.x = x
....: def __dir__(self):
....: return dir(self.x)
....:
sage: foo = FOO(x)
sage: dir(foo)
['__dir__', '__doc__', '__init__', '__module__', 'x']
sage: dir(foo.x)

['Order',
'__abs__',
'__add__',
'__call__',
'__class__',
'__cmp__',
'__complex__',
'__delattr__',
'__div__',
'__doc__',
'__eq__',
'__float__',
'__format__',
'__ge__',
'__getattribute__',
...
}}}

Is it an error in the Python documentation, or is it a Sage "feature"?

Cheers,
Simon

William Stein

unread,
Dec 1, 2009, 7:33:38 AM12/1/09
to sage-devel
I would call the code in the Sage notebook that is used to *implement*
tab completion.

sage: import sagenb.misc.support as s
sage: n = 949292
sage: s.completions('n.f',globals(),system='python')
['n.factor', 'n.factorial', 'n.floor']

-- William

Simon King

unread,
Dec 1, 2009, 8:49:02 AM12/1/09
to sage-devel
Hi William!

On Dec 1, 12:33 pm, William Stein <wst...@gmail.com> wrote:
[...]
> > But how to doc test tab completion? Is there a better way than to call
> > the relevant underscore method (here: _getAttributeNames) directly?
>
> I would call the code in the Sage notebook that is used to *implement*
> tab completion.
>
> sage: import sagenb.misc.support as s
> sage: n = 949292
> sage: s.completions('n.f',globals(),system='python')
> ['n.factor', 'n.factorial', 'n.floor']

Cool! That's what I was looking for!

Thank you
Simon

Simon King

unread,
Dec 1, 2009, 9:15:25 AM12/1/09
to sage-devel
Hi William!

On Dec 1, 12:33 pm, William Stein <wst...@gmail.com> wrote:
[...]
> I would call the code in the Sage notebook that is used to *implement*
> tab completion.

If I see correctly, the notebook tab completion is different from the
tab completion on the command line, which can be seen in the following
example:

sage: class FOO:
....: def __init__(self,x):
....: self.x = x
....: def __getattr__(self,s):
....: print s
....: return getattr(self.x,s)
....:
sage: foo = FOO(x)
sage: import sagenb.misc.support as s
sage: s.completions('foo.c',globals(),system='python')
__members__
__methods__
trait_names
[]
sage: foo.c<hit tab key>__members__
__methods__
trait_names
_getAttributeNames
}}}

So, the methods of foo called by command line tab completion and
notebook tab completion are different.

In ticket #6854 I provide an attribute __methods__, which makes
introspection work. If I understand correctly, it also makes tab
completion in the notebook work. But for tab completion on the command
line, I implemented _getAttributeNames.

Please comment on the ticket if that approach was stupid.

Cheers,
Simon
Reply all
Reply to author
Forward
0 new messages