MCPcopy Index your code
hub / github.com/python/cpython / __convert_to_ast

Method __convert_to_ast

Lib/annotationlib.py:361–402  ·  view source on GitHub ↗
(self, other)

Source from the content-addressed store, hash-verified

359 self.__stringifier_dict__ = stringifier_dict
360
361 def __convert_to_ast(self, other):
362 if isinstance(other, _Stringifier):
363 if isinstance(other.__ast_node__, str):
364 return ast.Name(id=other.__ast_node__), other.__extra_names__
365 return other.__ast_node__, other.__extra_names__
366 elif type(other) is _Template:
367 return _template_to_ast(other), None
368 elif (
369 # In STRING format we don't bother with the create_unique_name() dance;
370 # it's better to emit the repr() of the object instead of an opaque name.
371 self.__stringifier_dict__.format == Format.STRING
372 or other is None
373 or type(other) in (str, int, float, bool, complex)
374 ):
375 return ast.Constant(value=other), None
376 elif type(other) is dict:
377 extra_names = {}
378 keys = []
379 values = []
380 for key, value in other.items():
381 new_key, new_extra_names = self.__convert_to_ast(key)
382 if new_extra_names is not None:
383 extra_names.update(new_extra_names)
384 keys.append(new_key)
385 new_value, new_extra_names = self.__convert_to_ast(value)
386 if new_extra_names is not None:
387 extra_names.update(new_extra_names)
388 values.append(new_value)
389 return ast.Dict(keys, values), extra_names
390 elif type(other) in (list, tuple, set):
391 extra_names = {}
392 elts = []
393 for elt in other:
394 new_elt, new_extra_names = self.__convert_to_ast(elt)
395 if new_extra_names is not None:
396 extra_names.update(new_extra_names)
397 elts.append(new_elt)
398 ast_class = {list: ast.List, tuple: ast.Tuple, set: ast.Set}[type(other)]
399 return ast_class(elts), extra_names
400 else:
401 name = self.__stringifier_dict__.create_unique_name()
402 return ast.Name(id=name), {name: other}
403
404 def __convert_to_ast_getitem(self, other):
405 if isinstance(other, slice):

Callers 6

convMethod · 0.95
__call__Method · 0.95
binopMethod · 0.95
rbinopMethod · 0.95
compareMethod · 0.95

Calls 5

_template_to_astFunction · 0.85
create_unique_nameMethod · 0.80
itemsMethod · 0.45
updateMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected