def get_filters(self, search_global=False, position=None, origin_scope=None): yield DictFilter(dict( name=compiled.create_simple_object(self.evaluator, self._name.string_name).name, value=self._name, )) for f in self._get_wrapped_context().get_filters(): yield f
def wrapper(evaluator, context, tree_name): if tree_name.value == 'sep' and context.is_module( ) and context.py__name__() == 'os.path': return ContextSet({ compiled.create_simple_object(evaluator, os.path.sep), }) return func(evaluator, context, tree_name)
def py__getitem__(self, index): try: names = self.get_function_slot_names(u'__getitem__') except KeyError: debug.warning('No __getitem__, cannot access the array.') return NO_CONTEXTS else: index_obj = compiled.create_simple_object(self.evaluator, index) return self.execute_function_slots(names, index_obj)
def test_doc(evaluator): """ Even CompiledObject docs always return empty docstrings - not None, that's just a Jedi API definition. """ str_ = compiled.create_simple_object(evaluator, u'') # Equals `''.__getnewargs__` obj, = str_.py__getattribute__(u'__getnewargs__') assert obj.py__doc__() == ''
def test_doc(evaluator): """ Even CompiledObject docs always return empty docstrings - not None, that's just a Jedi API definition. """ str_ = compiled.create_simple_object(evaluator, u'') # Equals `''.__getnewargs__` obj = compiled.create_from_name(evaluator, str_, u'__getnewargs__') assert obj.py__doc__() == ''
def infer(self): if self._string_value is not None: s = self._string_value if self.parent_context.evaluator.environment.version_info.major == 2 \ and not isinstance(s, bytes): s = s.encode('utf-8') return ContextSet( [create_simple_object(self.parent_context.evaluator, s)]) return compiled.get_string_context_set(self.parent_context.evaluator)
def test_simple(evaluator, environment): obj = compiled.create_simple_object(evaluator, u'_str_') upper, = obj.py__getattribute__(u'upper') objs = list(execute_evaluated(upper)) assert len(objs) == 1 if environment.version_info.major == 2: expected = 'unicode' else: expected = 'str' assert objs[0].name.string_name == expected
def py__getitem__(context, typ, node): if not typ.get_root_context().name.string_name == "typing": return None # we assume that any class using [] in a module called # "typing" with a name for which we have a replacement # should be replaced by that class. This is not 100% # airtight but I don't have a better idea to check that it's # actually the PEP-0484 typing module and not some other if node.type == "subscriptlist": nodes = node.children[::2] # skip the commas else: nodes = [node] del node nodes = [_fix_forward_reference(context, node) for node in nodes] type_name = typ.name.string_name # hacked in Union and Optional, since it's hard to do nicely in parsed code if type_name in ("Union", '_Union'): # In Python 3.6 it's still called typing.Union but it's an instance # called _Union. return ContextSet.from_sets(context.eval_node(node) for node in nodes) if type_name in ("Optional", '_Optional'): # Here we have the same issue like in Union. Therefore we also need to # check for the instance typing._Optional (Python 3.6). return context.eval_node(nodes[0]) module_node, code_lines = _get_typing_replacement_module(context.evaluator.latest_grammar) typing = ModuleContext( context.evaluator, module_node=module_node, path=None, code_lines=code_lines, ) factories = typing.py__getattribute__("factory") assert len(factories) == 1 factory = list(factories)[0] assert factory function_body_nodes = factory.tree_node.children[4].children valid_classnames = set(child.name.value for child in function_body_nodes if isinstance(child, tree.Class)) if type_name not in valid_classnames: return None compiled_classname = compiled.create_simple_object(context.evaluator, type_name) from jedi.evaluate.context.iterable import FakeSequence args = FakeSequence( context.evaluator, u'tuple', [LazyTreeContext(context, n) for n in nodes] ) result = factory.execute_evaluated(compiled_classname, args) return result
def _check_getattr(self, inst): """Checks for both __getattr__ and __getattribute__ methods""" # str is important, because it shouldn't be `Name`! name = compiled.create_simple_object(self._evaluator, self._string_name) # This is a little bit special. `__getattribute__` is in Python # executed before `__getattr__`. But: I know no use case, where # this could be practical and where Jedi would return wrong types. # If you ever find something, let me know! # We are inversing this, because a hand-crafted `__getattribute__` # could still call another hand-crafted `__getattr__`, but not the # other way around. names = (inst.get_function_slot_names(u'__getattr__') or inst.get_function_slot_names(u'__getattribute__')) return inst.execute_function_slots(names, name)
def eval_factor(context_set, operator): """ Calculates `+`, `-`, `~` and `not` prefixes. """ for context in context_set: if operator == '-': if is_number(context): yield context.negate() elif operator == 'not': value = context.py__bool__() if value is None: # Uncertainty. return yield compiled.create_simple_object(context.evaluator, not value) else: yield context
def py__getitem__(self, index): """Here the index is an int/str. Raises IndexError/KeyError.""" if self.array_type == u'dict': compiled_obj_index = compiled.create_simple_object(self.evaluator, index) for key, value in self._items(): for k in self._defining_context.eval_node(key): if isinstance(k, compiled.CompiledObject) \ and k.execute_operation(compiled_obj_index, u'==').get_safe_value(): return self._defining_context.eval_node(value) raise KeyError('No key found in dictionary %s.' % self) # Can raise an IndexError if isinstance(index, slice): return ContextSet(self) else: return self._defining_context.eval_node(self._items()[index])
def _os_path_join(args_set, callback): if len(args_set) == 1: string = u'' sequence, = args_set is_first = True for lazy_context in sequence.py__iter__(): string_contexts = lazy_context.infer() if len(string_contexts) != 1: break s = get_str_or_none(next(iter(string_contexts))) if s is None: break if not is_first: string += os.path.sep string += force_unicode(s) is_first = False else: return ContextSet( [compiled.create_simple_object(sequence.evaluator, string)]) return callback()
def py__simple_getitem__(self, index): """Here the index is an int/str. Raises IndexError/KeyError.""" if self.array_type == u'dict': compiled_obj_index = compiled.create_simple_object(self.evaluator, index) for key, value in self.get_tree_entries(): for k in self._defining_context.eval_node(key): try: method = k.execute_operation except AttributeError: pass else: if method(compiled_obj_index, u'==').get_safe_value(): return self._defining_context.eval_node(value) raise SimpleGetItemNotFound('No key found in dictionary %s.' % self) if isinstance(index, slice): return ContextSet([self]) else: with reraise_getitem_errors(TypeError, KeyError, IndexError): node = self.get_tree_entries()[index] return self._defining_context.eval_node(node)
def eval_atom(context, atom): """ Basically to process ``atom`` nodes. The parser sometimes doesn't generate the node (because it has just one child). In that case an atom might be a name or a literal as well. """ if atom.type == 'name': # This is the first global lookup. stmt = tree.search_ancestor(atom, 'expr_stmt', 'lambdef') or atom if stmt.type == 'lambdef': stmt = atom return context.py__getattribute__(name_or_str=atom, position=stmt.start_pos, search_global=True) elif isinstance(atom, tree.Literal): string = context.evaluator.compiled_subprocess.safe_literal_eval( atom.value) return ContextSet( compiled.create_simple_object(context.evaluator, string)) else: c = atom.children if c[0].type == 'string': # Will be one string. context_set = eval_atom(context, c[0]) for string in c[1:]: right = eval_atom(context, string) context_set = _eval_comparison(context.evaluator, context, context_set, u'+', right) return context_set # Parentheses without commas are not tuples. elif c[0] == '(' and not len(c) == 2 \ and not(c[1].type == 'testlist_comp' and len(c[1].children) > 1): return context.eval_node(c[1]) try: comp_for = c[1].children[1] except (IndexError, AttributeError): pass else: if comp_for == ':': # Dict comprehensions have a colon at the 3rd index. try: comp_for = c[1].children[3] except IndexError: pass if comp_for.type == 'comp_for': return ContextSet( iterable.Comprehension.from_atom(context.evaluator, context, atom)) # It's a dict/list/tuple literal. array_node = c[1] try: array_node_c = array_node.children except AttributeError: array_node_c = [] if c[0] == '{' and (array_node == '}' or ':' in array_node_c): context = iterable.DictLiteralContext(context.evaluator, context, atom) else: context = iterable.SequenceLiteralContext(context.evaluator, context, atom) return ContextSet(context)
def eval_atom(context, atom): """ Basically to process ``atom`` nodes. The parser sometimes doesn't generate the node (because it has just one child). In that case an atom might be a name or a literal as well. """ if atom.type == 'name': # This is the first global lookup. stmt = tree.search_ancestor( atom, 'expr_stmt', 'lambdef' ) or atom if stmt.type == 'lambdef': stmt = atom return context.py__getattribute__( name_or_str=atom, position=stmt.start_pos, search_global=True ) elif atom.type == 'keyword': # For False/True/None if atom.value in ('False', 'True', 'None'): return ContextSet(compiled.builtin_from_name(context.evaluator, atom.value)) elif atom.value == 'print': # print e.g. could be evaluated like this in Python 2.7 return NO_CONTEXTS elif atom.value == 'yield': # Contrary to yield from, yield can just appear alone to return a # value when used with `.send()`. return NO_CONTEXTS assert False, 'Cannot evaluate the keyword %s' % atom elif isinstance(atom, tree.Literal): string = context.evaluator.compiled_subprocess.safe_literal_eval(atom.value) return ContextSet(compiled.create_simple_object(context.evaluator, string)) elif atom.type == 'strings': # Will be multiple string. context_set = eval_atom(context, atom.children[0]) for string in atom.children[1:]: right = eval_atom(context, string) context_set = _eval_comparison(context.evaluator, context, context_set, u'+', right) return context_set else: c = atom.children # Parentheses without commas are not tuples. if c[0] == '(' and not len(c) == 2 \ and not(c[1].type == 'testlist_comp' and len(c[1].children) > 1): return context.eval_node(c[1]) try: comp_for = c[1].children[1] except (IndexError, AttributeError): pass else: if comp_for == ':': # Dict comprehensions have a colon at the 3rd index. try: comp_for = c[1].children[3] except IndexError: pass if comp_for.type == 'comp_for': return ContextSet(iterable.comprehension_from_atom( context.evaluator, context, atom )) # It's a dict/list/tuple literal. array_node = c[1] try: array_node_c = array_node.children except AttributeError: array_node_c = [] if c[0] == '{' and (array_node == '}' or ':' in array_node_c): context = iterable.DictLiteralContext(context.evaluator, context, atom) else: context = iterable.SequenceLiteralContext(context.evaluator, context, atom) return ContextSet(context)
def test_simple(evaluator): obj = compiled.create_simple_object(evaluator, u'_str_') upper, = obj.py__getattribute__(u'upper') objs = list(upper.execute_evaluated()) assert len(objs) == 1 assert isinstance(objs[0], instance.CompiledInstance)
def iterate(): for context in strings: s = get_str_or_none(context) if s is not None: s = func(s) yield compiled.create_simple_object(context.evaluator, s)
def eval_atom(context, atom): """ Basically to process ``atom`` nodes. The parser sometimes doesn't generate the node (because it has just one child). In that case an atom might be a name or a literal as well. """ if atom.type == 'name': if atom.value in ('True', 'False', 'None'): # Python 2... return ContextSet( [compiled.builtin_from_name(context.evaluator, atom.value)]) # This is the first global lookup. stmt = tree.search_ancestor(atom, 'expr_stmt', 'lambdef') or atom if stmt.type == 'lambdef': stmt = atom position = stmt.start_pos if _is_annotation_name(atom): # Since Python 3.7 (with from __future__ import annotations), # annotations are essentially strings and can reference objects # that are defined further down in code. Therefore just set the # position to None, so the finder will not try to stop at a certain # position in the module. position = None return context.py__getattribute__(name_or_str=atom, position=position, search_global=True) elif atom.type == 'keyword': # For False/True/None if atom.value in ('False', 'True', 'None'): return ContextSet( [compiled.builtin_from_name(context.evaluator, atom.value)]) elif atom.value == 'print': # print e.g. could be evaluated like this in Python 2.7 return NO_CONTEXTS elif atom.value == 'yield': # Contrary to yield from, yield can just appear alone to return a # value when used with `.send()`. return NO_CONTEXTS assert False, 'Cannot evaluate the keyword %s' % atom elif isinstance(atom, tree.Literal): string = context.evaluator.compiled_subprocess.safe_literal_eval( atom.value) return ContextSet( [compiled.create_simple_object(context.evaluator, string)]) elif atom.type == 'strings': # Will be multiple string. context_set = eval_atom(context, atom.children[0]) for string in atom.children[1:]: right = eval_atom(context, string) context_set = _eval_comparison(context.evaluator, context, context_set, u'+', right) return context_set elif atom.type == 'fstring': return compiled.get_string_context_set(context.evaluator) else: c = atom.children # Parentheses without commas are not tuples. if c[0] == '(' and not len(c) == 2 \ and not(c[1].type == 'testlist_comp' and len(c[1].children) > 1): return context.eval_node(c[1]) try: comp_for = c[1].children[1] except (IndexError, AttributeError): pass else: if comp_for == ':': # Dict comprehensions have a colon at the 3rd index. try: comp_for = c[1].children[3] except IndexError: pass if comp_for.type in ('comp_for', 'sync_comp_for'): return ContextSet([ iterable.comprehension_from_atom(context.evaluator, context, atom) ]) # It's a dict/list/tuple literal. array_node = c[1] try: array_node_c = array_node.children except AttributeError: array_node_c = [] if c[0] == '{' and (array_node == '}' or ':' in array_node_c or '**' in array_node_c): context = iterable.DictLiteralContext(context.evaluator, context, atom) else: context = iterable.SequenceLiteralContext(context.evaluator, context, atom) return ContextSet([context])
def py__iter__(self, contextualized_node=None): for key in self._dct: yield LazyKnownContext( compiled.create_simple_object(self.evaluator, key))
def py__iter__(self): for key in self._dct: yield LazyKnownContext( compiled.create_simple_object(self.evaluator, key))
def py__iter__(self): for key in self._dct: yield LazyKnownContext(compiled.create_simple_object(self.evaluator, key))