def test_ipython_parse(self): def f(a): # type: (int)->str return "ok" actual = get_doc_annotaions(f, ["a"]) assert actual is None
def test_simple_parse(self): def f(a): # type: (int)->str return "ok" actual = get_doc_annotaions(f, ["a"]) assert actual["a"] == "int" assert actual["return"] == "str"
def test_nested_parse2(self): def f(a): # type: ( Dict[str, Dict[str, pd.DataFrame]]) -> str return "ok" actual = get_doc_annotaions(f, ["a", "b"]) assert actual["a"] == "Dict[str,Dict[str,pd.DataFrame]]" assert actual["return"] == "str"
def build_task_decorator_spec(item, decorator_kwargs, default_result): # type: (callable, ... ) -> _TaskDecoratorSpec is_class = inspect.isclass(item) if is_class: f = item.__init__ else: f = item name = item.__name__ if six.PY3: # python 3 https://docs.python.org/3/library/inspect.html#inspect.getfullargspec f_spec = inspect.getfullargspec(f) varkw = f_spec.varkw kwonlyargs = f_spec.kwonlyargs kwonlydefaults = f_spec.kwonlydefaults annotations = f_spec.annotations else: # python 2 f_spec = inspect.getargspec(f) varkw = f_spec.keywords kwonlyargs = None kwonlydefaults = None annotations = None f_defaults = f_spec.defaults or {} defaults_start_at = len(f_spec.args) - len(f_defaults) return _TaskDecoratorSpec( item=item, is_class=is_class, decorator_kwargs=decorator_kwargs, default_result=default_result, args=f_spec.args, varargs=f_spec.varargs, varkw=varkw, defaults={ f_spec.args[defaults_start_at + i]: v for i, v in enumerate(f_defaults) }, kwonlyargs=kwonlyargs or [], kwonlydefaults=kwonlydefaults or {}, annotations=annotations or {}, defaults_values=f_defaults, name=name, doc_annotations=get_doc_annotaions(f, f_spec.args) or {}, )
def build_callable_spec(class_or_func): # type: (callable ) -> CallableSpec is_class = inspect.isclass(class_or_func) if is_class: f = class_or_func.__init__ else: f = class_or_func if six.PY3: # python 3 https://docs.python.org/3/library/inspect.html#inspect.getfullargspec f_spec = inspect.getfullargspec(f) varkw = f_spec.varkw kwonlyargs = f_spec.kwonlyargs kwonlydefaults = f_spec.kwonlydefaults annotations = f_spec.annotations else: # python 2 f_spec = inspect.getargspec(f) varkw = f_spec.keywords kwonlyargs = None kwonlydefaults = None annotations = None f_defaults = f_spec.defaults or () defaults_start_at = len(f_spec.args) - len(f_defaults) return CallableSpec( item=class_or_func, is_class=is_class, args=f_spec.args, varargs=f_spec.varargs, varkw=varkw, defaults={ f_spec.args[defaults_start_at + i]: v for i, v in enumerate(f_defaults) }, kwonlyargs=kwonlyargs or [], kwonlydefaults=kwonlydefaults or {}, annotations=annotations or {}, defaults_values=f_defaults, doc_annotations=get_doc_annotaions(f, f_spec.args) or {}, )