syntax_tree.py 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. """
  2. Functions inferring the syntax tree.
  3. """
  4. import copy
  5. import itertools
  6. from parso.python import tree
  7. from jedi import debug
  8. from jedi import parser_utils
  9. from jedi.inference.base_value import ValueSet, NO_VALUES, ContextualizedNode, \
  10. iterator_to_value_set, iterate_values
  11. from jedi.inference.lazy_value import LazyTreeValue
  12. from jedi.inference import compiled
  13. from jedi.inference import recursion
  14. from jedi.inference import analysis
  15. from jedi.inference import imports
  16. from jedi.inference import arguments
  17. from jedi.inference.value import ClassValue, FunctionValue
  18. from jedi.inference.value import iterable
  19. from jedi.inference.value.dynamic_arrays import ListModification, DictModification
  20. from jedi.inference.value import TreeInstance
  21. from jedi.inference.helpers import is_string, is_literal, is_number, \
  22. get_names_of_node, is_big_annoying_library
  23. from jedi.inference.compiled.access import COMPARISON_OPERATORS
  24. from jedi.inference.cache import inference_state_method_cache
  25. from jedi.inference.gradual.stub_value import VersionInfo
  26. from jedi.inference.gradual import annotation
  27. from jedi.inference.names import TreeNameDefinition
  28. from jedi.inference.context import CompForContext
  29. from jedi.inference.value.decorator import Decoratee
  30. from jedi.plugins import plugin_manager
  31. operator_to_magic_method = {
  32. '+': '__add__',
  33. '-': '__sub__',
  34. '*': '__mul__',
  35. '@': '__matmul__',
  36. '/': '__truediv__',
  37. '//': '__floordiv__',
  38. '%': '__mod__',
  39. '**': '__pow__',
  40. '<<': '__lshift__',
  41. '>>': '__rshift__',
  42. '&': '__and__',
  43. '|': '__or__',
  44. '^': '__xor__',
  45. }
  46. reverse_operator_to_magic_method = {
  47. k: '__r' + v[2:] for k, v in operator_to_magic_method.items()
  48. }
  49. def _limit_value_infers(func):
  50. """
  51. This is for now the way how we limit type inference going wild. There are
  52. other ways to ensure recursion limits as well. This is mostly necessary
  53. because of instance (self) access that can be quite tricky to limit.
  54. I'm still not sure this is the way to go, but it looks okay for now and we
  55. can still go anther way in the future. Tests are there. ~ dave
  56. """
  57. def wrapper(context, *args, **kwargs):
  58. n = context.tree_node
  59. inference_state = context.inference_state
  60. try:
  61. inference_state.inferred_element_counts[n] += 1
  62. maximum = 300
  63. if context.parent_context is None \
  64. and context.get_value() is inference_state.builtins_module:
  65. # Builtins should have a more generous inference limit.
  66. # It is important that builtins can be executed, otherwise some
  67. # functions that depend on certain builtins features would be
  68. # broken, see e.g. GH #1432
  69. maximum *= 100
  70. if inference_state.inferred_element_counts[n] > maximum:
  71. debug.warning('In value %s there were too many inferences.', n)
  72. return NO_VALUES
  73. except KeyError:
  74. inference_state.inferred_element_counts[n] = 1
  75. return func(context, *args, **kwargs)
  76. return wrapper
  77. def infer_node(context, element):
  78. if isinstance(context, CompForContext):
  79. return _infer_node(context, element)
  80. if_stmt = element
  81. while if_stmt is not None:
  82. if_stmt = if_stmt.parent
  83. if if_stmt.type in ('if_stmt', 'for_stmt'):
  84. break
  85. if parser_utils.is_scope(if_stmt):
  86. if_stmt = None
  87. break
  88. predefined_if_name_dict = context.predefined_names.get(if_stmt)
  89. # TODO there's a lot of issues with this one. We actually should do
  90. # this in a different way. Caching should only be active in certain
  91. # cases and this all sucks.
  92. if predefined_if_name_dict is None and if_stmt \
  93. and if_stmt.type == 'if_stmt' and context.inference_state.is_analysis:
  94. if_stmt_test = if_stmt.children[1]
  95. name_dicts = [{}]
  96. # If we already did a check, we don't want to do it again -> If
  97. # value.predefined_names is filled, we stop.
  98. # We don't want to check the if stmt itself, it's just about
  99. # the content.
  100. if element.start_pos > if_stmt_test.end_pos:
  101. # Now we need to check if the names in the if_stmt match the
  102. # names in the suite.
  103. if_names = get_names_of_node(if_stmt_test)
  104. element_names = get_names_of_node(element)
  105. str_element_names = [e.value for e in element_names]
  106. if any(i.value in str_element_names for i in if_names):
  107. for if_name in if_names:
  108. definitions = context.inference_state.infer(context, if_name)
  109. # Every name that has multiple different definitions
  110. # causes the complexity to rise. The complexity should
  111. # never fall below 1.
  112. if len(definitions) > 1:
  113. if len(name_dicts) * len(definitions) > 16:
  114. debug.dbg('Too many options for if branch inference %s.', if_stmt)
  115. # There's only a certain amount of branches
  116. # Jedi can infer, otherwise it will take to
  117. # long.
  118. name_dicts = [{}]
  119. break
  120. original_name_dicts = list(name_dicts)
  121. name_dicts = []
  122. for definition in definitions:
  123. new_name_dicts = list(original_name_dicts)
  124. for i, name_dict in enumerate(new_name_dicts):
  125. new_name_dicts[i] = name_dict.copy()
  126. new_name_dicts[i][if_name.value] = ValueSet([definition])
  127. name_dicts += new_name_dicts
  128. else:
  129. for name_dict in name_dicts:
  130. name_dict[if_name.value] = definitions
  131. if len(name_dicts) > 1:
  132. result = NO_VALUES
  133. for name_dict in name_dicts:
  134. with context.predefine_names(if_stmt, name_dict):
  135. result |= _infer_node(context, element)
  136. return result
  137. else:
  138. return _infer_node_if_inferred(context, element)
  139. else:
  140. if predefined_if_name_dict:
  141. return _infer_node(context, element)
  142. else:
  143. return _infer_node_if_inferred(context, element)
  144. def _infer_node_if_inferred(context, element):
  145. """
  146. TODO This function is temporary: Merge with infer_node.
  147. """
  148. parent = element
  149. while parent is not None:
  150. parent = parent.parent
  151. predefined_if_name_dict = context.predefined_names.get(parent)
  152. if predefined_if_name_dict is not None:
  153. return _infer_node(context, element)
  154. return _infer_node_cached(context, element)
  155. @inference_state_method_cache(default=NO_VALUES)
  156. def _infer_node_cached(context, element):
  157. return _infer_node(context, element)
  158. @debug.increase_indent
  159. @_limit_value_infers
  160. def _infer_node(context, element):
  161. debug.dbg('infer_node %s@%s in %s', element, element.start_pos, context)
  162. inference_state = context.inference_state
  163. typ = element.type
  164. if typ in ('name', 'number', 'string', 'atom', 'strings', 'keyword', 'fstring'):
  165. return infer_atom(context, element)
  166. elif typ == 'lambdef':
  167. return ValueSet([FunctionValue.from_context(context, element)])
  168. elif typ == 'expr_stmt':
  169. return infer_expr_stmt(context, element)
  170. elif typ in ('power', 'atom_expr'):
  171. first_child = element.children[0]
  172. children = element.children[1:]
  173. had_await = False
  174. if first_child.type == 'keyword' and first_child.value == 'await':
  175. had_await = True
  176. first_child = children.pop(0)
  177. value_set = context.infer_node(first_child)
  178. for (i, trailer) in enumerate(children):
  179. if trailer == '**': # has a power operation.
  180. right = context.infer_node(children[i + 1])
  181. value_set = _infer_comparison(
  182. context,
  183. value_set,
  184. trailer,
  185. right
  186. )
  187. break
  188. value_set = infer_trailer(context, value_set, trailer)
  189. if had_await:
  190. return value_set.py__await__().py__stop_iteration_returns()
  191. return value_set
  192. elif typ in ('testlist_star_expr', 'testlist',):
  193. # The implicit tuple in statements.
  194. return ValueSet([iterable.SequenceLiteralValue(inference_state, context, element)])
  195. elif typ in ('not_test', 'factor'):
  196. value_set = context.infer_node(element.children[-1])
  197. for operator in element.children[:-1]:
  198. value_set = infer_factor(value_set, operator)
  199. return value_set
  200. elif typ == 'test':
  201. # `x if foo else y` case.
  202. return (context.infer_node(element.children[0])
  203. | context.infer_node(element.children[-1]))
  204. elif typ == 'operator':
  205. # Must be an ellipsis, other operators are not inferred.
  206. if element.value != '...':
  207. origin = element.parent
  208. raise AssertionError("unhandled operator %s in %s " % (repr(element.value), origin))
  209. return ValueSet([compiled.builtin_from_name(inference_state, 'Ellipsis')])
  210. elif typ == 'dotted_name':
  211. value_set = infer_atom(context, element.children[0])
  212. for next_name in element.children[2::2]:
  213. value_set = value_set.py__getattribute__(next_name, name_context=context)
  214. return value_set
  215. elif typ == 'eval_input':
  216. return context.infer_node(element.children[0])
  217. elif typ == 'annassign':
  218. return annotation.infer_annotation(context, element.children[1]) \
  219. .execute_annotation()
  220. elif typ == 'yield_expr':
  221. if len(element.children) and element.children[1].type == 'yield_arg':
  222. # Implies that it's a yield from.
  223. element = element.children[1].children[1]
  224. generators = context.infer_node(element) \
  225. .py__getattribute__('__iter__').execute_with_values()
  226. return generators.py__stop_iteration_returns()
  227. # Generator.send() is not implemented.
  228. return NO_VALUES
  229. elif typ == 'namedexpr_test':
  230. return context.infer_node(element.children[2])
  231. else:
  232. return infer_or_test(context, element)
  233. def infer_trailer(context, atom_values, trailer):
  234. trailer_op, node = trailer.children[:2]
  235. if node == ')': # `arglist` is optional.
  236. node = None
  237. if trailer_op == '[':
  238. trailer_op, node, _ = trailer.children
  239. return atom_values.get_item(
  240. _infer_subscript_list(context, node),
  241. ContextualizedNode(context, trailer)
  242. )
  243. else:
  244. debug.dbg('infer_trailer: %s in %s', trailer, atom_values)
  245. if trailer_op == '.':
  246. return atom_values.py__getattribute__(
  247. name_context=context,
  248. name_or_str=node
  249. )
  250. else:
  251. assert trailer_op == '(', 'trailer_op is actually %s' % trailer_op
  252. args = arguments.TreeArguments(context.inference_state, context, node, trailer)
  253. return atom_values.execute(args)
  254. def infer_atom(context, atom):
  255. """
  256. Basically to process ``atom`` nodes. The parser sometimes doesn't
  257. generate the node (because it has just one child). In that case an atom
  258. might be a name or a literal as well.
  259. """
  260. state = context.inference_state
  261. if atom.type == 'name':
  262. # This is the first global lookup.
  263. stmt = tree.search_ancestor(atom, 'expr_stmt', 'lambdef', 'if_stmt') or atom
  264. if stmt.type == 'if_stmt':
  265. if not any(n.start_pos <= atom.start_pos < n.end_pos for n in stmt.get_test_nodes()):
  266. stmt = atom
  267. elif stmt.type == 'lambdef':
  268. stmt = atom
  269. position = stmt.start_pos
  270. if _is_annotation_name(atom):
  271. # Since Python 3.7 (with from __future__ import annotations),
  272. # annotations are essentially strings and can reference objects
  273. # that are defined further down in code. Therefore just set the
  274. # position to None, so the finder will not try to stop at a certain
  275. # position in the module.
  276. position = None
  277. return context.py__getattribute__(atom, position=position)
  278. elif atom.type == 'keyword':
  279. # For False/True/None
  280. if atom.value in ('False', 'True', 'None'):
  281. return ValueSet([compiled.builtin_from_name(state, atom.value)])
  282. elif atom.value == 'yield':
  283. # Contrary to yield from, yield can just appear alone to return a
  284. # value when used with `.send()`.
  285. return NO_VALUES
  286. assert False, 'Cannot infer the keyword %s' % atom
  287. elif isinstance(atom, tree.Literal):
  288. string = state.compiled_subprocess.safe_literal_eval(atom.value)
  289. return ValueSet([compiled.create_simple_object(state, string)])
  290. elif atom.type == 'strings':
  291. # Will be multiple string.
  292. value_set = infer_atom(context, atom.children[0])
  293. for string in atom.children[1:]:
  294. right = infer_atom(context, string)
  295. value_set = _infer_comparison(context, value_set, '+', right)
  296. return value_set
  297. elif atom.type == 'fstring':
  298. return compiled.get_string_value_set(state)
  299. else:
  300. c = atom.children
  301. # Parentheses without commas are not tuples.
  302. if c[0] == '(' and not len(c) == 2 \
  303. and not (c[1].type == 'testlist_comp'
  304. and len(c[1].children) > 1):
  305. return context.infer_node(c[1])
  306. try:
  307. comp_for = c[1].children[1]
  308. except (IndexError, AttributeError):
  309. pass
  310. else:
  311. if comp_for == ':':
  312. # Dict comprehensions have a colon at the 3rd index.
  313. try:
  314. comp_for = c[1].children[3]
  315. except IndexError:
  316. pass
  317. if comp_for.type in ('comp_for', 'sync_comp_for'):
  318. return ValueSet([iterable.comprehension_from_atom(
  319. state, context, atom
  320. )])
  321. # It's a dict/list/tuple literal.
  322. array_node = c[1]
  323. try:
  324. array_node_c = array_node.children
  325. except AttributeError:
  326. array_node_c = []
  327. if c[0] == '{' and (array_node == '}' or ':' in array_node_c
  328. or '**' in array_node_c):
  329. new_value = iterable.DictLiteralValue(state, context, atom)
  330. else:
  331. new_value = iterable.SequenceLiteralValue(state, context, atom)
  332. return ValueSet([new_value])
  333. @_limit_value_infers
  334. def infer_expr_stmt(context, stmt, seek_name=None):
  335. with recursion.execution_allowed(context.inference_state, stmt) as allowed:
  336. if allowed:
  337. if seek_name is not None:
  338. pep0484_values = \
  339. annotation.find_type_from_comment_hint_assign(context, stmt, seek_name)
  340. if pep0484_values:
  341. return pep0484_values
  342. return _infer_expr_stmt(context, stmt, seek_name)
  343. return NO_VALUES
  344. @debug.increase_indent
  345. def _infer_expr_stmt(context, stmt, seek_name=None):
  346. """
  347. The starting point of the completion. A statement always owns a call
  348. list, which are the calls, that a statement does. In case multiple
  349. names are defined in the statement, `seek_name` returns the result for
  350. this name.
  351. expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
  352. ('=' (yield_expr|testlist_star_expr))*)
  353. annassign: ':' test ['=' test]
  354. augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
  355. '<<=' | '>>=' | '**=' | '//=')
  356. :param stmt: A `tree.ExprStmt`.
  357. """
  358. def check_setitem(stmt):
  359. atom_expr = stmt.children[0]
  360. if atom_expr.type not in ('atom_expr', 'power'):
  361. return False, None
  362. name = atom_expr.children[0]
  363. if name.type != 'name' or len(atom_expr.children) != 2:
  364. return False, None
  365. trailer = atom_expr.children[-1]
  366. return trailer.children[0] == '[', trailer.children[1]
  367. debug.dbg('infer_expr_stmt %s (%s)', stmt, seek_name)
  368. rhs = stmt.get_rhs()
  369. value_set = context.infer_node(rhs)
  370. if seek_name:
  371. n = TreeNameDefinition(context, seek_name)
  372. value_set = check_tuple_assignments(n, value_set)
  373. first_operator = next(stmt.yield_operators(), None)
  374. is_setitem, subscriptlist = check_setitem(stmt)
  375. is_annassign = first_operator not in ('=', None) and first_operator.type == 'operator'
  376. if is_annassign or is_setitem:
  377. # `=` is always the last character in aug assignments -> -1
  378. name = stmt.get_defined_names(include_setitem=True)[0].value
  379. left_values = context.py__getattribute__(name, position=stmt.start_pos)
  380. if is_setitem:
  381. def to_mod(v):
  382. c = ContextualizedSubscriptListNode(context, subscriptlist)
  383. if v.array_type == 'dict':
  384. return DictModification(v, value_set, c)
  385. elif v.array_type == 'list':
  386. return ListModification(v, value_set, c)
  387. return v
  388. value_set = ValueSet(to_mod(v) for v in left_values)
  389. else:
  390. operator = copy.copy(first_operator)
  391. operator.value = operator.value[:-1]
  392. for_stmt = tree.search_ancestor(stmt, 'for_stmt')
  393. if for_stmt is not None and for_stmt.type == 'for_stmt' and value_set \
  394. and parser_utils.for_stmt_defines_one_name(for_stmt):
  395. # Iterate through result and add the values, that's possible
  396. # only in for loops without clutter, because they are
  397. # predictable. Also only do it, if the variable is not a tuple.
  398. node = for_stmt.get_testlist()
  399. cn = ContextualizedNode(context, node)
  400. ordered = list(cn.infer().iterate(cn))
  401. for lazy_value in ordered:
  402. dct = {for_stmt.children[1].value: lazy_value.infer()}
  403. with context.predefine_names(for_stmt, dct):
  404. t = context.infer_node(rhs)
  405. left_values = _infer_comparison(context, left_values, operator, t)
  406. value_set = left_values
  407. else:
  408. value_set = _infer_comparison(context, left_values, operator, value_set)
  409. debug.dbg('infer_expr_stmt result %s', value_set)
  410. return value_set
  411. def infer_or_test(context, or_test):
  412. iterator = iter(or_test.children)
  413. types = context.infer_node(next(iterator))
  414. for operator in iterator:
  415. right = next(iterator)
  416. if operator.type == 'comp_op': # not in / is not
  417. operator = ' '.join(c.value for c in operator.children)
  418. # handle type inference of and/or here.
  419. if operator in ('and', 'or'):
  420. left_bools = set(left.py__bool__() for left in types)
  421. if left_bools == {True}:
  422. if operator == 'and':
  423. types = context.infer_node(right)
  424. elif left_bools == {False}:
  425. if operator != 'and':
  426. types = context.infer_node(right)
  427. # Otherwise continue, because of uncertainty.
  428. else:
  429. types = _infer_comparison(context, types, operator,
  430. context.infer_node(right))
  431. debug.dbg('infer_or_test types %s', types)
  432. return types
  433. @iterator_to_value_set
  434. def infer_factor(value_set, operator):
  435. """
  436. Calculates `+`, `-`, `~` and `not` prefixes.
  437. """
  438. for value in value_set:
  439. if operator == '-':
  440. if is_number(value):
  441. yield value.negate()
  442. elif operator == 'not':
  443. b = value.py__bool__()
  444. if b is None: # Uncertainty.
  445. yield list(value.inference_state.builtins_module.py__getattribute__('bool')
  446. .execute_annotation()).pop()
  447. else:
  448. yield compiled.create_simple_object(value.inference_state, not b)
  449. else:
  450. yield value
  451. def _literals_to_types(inference_state, result):
  452. # Changes literals ('a', 1, 1.0, etc) to its type instances (str(),
  453. # int(), float(), etc).
  454. new_result = NO_VALUES
  455. for typ in result:
  456. if is_literal(typ):
  457. # Literals are only valid as long as the operations are
  458. # correct. Otherwise add a value-free instance.
  459. cls = compiled.builtin_from_name(inference_state, typ.name.string_name)
  460. new_result |= cls.execute_with_values()
  461. else:
  462. new_result |= ValueSet([typ])
  463. return new_result
  464. def _infer_comparison(context, left_values, operator, right_values):
  465. state = context.inference_state
  466. if isinstance(operator, str):
  467. operator_str = operator
  468. else:
  469. operator_str = str(operator.value)
  470. if not left_values or not right_values:
  471. # illegal slices e.g. cause left/right_result to be None
  472. result = (left_values or NO_VALUES) | (right_values or NO_VALUES)
  473. return _literals_to_types(state, result)
  474. elif operator_str == "|" and all(
  475. value.is_class() or value.is_compiled()
  476. for value in itertools.chain(left_values, right_values)
  477. ):
  478. # ^^^ A naive hack for PEP 604
  479. return ValueSet.from_sets((left_values, right_values))
  480. else:
  481. # I don't think there's a reasonable chance that a string
  482. # operation is still correct, once we pass something like six
  483. # objects.
  484. if len(left_values) * len(right_values) > 6:
  485. return _literals_to_types(state, left_values | right_values)
  486. else:
  487. return ValueSet.from_sets(
  488. _infer_comparison_part(state, context, left, operator, right)
  489. for left in left_values
  490. for right in right_values
  491. )
  492. def _is_annotation_name(name):
  493. ancestor = tree.search_ancestor(name, 'param', 'funcdef', 'expr_stmt')
  494. if ancestor is None:
  495. return False
  496. if ancestor.type in ('param', 'funcdef'):
  497. ann = ancestor.annotation
  498. if ann is not None:
  499. return ann.start_pos <= name.start_pos < ann.end_pos
  500. elif ancestor.type == 'expr_stmt':
  501. c = ancestor.children
  502. if len(c) > 1 and c[1].type == 'annassign':
  503. return c[1].start_pos <= name.start_pos < c[1].end_pos
  504. return False
  505. def _is_list(value):
  506. return value.array_type == 'list'
  507. def _is_tuple(value):
  508. return value.array_type == 'tuple'
  509. def _bool_to_value(inference_state, bool_):
  510. return compiled.builtin_from_name(inference_state, str(bool_))
  511. def _get_tuple_ints(value):
  512. if not isinstance(value, iterable.SequenceLiteralValue):
  513. return None
  514. numbers = []
  515. for lazy_value in value.py__iter__():
  516. if not isinstance(lazy_value, LazyTreeValue):
  517. return None
  518. node = lazy_value.data
  519. if node.type != 'number':
  520. return None
  521. try:
  522. numbers.append(int(node.value))
  523. except ValueError:
  524. return None
  525. return numbers
  526. def _infer_comparison_part(inference_state, context, left, operator, right):
  527. l_is_num = is_number(left)
  528. r_is_num = is_number(right)
  529. if isinstance(operator, str):
  530. str_operator = operator
  531. else:
  532. str_operator = str(operator.value)
  533. if str_operator == '*':
  534. # for iterables, ignore * operations
  535. if isinstance(left, iterable.Sequence) or is_string(left):
  536. return ValueSet([left])
  537. elif isinstance(right, iterable.Sequence) or is_string(right):
  538. return ValueSet([right])
  539. elif str_operator == '+':
  540. if l_is_num and r_is_num or is_string(left) and is_string(right):
  541. return left.execute_operation(right, str_operator)
  542. elif _is_list(left) and _is_list(right) or _is_tuple(left) and _is_tuple(right):
  543. return ValueSet([iterable.MergedArray(inference_state, (left, right))])
  544. elif str_operator == '-':
  545. if l_is_num and r_is_num:
  546. return left.execute_operation(right, str_operator)
  547. elif str_operator == '%':
  548. # With strings and numbers the left type typically remains. Except for
  549. # `int() % float()`.
  550. return ValueSet([left])
  551. elif str_operator in COMPARISON_OPERATORS:
  552. if left.is_compiled() and right.is_compiled():
  553. # Possible, because the return is not an option. Just compare.
  554. result = left.execute_operation(right, str_operator)
  555. if result:
  556. return result
  557. else:
  558. if str_operator in ('is', '!=', '==', 'is not'):
  559. operation = COMPARISON_OPERATORS[str_operator]
  560. bool_ = operation(left, right)
  561. # Only if == returns True or != returns False, we can continue.
  562. # There's no guarantee that they are not equal. This can help
  563. # in some cases, but does not cover everything.
  564. if (str_operator in ('is', '==')) == bool_:
  565. return ValueSet([_bool_to_value(inference_state, bool_)])
  566. if isinstance(left, VersionInfo):
  567. version_info = _get_tuple_ints(right)
  568. if version_info is not None:
  569. bool_result = compiled.access.COMPARISON_OPERATORS[operator](
  570. inference_state.environment.version_info,
  571. tuple(version_info)
  572. )
  573. return ValueSet([_bool_to_value(inference_state, bool_result)])
  574. return ValueSet([
  575. _bool_to_value(inference_state, True),
  576. _bool_to_value(inference_state, False)
  577. ])
  578. elif str_operator in ('in', 'not in'):
  579. return inference_state.builtins_module.py__getattribute__('bool').execute_annotation()
  580. def check(obj):
  581. """Checks if a Jedi object is either a float or an int."""
  582. return isinstance(obj, TreeInstance) and \
  583. obj.name.string_name in ('int', 'float')
  584. # Static analysis, one is a number, the other one is not.
  585. if str_operator in ('+', '-') and l_is_num != r_is_num \
  586. and not (check(left) or check(right)):
  587. message = "TypeError: unsupported operand type(s) for +: %s and %s"
  588. analysis.add(context, 'type-error-operation', operator,
  589. message % (left, right))
  590. if left.is_class() or right.is_class():
  591. return NO_VALUES
  592. method_name = operator_to_magic_method[str_operator]
  593. magic_methods = left.py__getattribute__(method_name)
  594. if magic_methods:
  595. result = magic_methods.execute_with_values(right)
  596. if result:
  597. return result
  598. if not magic_methods:
  599. reverse_method_name = reverse_operator_to_magic_method[str_operator]
  600. magic_methods = right.py__getattribute__(reverse_method_name)
  601. result = magic_methods.execute_with_values(left)
  602. if result:
  603. return result
  604. result = ValueSet([left, right])
  605. debug.dbg('Used operator %s resulting in %s', operator, result)
  606. return result
  607. @plugin_manager.decorate()
  608. def tree_name_to_values(inference_state, context, tree_name):
  609. value_set = NO_VALUES
  610. module_node = context.get_root_context().tree_node
  611. # First check for annotations, like: `foo: int = 3`
  612. if module_node is not None:
  613. names = module_node.get_used_names().get(tree_name.value, [])
  614. found_annotation = False
  615. for name in names:
  616. expr_stmt = name.parent
  617. if expr_stmt.type == "expr_stmt" and expr_stmt.children[1].type == "annassign":
  618. correct_scope = parser_utils.get_parent_scope(name) == context.tree_node
  619. ann_assign = expr_stmt.children[1]
  620. if correct_scope:
  621. found_annotation = True
  622. if (
  623. (ann_assign.children[1].type == 'name')
  624. and (ann_assign.children[1].value == tree_name.value)
  625. and context.parent_context
  626. ):
  627. context = context.parent_context
  628. value_set |= annotation.infer_annotation(
  629. context, expr_stmt.children[1].children[1]
  630. ).execute_annotation()
  631. if found_annotation:
  632. return value_set
  633. types = []
  634. node = tree_name.get_definition(import_name_always=True, include_setitem=True)
  635. if node is None:
  636. node = tree_name.parent
  637. if node.type == 'global_stmt':
  638. c = context.create_context(tree_name)
  639. if c.is_module():
  640. # In case we are already part of the module, there is no point
  641. # in looking up the global statement anymore, because it's not
  642. # valid at that point anyway.
  643. return NO_VALUES
  644. # For global_stmt lookups, we only need the first possible scope,
  645. # which means the function itself.
  646. filter = next(c.get_filters())
  647. names = filter.get(tree_name.value)
  648. return ValueSet.from_sets(name.infer() for name in names)
  649. elif node.type not in ('import_from', 'import_name'):
  650. c = context.create_context(tree_name)
  651. return infer_atom(c, tree_name)
  652. typ = node.type
  653. if typ == 'for_stmt':
  654. types = annotation.find_type_from_comment_hint_for(context, node, tree_name)
  655. if types:
  656. return types
  657. if typ == 'with_stmt':
  658. types = annotation.find_type_from_comment_hint_with(context, node, tree_name)
  659. if types:
  660. return types
  661. if typ in ('for_stmt', 'comp_for', 'sync_comp_for'):
  662. try:
  663. types = context.predefined_names[node][tree_name.value]
  664. except KeyError:
  665. cn = ContextualizedNode(context, node.children[3])
  666. for_types = iterate_values(
  667. cn.infer(),
  668. contextualized_node=cn,
  669. is_async=node.parent.type == 'async_stmt',
  670. )
  671. n = TreeNameDefinition(context, tree_name)
  672. types = check_tuple_assignments(n, for_types)
  673. elif typ == 'expr_stmt':
  674. types = infer_expr_stmt(context, node, tree_name)
  675. elif typ == 'with_stmt':
  676. value_managers = context.infer_node(node.get_test_node_from_name(tree_name))
  677. if node.parent.type == 'async_stmt':
  678. # In the case of `async with` statements, we need to
  679. # first get the coroutine from the `__aenter__` method,
  680. # then "unwrap" via the `__await__` method
  681. enter_methods = value_managers.py__getattribute__('__aenter__')
  682. coro = enter_methods.execute_with_values()
  683. return coro.py__await__().py__stop_iteration_returns()
  684. enter_methods = value_managers.py__getattribute__('__enter__')
  685. return enter_methods.execute_with_values()
  686. elif typ in ('import_from', 'import_name'):
  687. types = imports.infer_import(context, tree_name)
  688. elif typ in ('funcdef', 'classdef'):
  689. types = _apply_decorators(context, node)
  690. elif typ == 'try_stmt':
  691. # TODO an exception can also be a tuple. Check for those.
  692. # TODO check for types that are not classes and add it to
  693. # the static analysis report.
  694. exceptions = context.infer_node(tree_name.get_previous_sibling().get_previous_sibling())
  695. types = exceptions.execute_with_values()
  696. elif typ == 'param':
  697. types = NO_VALUES
  698. elif typ == 'del_stmt':
  699. types = NO_VALUES
  700. elif typ == 'namedexpr_test':
  701. types = infer_node(context, node)
  702. else:
  703. raise ValueError("Should not happen. type: %s" % typ)
  704. return types
  705. # We don't want to have functions/classes that are created by the same
  706. # tree_node.
  707. @inference_state_method_cache()
  708. def _apply_decorators(context, node):
  709. """
  710. Returns the function, that should to be executed in the end.
  711. This is also the places where the decorators are processed.
  712. """
  713. if node.type == 'classdef':
  714. decoratee_value = ClassValue(
  715. context.inference_state,
  716. parent_context=context,
  717. tree_node=node
  718. )
  719. else:
  720. decoratee_value = FunctionValue.from_context(context, node)
  721. initial = values = ValueSet([decoratee_value])
  722. if is_big_annoying_library(context):
  723. return values
  724. for dec in reversed(node.get_decorators()):
  725. debug.dbg('decorator: %s %s', dec, values, color="MAGENTA")
  726. with debug.increase_indent_cm():
  727. dec_values = context.infer_node(dec.children[1])
  728. trailer_nodes = dec.children[2:-1]
  729. if trailer_nodes:
  730. # Create a trailer and infer it.
  731. trailer = tree.PythonNode('trailer', trailer_nodes)
  732. trailer.parent = dec
  733. dec_values = infer_trailer(context, dec_values, trailer)
  734. if not len(dec_values):
  735. code = dec.get_code(include_prefix=False)
  736. # For the short future, we don't want to hear about the runtime
  737. # decorator in typing that was intentionally omitted. This is not
  738. # "correct", but helps with debugging.
  739. if code != '@runtime\n':
  740. debug.warning('decorator not found: %s on %s', dec, node)
  741. return initial
  742. values = dec_values.execute(arguments.ValuesArguments([values]))
  743. if not len(values):
  744. debug.warning('not possible to resolve wrappers found %s', node)
  745. return initial
  746. debug.dbg('decorator end %s', values, color="MAGENTA")
  747. if values != initial:
  748. return ValueSet([Decoratee(c, decoratee_value) for c in values])
  749. return values
  750. def check_tuple_assignments(name, value_set):
  751. """
  752. Checks if tuples are assigned.
  753. """
  754. lazy_value = None
  755. for index, node in name.assignment_indexes():
  756. cn = ContextualizedNode(name.parent_context, node)
  757. iterated = value_set.iterate(cn)
  758. if isinstance(index, slice):
  759. # For no star unpacking is not possible.
  760. return NO_VALUES
  761. i = 0
  762. while i <= index:
  763. try:
  764. lazy_value = next(iterated)
  765. except StopIteration:
  766. # We could do this with the default param in next. But this
  767. # would allow this loop to run for a very long time if the
  768. # index number is high. Therefore break if the loop is
  769. # finished.
  770. return NO_VALUES
  771. else:
  772. i += lazy_value.max
  773. value_set = lazy_value.infer()
  774. return value_set
  775. class ContextualizedSubscriptListNode(ContextualizedNode):
  776. def infer(self):
  777. return _infer_subscript_list(self.context, self.node)
  778. def _infer_subscript_list(context, index):
  779. """
  780. Handles slices in subscript nodes.
  781. """
  782. if index == ':':
  783. # Like array[:]
  784. return ValueSet([iterable.Slice(context, None, None, None)])
  785. elif index.type == 'subscript' and not index.children[0] == '.':
  786. # subscript basically implies a slice operation
  787. # e.g. array[:3]
  788. result = []
  789. for el in index.children:
  790. if el == ':':
  791. if not result:
  792. result.append(None)
  793. elif el.type == 'sliceop':
  794. if len(el.children) == 2:
  795. result.append(el.children[1])
  796. else:
  797. result.append(el)
  798. result += [None] * (3 - len(result))
  799. return ValueSet([iterable.Slice(context, *result)])
  800. elif index.type == 'subscriptlist':
  801. return ValueSet([iterable.SequenceLiteralValue(context.inference_state, context, index)])
  802. # No slices
  803. return context.infer_node(index)