Debian Codegraph @

jedi/inference/finder.py

raw symbols SCIP
1 """
2 Searching for names with given scope and name. This is very central in Jedi and
3 Python. The name resolution is quite complicated with descripter,
4 ``__getattribute__``, ``__getattr__``, ``global``, etc.
5
6 If you want to understand name resolution, please read the first few chapters
7 in http://blog.ionelmc.ro/2015/02/09/understanding-python-metaclasses/.
8
9 Flow checks
10 +++++++++++
11
12 Flow checks are not really mature. There's only a check for ``isinstance``. It
13 would check whether a flow has the form of ``if isinstance(a, type_or_tuple)``.
14 Unfortunately every other thing is being ignored (e.g. a == '' would be easy to
15 check for -> a is a string). There's big potential in these checks.
16 """
17
18 from parso.python.tree import Name
19
20 from jedi import settings
21 from jedi.inference.arguments import TreeArguments
22 from jedi.inference.value import iterable
23 from jedi.inference.base_value import NO_VALUES
24 from jedi.parser_utils import is_scope
25
26
27 def filter_name(filters, name_or_str):
28 """
29 Searches names that are defined in a scope (the different
30 ``filters``), until a name fits.
31 """
32 string_name = name_or_str.value if isinstance(name_or_str, Name) else name_or_str
33 names = []
34 for filter in filters:
35 names = filter.get(string_name)
36 if names:
37 break
38
39 return list(_remove_del_stmt(names))
40
41
42 def _remove_del_stmt(names):
43 # Catch del statements and remove them from results.
44 for name in names:
45 if name.tree_name is not None:
46 definition = name.tree_name.get_definition()
47 if definition is not None and definition.type == 'del_stmt':
48 continue
49 yield name
50
51
52 def check_flow_information(value, flow, search_name, pos):
53 """ Try to find out the type of a variable just with the information that
54 is given by the flows: e.g. It is also responsible for assert checks.::
55
56 if isinstance(k, str):
57 k. # <- completion here
58
59 ensures that `k` is a string.
60 """
61 if not settings.dynamic_flow_information:
62 return None
63
64 result = None
65 if is_scope(flow):
66 # Check for asserts.
67 module_node = flow.get_root_node()
68 try:
69 names = module_node.get_used_names()[search_name.value]
70 except KeyError:
71 return None
72 names = reversed([
73 n for n in names
74 if flow.start_pos <= n.start_pos < (pos or flow.end_pos)
75 ])
76
77 for name in names:
78 ass = name.search_ancestor('assert_stmt')
79 if ass is not None:
80 result = _check_isinstance_type(value, ass.assertion, search_name)
81 if result is not None:
82 return result
83
84 if flow.type in ('if_stmt', 'while_stmt'):
85 potential_ifs = [c for c in flow.children[1::4] if c != ':']
86 for if_test in reversed(potential_ifs):
87 if search_name.start_pos > if_test.end_pos:
88 return _check_isinstance_type(value, if_test, search_name)
89 return result
90
91
92 def _get_isinstance_trailer_arglist(node):
93 if node.type in ('power', 'atom_expr') and len(node.children) == 2:
94 # This might be removed if we analyze and, etc
95 first, trailer = node.children
96 if first.type == 'name' and first.value == 'isinstance' \
97 and trailer.type == 'trailer' and trailer.children[0] == '(':
98 return trailer
99 return None
100
101
102 def _check_isinstance_type(value, node, search_name):
103 lazy_cls = None
104 trailer = _get_isinstance_trailer_arglist(node)
105 if trailer is not None and len(trailer.children) == 3:
106 arglist = trailer.children[1]
107 args = TreeArguments(value.inference_state, value, arglist, trailer)
108 param_list = list(args.unpack())
109 # Disallow keyword arguments
110 if len(param_list) == 2 and len(arglist.children) == 3:
111 (key1, _), (key2, lazy_value_cls) = param_list
112 if key1 is None and key2 is None:
113 call = _get_call_string(search_name)
114 is_instance_call = _get_call_string(arglist.children[0])
115 # Do a simple get_code comparison of the strings . They should
116 # just have the same code, and everything will be all right.
117 # There are ways that this is not correct, if some stuff is
118 # redefined in between. However here we don't care, because
119 # it's a heuristic that works pretty well.
120 if call == is_instance_call:
121 lazy_cls = lazy_value_cls
122 if lazy_cls is None:
123 return None
124
125 value_set = NO_VALUES
126 for cls_or_tup in lazy_cls.infer():
127 if isinstance(cls_or_tup, iterable.Sequence) and cls_or_tup.array_type == 'tuple':
128 for lazy_value in cls_or_tup.py__iter__():
129 value_set |= lazy_value.infer().execute_with_values()
130 else:
131 value_set |= cls_or_tup.execute_with_values()
132 return value_set
133
134
135 def _get_call_string(node):
136 if node.parent.type == 'atom_expr':
137 return _get_call_string(node.parent)
138
139 code = ''
140 leaf = node.get_first_leaf()
141 end = node.get_last_leaf().end_pos
142 while leaf.start_pos < end:
143 code += leaf.value
144 leaf = leaf.get_next_leaf()
145 return code