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

Class _Stringifier

Lib/annotationlib.py:330–584  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

328
329
330class _Stringifier:
331 # Must match the slots on ForwardRef, so we can turn an instance of one into an
332 # instance of the other in place.
333 __slots__ = _SLOTS
334
335 def __init__(
336 self,
337 node,
338 globals=None,
339 owner=None,
340 is_class=False,
341 cell=None,
342 *,
343 stringifier_dict,
344 extra_names=None,
345 ):
346 # Either an AST node or a simple str (for the common case where a ForwardRef
347 # represent a single name).
348 assert isinstance(node, (ast.AST, str))
349 self.__arg__ = None
350 self.__forward_is_argument__ = False
351 self.__forward_is_class__ = is_class
352 self.__forward_module__ = None
353 self.__code__ = None
354 self.__ast_node__ = node
355 self.__globals__ = globals
356 self.__extra_names__ = extra_names
357 self.__cell__ = cell
358 self.__owner__ = owner
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)

Callers 3

__make_newMethod · 0.85
__missing__Method · 0.85
_build_closureFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…