annotation.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. """
  2. PEP 0484 ( https://www.python.org/dev/peps/pep-0484/ ) describes type hints
  3. through function annotations. There is a strong suggestion in this document
  4. that only the type of type hinting defined in PEP0484 should be allowed
  5. as annotations in future python versions.
  6. """
  7. import re
  8. from inspect import Parameter
  9. from parso import ParserSyntaxError, parse
  10. from jedi.inference.cache import inference_state_method_cache
  11. from jedi.inference.base_value import ValueSet, NO_VALUES
  12. from jedi.inference.gradual.base import DefineGenericBaseClass, GenericClass
  13. from jedi.inference.gradual.generics import TupleGenericManager
  14. from jedi.inference.gradual.type_var import TypeVar
  15. from jedi.inference.helpers import is_string
  16. from jedi.inference.compiled import builtin_from_name
  17. from jedi.inference.param import get_executed_param_names
  18. from jedi import debug
  19. from jedi import parser_utils
  20. def infer_annotation(context, annotation):
  21. """
  22. Inferes an annotation node. This means that it inferes the part of
  23. `int` here:
  24. foo: int = 3
  25. Also checks for forward references (strings)
  26. """
  27. value_set = context.infer_node(annotation)
  28. if len(value_set) != 1:
  29. debug.warning("Inferred typing index %s should lead to 1 object, "
  30. " not %s" % (annotation, value_set))
  31. return value_set
  32. inferred_value = list(value_set)[0]
  33. if is_string(inferred_value):
  34. result = _get_forward_reference_node(context, inferred_value.get_safe_value())
  35. if result is not None:
  36. return context.infer_node(result)
  37. return value_set
  38. def _infer_annotation_string(context, string, index=None):
  39. node = _get_forward_reference_node(context, string)
  40. if node is None:
  41. return NO_VALUES
  42. value_set = context.infer_node(node)
  43. if index is not None:
  44. value_set = value_set.filter(
  45. lambda value: (
  46. value.array_type == 'tuple'
  47. and len(list(value.py__iter__())) >= index
  48. )
  49. ).py__simple_getitem__(index)
  50. return value_set
  51. def _get_forward_reference_node(context, string):
  52. try:
  53. new_node = context.inference_state.grammar.parse(
  54. string,
  55. start_symbol='eval_input',
  56. error_recovery=False
  57. )
  58. except ParserSyntaxError:
  59. debug.warning('Annotation not parsed: %s' % string)
  60. return None
  61. else:
  62. module = context.tree_node.get_root_node()
  63. parser_utils.move(new_node, module.end_pos[0])
  64. new_node.parent = context.tree_node
  65. return new_node
  66. def _split_comment_param_declaration(decl_text):
  67. """
  68. Split decl_text on commas, but group generic expressions
  69. together.
  70. For example, given "foo, Bar[baz, biz]" we return
  71. ['foo', 'Bar[baz, biz]'].
  72. """
  73. try:
  74. node = parse(decl_text, error_recovery=False).children[0]
  75. except ParserSyntaxError:
  76. debug.warning('Comment annotation is not valid Python: %s' % decl_text)
  77. return []
  78. if node.type in ['name', 'atom_expr', 'power']:
  79. return [node.get_code().strip()]
  80. params = []
  81. try:
  82. children = node.children
  83. except AttributeError:
  84. return []
  85. else:
  86. for child in children:
  87. if child.type in ['name', 'atom_expr', 'power']:
  88. params.append(child.get_code().strip())
  89. return params
  90. @inference_state_method_cache()
  91. def infer_param(function_value, param, ignore_stars=False):
  92. values = _infer_param(function_value, param)
  93. if ignore_stars or not values:
  94. return values
  95. inference_state = function_value.inference_state
  96. if param.star_count == 1:
  97. tuple_ = builtin_from_name(inference_state, 'tuple')
  98. return ValueSet([GenericClass(
  99. tuple_,
  100. TupleGenericManager((values,)),
  101. )])
  102. elif param.star_count == 2:
  103. dct = builtin_from_name(inference_state, 'dict')
  104. generics = (
  105. ValueSet([builtin_from_name(inference_state, 'str')]),
  106. values
  107. )
  108. return ValueSet([GenericClass(
  109. dct,
  110. TupleGenericManager(generics),
  111. )])
  112. return values
  113. def _infer_param(function_value, param):
  114. """
  115. Infers the type of a function parameter, using type annotations.
  116. """
  117. annotation = param.annotation
  118. if annotation is None:
  119. # If no Python 3-style annotation, look for a comment annotation.
  120. # Identify parameters to function in the same sequence as they would
  121. # appear in a type comment.
  122. all_params = [child for child in param.parent.children
  123. if child.type == 'param']
  124. node = param.parent.parent
  125. comment = parser_utils.get_following_comment_same_line(node)
  126. if comment is None:
  127. return NO_VALUES
  128. match = re.match(r"^#\s*type:\s*\(([^#]*)\)\s*->", comment)
  129. if not match:
  130. return NO_VALUES
  131. params_comments = _split_comment_param_declaration(match.group(1))
  132. # Find the specific param being investigated
  133. index = all_params.index(param)
  134. # If the number of parameters doesn't match length of type comment,
  135. # ignore first parameter (assume it's self).
  136. if len(params_comments) != len(all_params):
  137. debug.warning(
  138. "Comments length != Params length %s %s",
  139. params_comments, all_params
  140. )
  141. if function_value.is_bound_method():
  142. if index == 0:
  143. # Assume it's self, which is already handled
  144. return NO_VALUES
  145. index -= 1
  146. if index >= len(params_comments):
  147. return NO_VALUES
  148. param_comment = params_comments[index]
  149. return _infer_annotation_string(
  150. function_value.get_default_param_context(),
  151. param_comment
  152. )
  153. # Annotations are like default params and resolve in the same way.
  154. context = function_value.get_default_param_context()
  155. return infer_annotation(context, annotation)
  156. def py__annotations__(funcdef):
  157. dct = {}
  158. for function_param in funcdef.get_params():
  159. param_annotation = function_param.annotation
  160. if param_annotation is not None:
  161. dct[function_param.name.value] = param_annotation
  162. return_annotation = funcdef.annotation
  163. if return_annotation:
  164. dct['return'] = return_annotation
  165. return dct
  166. def resolve_forward_references(context, all_annotations):
  167. def resolve(node):
  168. if node is None or node.type != 'string':
  169. return node
  170. node = _get_forward_reference_node(
  171. context,
  172. context.inference_state.compiled_subprocess.safe_literal_eval(
  173. node.value,
  174. ),
  175. )
  176. if node is None:
  177. # There was a string, but it's not a valid annotation
  178. return None
  179. # The forward reference tree has an additional root node ('eval_input')
  180. # that we don't want. Extract the node we do want, that is equivalent to
  181. # the nodes returned by `py__annotations__` for a non-quoted node.
  182. node = node.children[0]
  183. return node
  184. return {name: resolve(node) for name, node in all_annotations.items()}
  185. @inference_state_method_cache()
  186. def infer_return_types(function, arguments):
  187. """
  188. Infers the type of a function's return value,
  189. according to type annotations.
  190. """
  191. context = function.get_default_param_context()
  192. all_annotations = resolve_forward_references(
  193. context,
  194. py__annotations__(function.tree_node),
  195. )
  196. annotation = all_annotations.get("return", None)
  197. if annotation is None:
  198. # If there is no Python 3-type annotation, look for an annotation
  199. # comment.
  200. node = function.tree_node
  201. comment = parser_utils.get_following_comment_same_line(node)
  202. if comment is None:
  203. return NO_VALUES
  204. match = re.match(r"^#\s*type:\s*\([^#]*\)\s*->\s*([^#]*)", comment)
  205. if not match:
  206. return NO_VALUES
  207. return _infer_annotation_string(
  208. context,
  209. match.group(1).strip()
  210. ).execute_annotation()
  211. unknown_type_vars = find_unknown_type_vars(context, annotation)
  212. annotation_values = infer_annotation(context, annotation)
  213. if not unknown_type_vars:
  214. return annotation_values.execute_annotation()
  215. type_var_dict = infer_type_vars_for_execution(function, arguments, all_annotations)
  216. return ValueSet.from_sets(
  217. ann.define_generics(type_var_dict)
  218. if isinstance(ann, (DefineGenericBaseClass, TypeVar)) else ValueSet({ann})
  219. for ann in annotation_values
  220. ).execute_annotation()
  221. def infer_type_vars_for_execution(function, arguments, annotation_dict):
  222. """
  223. Some functions use type vars that are not defined by the class, but rather
  224. only defined in the function. See for example `iter`. In those cases we
  225. want to:
  226. 1. Search for undefined type vars.
  227. 2. Infer type vars with the execution state we have.
  228. 3. Return the union of all type vars that have been found.
  229. """
  230. context = function.get_default_param_context()
  231. annotation_variable_results = {}
  232. executed_param_names = get_executed_param_names(function, arguments)
  233. for executed_param_name in executed_param_names:
  234. try:
  235. annotation_node = annotation_dict[executed_param_name.string_name]
  236. except KeyError:
  237. continue
  238. annotation_variables = find_unknown_type_vars(context, annotation_node)
  239. if annotation_variables:
  240. # Infer unknown type var
  241. annotation_value_set = context.infer_node(annotation_node)
  242. kind = executed_param_name.get_kind()
  243. actual_value_set = executed_param_name.infer()
  244. if kind is Parameter.VAR_POSITIONAL:
  245. actual_value_set = actual_value_set.merge_types_of_iterate()
  246. elif kind is Parameter.VAR_KEYWORD:
  247. # TODO _dict_values is not public.
  248. actual_value_set = actual_value_set.try_merge('_dict_values')
  249. merge_type_var_dicts(
  250. annotation_variable_results,
  251. annotation_value_set.infer_type_vars(actual_value_set),
  252. )
  253. return annotation_variable_results
  254. def infer_return_for_callable(arguments, param_values, result_values):
  255. all_type_vars = {}
  256. for pv in param_values:
  257. if pv.array_type == 'list':
  258. type_var_dict = _infer_type_vars_for_callable(arguments, pv.py__iter__())
  259. all_type_vars.update(type_var_dict)
  260. return ValueSet.from_sets(
  261. v.define_generics(all_type_vars)
  262. if isinstance(v, (DefineGenericBaseClass, TypeVar))
  263. else ValueSet({v})
  264. for v in result_values
  265. ).execute_annotation()
  266. def _infer_type_vars_for_callable(arguments, lazy_params):
  267. """
  268. Infers type vars for the Calllable class:
  269. def x() -> Callable[[Callable[..., _T]], _T]: ...
  270. """
  271. annotation_variable_results = {}
  272. for (_, lazy_value), lazy_callable_param in zip(arguments.unpack(), lazy_params):
  273. callable_param_values = lazy_callable_param.infer()
  274. # Infer unknown type var
  275. actual_value_set = lazy_value.infer()
  276. merge_type_var_dicts(
  277. annotation_variable_results,
  278. callable_param_values.infer_type_vars(actual_value_set),
  279. )
  280. return annotation_variable_results
  281. def merge_type_var_dicts(base_dict, new_dict):
  282. for type_var_name, values in new_dict.items():
  283. if values:
  284. try:
  285. base_dict[type_var_name] |= values
  286. except KeyError:
  287. base_dict[type_var_name] = values
  288. def merge_pairwise_generics(annotation_value, annotated_argument_class):
  289. """
  290. Match up the generic parameters from the given argument class to the
  291. target annotation.
  292. This walks the generic parameters immediately within the annotation and
  293. argument's type, in order to determine the concrete values of the
  294. annotation's parameters for the current case.
  295. For example, given the following code:
  296. def values(mapping: Mapping[K, V]) -> List[V]: ...
  297. for val in values({1: 'a'}):
  298. val
  299. Then this function should be given representations of `Mapping[K, V]`
  300. and `Mapping[int, str]`, so that it can determine that `K` is `int and
  301. `V` is `str`.
  302. Note that it is responsibility of the caller to traverse the MRO of the
  303. argument type as needed in order to find the type matching the
  304. annotation (in this case finding `Mapping[int, str]` as a parent of
  305. `Dict[int, str]`).
  306. Parameters
  307. ----------
  308. `annotation_value`: represents the annotation to infer the concrete
  309. parameter types of.
  310. `annotated_argument_class`: represents the annotated class of the
  311. argument being passed to the object annotated by `annotation_value`.
  312. """
  313. type_var_dict = {}
  314. if not isinstance(annotated_argument_class, DefineGenericBaseClass):
  315. return type_var_dict
  316. annotation_generics = annotation_value.get_generics()
  317. actual_generics = annotated_argument_class.get_generics()
  318. for annotation_generics_set, actual_generic_set in zip(annotation_generics, actual_generics):
  319. merge_type_var_dicts(
  320. type_var_dict,
  321. annotation_generics_set.infer_type_vars(actual_generic_set.execute_annotation()),
  322. )
  323. return type_var_dict
  324. def find_type_from_comment_hint_for(context, node, name):
  325. return _find_type_from_comment_hint(context, node, node.children[1], name)
  326. def find_type_from_comment_hint_with(context, node, name):
  327. if len(node.children) > 4:
  328. # In case there are multiple with_items, we do not want a type hint for
  329. # now.
  330. return []
  331. assert len(node.children[1].children) == 3, \
  332. "Can only be here when children[1] is 'foo() as f'"
  333. varlist = node.children[1].children[2]
  334. return _find_type_from_comment_hint(context, node, varlist, name)
  335. def find_type_from_comment_hint_assign(context, node, name):
  336. return _find_type_from_comment_hint(context, node, node.children[0], name)
  337. def _find_type_from_comment_hint(context, node, varlist, name):
  338. index = None
  339. if varlist.type in ("testlist_star_expr", "exprlist", "testlist"):
  340. # something like "a, b = 1, 2"
  341. index = 0
  342. for child in varlist.children:
  343. if child == name:
  344. break
  345. if child.type == "operator":
  346. continue
  347. index += 1
  348. else:
  349. return []
  350. comment = parser_utils.get_following_comment_same_line(node)
  351. if comment is None:
  352. return []
  353. match = re.match(r"^#\s*type:\s*([^#]*)", comment)
  354. if match is None:
  355. return []
  356. return _infer_annotation_string(
  357. context, match.group(1).strip(), index
  358. ).execute_annotation()
  359. def find_unknown_type_vars(context, node):
  360. def check_node(node):
  361. if node.type in ('atom_expr', 'power'):
  362. trailer = node.children[-1]
  363. if trailer.type == 'trailer' and trailer.children[0] == '[':
  364. for subscript_node in _unpack_subscriptlist(trailer.children[1]):
  365. check_node(subscript_node)
  366. else:
  367. found[:] = _filter_type_vars(context.infer_node(node), found)
  368. found = [] # We're not using a set, because the order matters.
  369. check_node(node)
  370. return found
  371. def _filter_type_vars(value_set, found=()):
  372. new_found = list(found)
  373. for type_var in value_set:
  374. if isinstance(type_var, TypeVar) and type_var not in found:
  375. new_found.append(type_var)
  376. return new_found
  377. def _unpack_subscriptlist(subscriptlist):
  378. if subscriptlist.type == 'subscriptlist':
  379. for subscript in subscriptlist.children[::2]:
  380. if subscript.type != 'subscript':
  381. yield subscript
  382. else:
  383. if subscriptlist.type != 'subscript':
  384. yield subscriptlist