def find_return_types(module_context, func): """ Determines a set of potential return types for `func` using docstring hints :type evaluator: jedi.evaluate.Evaluator :type param: jedi.parser.tree.Param :rtype: list >>> from jedi.evaluate.docstrings import * # NOQA >>> from jedi.evaluate.docstrings import _search_param_in_docstr >>> from jedi.evaluate.docstrings import _evaluate_for_statement_string >>> from jedi.evaluate.docstrings import _search_return_in_gooogledocstr >>> from jedi.evaluate.docstrings import _search_return_in_numpydocstr >>> from jedi._compatibility import builtins >>> source = open(jedi.evaluate.docstrings.__file__.replace('.pyc', '.py'), 'r').read() >>> script = jedi.Script(source) >>> evaluator = script._evaluator >>> func = script._get_module().names_dict['find_return_types'][0].parent >>> types = find_return_types(evaluator, func) >>> print('types = %r' % (types,)) >>> assert len(types) == 1 >>> assert types[0].base.obj is builtins.list """ def search_return_in_docstr(docstr): # Check for Sphinx/Epydoc return hint for p in DOCSTRING_RETURN_PATTERNS: match = p.search(docstr) if match: return [_strip_rst_role(match.group(1))] found = [] if not found: # Check for numpy style return hint found = _search_return_in_numpydocstr(docstr) return found try: docstr = u(func.raw_doc) except AttributeError: docstr = u(func.doc) types = [] for type_str in search_return_in_docstr(docstr): if is_module_installed('jedi', '>=0.10.0;<0.11'): type_ = _evaluate_for_statement_string(module_context, type_str) else: module = func.get_parent_until() type_ = _evaluate_for_statement_string(module_context, type_str, module) types.extend(type_) return types
def actual(evaluator, params): # Parse the docstring to find the return type: ret_type = '' if '->' in self.obj.__doc__: ret_type = self.obj.__doc__.split('->')[1].strip() ret_type = ret_type.replace(' or None', '') if ret_type.startswith('iter:'): ret_type = ret_type[len('iter:'):] # we don't care if it's an iterator if ret_type in __builtins__: # The function we're inspecting returns a builtin python type, that's easy obj = _create_from_name(builtin, builtin, ret_type) return evaluator.execute(obj, params) else: # The function we're inspecting returns a GObject type parent = self.parent.obj.__name__ if parent.startswith('gi.repository'): parent = parent[len('gi.repository.'):] else: # a module with overrides, such as Gtk, behaves differently parent_module = self.parent.obj.__module__ if parent_module.startswith('gi.overrides'): parent_module = parent_module[len('gi.overrides.'):] parent = '%s.%s' % (parent_module, parent) if ret_type.startswith(parent): # A pygobject type in the same module ret_type = ret_type[len(parent):] else: # A pygobject type in a different module return_type_parent = ret_type.split('.', 1)[0] ret_type = 'from gi.repository import %s\n%s' % (return_type_parent, ret_type) result = _evaluate_for_statement_string(evaluator, ret_type, self.parent) return result
def find_return_types(module_context, func): """ Determines a set of potential return types for `func` using docstring hints :type evaluator: jedi.evaluate.Evaluator :type param: jedi.parser.tree.Param :rtype: list >>> from jedi.evaluate.docstrings import * # NOQA >>> from jedi.evaluate.docstrings import _search_param_in_docstr >>> from jedi.evaluate.docstrings import _evaluate_for_statement_string >>> from jedi.evaluate.docstrings import _search_return_in_gooogledocstr >>> from jedi.evaluate.docstrings import _search_return_in_numpydocstr >>> from jedi._compatibility import builtins >>> source = open(jedi.evaluate.docstrings.__file__.replace('.pyc', '.py'), 'r').read() >>> script = jedi.Script(source) >>> evaluator = script._evaluator >>> func = script._get_module().names_dict['find_return_types'][0].parent >>> types = find_return_types(evaluator, func) >>> print('types = %r' % (types,)) >>> assert len(types) == 1 >>> assert types[0].base.obj is builtins.list """ def search_return_in_docstr(docstr): # Check for Sphinx/Epydoc return hint for p in DOCSTRING_RETURN_PATTERNS: match = p.search(docstr) if match: return [_strip_rst_role(match.group(1))] found = [] if not found: # Check for numpy style return hint found = _search_return_in_numpydocstr(docstr) return found try: docstr = u(func.raw_doc) except AttributeError: docstr = u(func.doc) types = [] for type_str in search_return_in_docstr(docstr): if is_module_installed('jedi', '>=0.10.0;<0.11'): type_ = _evaluate_for_statement_string(module_context, type_str) else: module = func.get_parent_until() type_ = _evaluate_for_statement_string(module_context, type_str, module) types.extend(type_) return types
def actual(params): # Parse the docstring to find the return type: ret_type = '' if '->' in self.obj.__doc__: ret_type = self.obj.__doc__.split('->')[1].strip() ret_type = ret_type.replace(' or None', '') if ret_type.startswith('iter:'): ret_type = ret_type[len( 'iter:'):] # we don't care if it's an iterator if hasattr(__builtins__, ret_type): # The function we're inspecting returns a builtin python type, that's easy # (see test/test_evaluate/test_compiled.py in the jedi source code for usage) builtins = get_special_object(self.evaluator, 'BUILTINS') builtin_obj = builtins.py__getattribute__(ret_type) obj = _create_from_name(self.evaluator, builtins, builtin_obj, "") return self.evaluator.execute(obj, params) else: # The function we're inspecting returns a GObject type parent = self.parent_context.obj.__name__ if parent.startswith('gi.repository'): parent = parent[len('gi.repository.'):] else: # a module with overrides, such as Gtk, behaves differently parent_module = self.parent_context.obj.__module__ if parent_module.startswith('gi.overrides'): parent_module = parent_module[ len('gi.overrides.'):] parent = '%s.%s' % (parent_module, parent) if ret_type.startswith(parent): # A pygobject type in the same module ret_type = ret_type[len(parent):] else: # A pygobject type in a different module return_type_parent = ret_type.split('.', 1)[0] ret_type = 'from gi.repository import %s\n%s' % ( return_type_parent, ret_type) result = _evaluate_for_statement_string( self.parent_context, ret_type) return set(result)
def actual(params): # Parse the docstring to find the return type: ret_type = '' if '->' in self.obj.__doc__: ret_type = self.obj.__doc__.split('->')[1].strip() ret_type = ret_type.replace(' or None', '') if ret_type.startswith('iter:'): ret_type = ret_type[len('iter:'):] # we don't care if it's an iterator if hasattr(__builtins__, ret_type): # The function we're inspecting returns a builtin python type, that's easy # (see test/test_evaluate/test_compiled.py in the jedi source code for usage) builtins = get_special_object(self.evaluator, 'BUILTINS') builtin_obj = builtins.py__getattribute__(ret_type) obj = _create_from_name(self.evaluator, builtins, builtin_obj, "") return self.evaluator.execute(obj, params) else: # The function we're inspecting returns a GObject type parent = self.parent_context.obj.__name__ if parent.startswith('gi.repository'): parent = parent[len('gi.repository.'):] else: # a module with overrides, such as Gtk, behaves differently parent_module = self.parent_context.obj.__module__ if parent_module.startswith('gi.overrides'): parent_module = parent_module[len('gi.overrides.'):] parent = '%s.%s' % (parent_module, parent) if ret_type.startswith(parent): # A pygobject type in the same module ret_type = ret_type[len(parent):] else: # A pygobject type in a different module return_type_parent = ret_type.split('.', 1)[0] ret_type = 'from gi.repository import %s\n%s' % (return_type_parent, ret_type) result = _evaluate_for_statement_string(self.parent_context, ret_type) return set(result)