##// END OF EJS Templates
more robust docstring parsing and docstring parser test...
Piti Ongmongkolkul -
Show More
@@ -670,12 +670,13 b' class IPCompleter(Completer):'
670 670 form 'min(iterable[, key=func])\n' to find
671 671 keyword argument names.
672 672 """
673 if doc is None: return []
673 674 doc = doc.lstrip()
674 675 sio = StringIO.StringIO(doc)
675 676 #care only the firstline
676 677 #docstring can be long
677 678 line = sio.readline()
678 p = re.compile(r'^[\w]+\(([^)]*)\).*')
679 p = re.compile(r'^[\w|\s.]+\(([^)]*)\).*')
679 680 #'min(iterable[, key=func])\n' -> 'iterable[, key=func]'
680 681 sig = p.search(line)
681 682 if sig is None: return []
@@ -686,9 +687,7 b' class IPCompleter(Completer):'
686 687 q = re.compile('[\s|\[]*(\w+)(?:\s*=\s*.*)')
687 688 ret = []
688 689 for s in sig:
689 tmp = q.match(s)
690 if tmp is not None:
691 ret.append(tmp.groups()[0])
690 ret += q.findall(s)
692 691 return ret
693 692
694 693 def _default_arguments(self, obj):
@@ -697,24 +696,23 b' class IPCompleter(Completer):'
697 696 call_obj = obj
698 697 ret = []
699 698 if inspect.isbuiltin(obj):
700 #parse builtin docstring for signature
699 pass
700 elif not (inspect.isfunction(obj) or inspect.ismethod(obj)):
701 #for cython embededsignature=True the docstring belongs to
702 #the object itself not __init__ or __call__
701 703 ret+=self._default_arguments_from_docstring(
702 704 getattr(obj,'__doc__',''))
703 elif not (inspect.isfunction(obj) or inspect.ismethod(obj)):
704 # for classes, check for __init__,__new__
705 705 if inspect.isclass(obj):
706 #for cython embded signature it puts
707 #__init__ signature in class docstring not __init__'s one
708 ret = self._default_arguments_from_docstring(
709 getattr(obj,'__doc__',''))
706 # for classes, check for __init__,__new__
710 707 call_obj = (getattr(obj,'__init__',None) or
711 708 getattr(obj,'__new__',None))
712 ret += self._default_arguments_from_docstring(
713 getattr(all_obj,'__doc__',''))
714 709 # for all others, check if they are __call__able
715 710 elif hasattr(obj, '__call__'):
716 711 call_obj = obj.__call__
717 712
713 ret += self._default_arguments_from_docstring(
714 getattr(call_obj,'__doc__',''))
715
718 716 try:
719 717 args,_,_1,defaults = inspect.getargspec(call_obj)
720 718 if defaults:
@@ -297,6 +297,22 b' def test_func_kw_completions():'
297 297 nt.assert_in('key=', matches)
298 298
299 299
300 def test_default_arguments_from_docstring():
301 doc = min.__doc__
302 ip = get_ipython()
303 c = ip.Completer
304 kwd = c._default_arguments_from_docstring(
305 'min(iterable[, key=func]) -> value')
306 nt.assert_equal(kwd,['key'])
307 #with cython type etc
308 kwd = c._default_arguments_from_docstring(
309 'Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)\n')
310 nt.assert_equal(kwd,['ncall','resume','nsplit'])
311 #white spaces
312 kwd = c._default_arguments_from_docstring(
313 '\n Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)\n')
314 nt.assert_equal(kwd,['ncall','resume','nsplit'])
315
300 316 def test_line_magics():
301 317 ip = get_ipython()
302 318 c = ip.Completer
General Comments 0
You need to be logged in to leave comments. Login now