##// 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 form 'min(iterable[, key=func])\n' to find
670 form 'min(iterable[, key=func])\n' to find
671 keyword argument names.
671 keyword argument names.
672 """
672 """
673 if doc is None: return []
673 doc = doc.lstrip()
674 doc = doc.lstrip()
674 sio = StringIO.StringIO(doc)
675 sio = StringIO.StringIO(doc)
675 #care only the firstline
676 #care only the firstline
676 #docstring can be long
677 #docstring can be long
677 line = sio.readline()
678 line = sio.readline()
678 p = re.compile(r'^[\w]+\(([^)]*)\).*')
679 p = re.compile(r'^[\w|\s.]+\(([^)]*)\).*')
679 #'min(iterable[, key=func])\n' -> 'iterable[, key=func]'
680 #'min(iterable[, key=func])\n' -> 'iterable[, key=func]'
680 sig = p.search(line)
681 sig = p.search(line)
681 if sig is None: return []
682 if sig is None: return []
@@ -686,9 +687,7 b' class IPCompleter(Completer):'
686 q = re.compile('[\s|\[]*(\w+)(?:\s*=\s*.*)')
687 q = re.compile('[\s|\[]*(\w+)(?:\s*=\s*.*)')
687 ret = []
688 ret = []
688 for s in sig:
689 for s in sig:
689 tmp = q.match(s)
690 ret += q.findall(s)
690 if tmp is not None:
691 ret.append(tmp.groups()[0])
692 return ret
691 return ret
693
692
694 def _default_arguments(self, obj):
693 def _default_arguments(self, obj):
@@ -697,24 +696,23 b' class IPCompleter(Completer):'
697 call_obj = obj
696 call_obj = obj
698 ret = []
697 ret = []
699 if inspect.isbuiltin(obj):
698 if inspect.isbuiltin(obj):
700 #parse builtin docstring for signature
699 pass
701 ret+=self._default_arguments_from_docstring(
702 getattr(obj,'__doc__',''))
703 elif not (inspect.isfunction(obj) or inspect.ismethod(obj)):
700 elif not (inspect.isfunction(obj) or inspect.ismethod(obj)):
704 # for classes, check for __init__,__new__
701 #for cython embededsignature=True the docstring belongs to
705 if inspect.isclass(obj):
702 #the object itself not __init__ or __call__
706 #for cython embded signature it puts
703 ret += self._default_arguments_from_docstring(
707 #__init__ signature in class docstring not __init__'s one
708 ret = self._default_arguments_from_docstring(
709 getattr(obj,'__doc__',''))
704 getattr(obj,'__doc__',''))
705 if inspect.isclass(obj):
706 # for classes, check for __init__,__new__
710 call_obj = (getattr(obj,'__init__',None) or
707 call_obj = (getattr(obj,'__init__',None) or
711 getattr(obj,'__new__',None))
708 getattr(obj,'__new__',None))
712 ret += self._default_arguments_from_docstring(
713 getattr(all_obj,'__doc__',''))
714 # for all others, check if they are __call__able
709 # for all others, check if they are __call__able
715 elif hasattr(obj, '__call__'):
710 elif hasattr(obj, '__call__'):
716 call_obj = obj.__call__
711 call_obj = obj.__call__
717
712
713 ret += self._default_arguments_from_docstring(
714 getattr(call_obj,'__doc__',''))
715
718 try:
716 try:
719 args,_,_1,defaults = inspect.getargspec(call_obj)
717 args,_,_1,defaults = inspect.getargspec(call_obj)
720 if defaults:
718 if defaults:
@@ -297,6 +297,22 b' def test_func_kw_completions():'
297 nt.assert_in('key=', matches)
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 def test_line_magics():
316 def test_line_magics():
301 ip = get_ipython()
317 ip = get_ipython()
302 c = ip.Completer
318 c = ip.Completer
General Comments 0
You need to be logged in to leave comments. Login now