Wrapper that holds a forward reference. Constructor arguments: * arg: a string representing the code to be evaluated. * module: the module where the forward reference was created. Must be a string, not a module object. * owner: The owning object (module, class, or function).
| 51 | |
| 52 | |
| 53 | class ForwardRef: |
| 54 | """Wrapper that holds a forward reference. |
| 55 | |
| 56 | Constructor arguments: |
| 57 | * arg: a string representing the code to be evaluated. |
| 58 | * module: the module where the forward reference was created. |
| 59 | Must be a string, not a module object. |
| 60 | * owner: The owning object (module, class, or function). |
| 61 | * is_argument: Does nothing, retained for compatibility. |
| 62 | * is_class: True if the forward reference was created in class scope. |
| 63 | |
| 64 | """ |
| 65 | |
| 66 | __slots__ = _SLOTS |
| 67 | |
| 68 | def __init__( |
| 69 | self, |
| 70 | arg, |
| 71 | *, |
| 72 | module=None, |
| 73 | owner=None, |
| 74 | is_argument=True, |
| 75 | is_class=False, |
| 76 | ): |
| 77 | if not isinstance(arg, str): |
| 78 | raise TypeError(f"Forward reference must be a string -- got {arg!r}") |
| 79 | |
| 80 | self.__arg__ = arg |
| 81 | self.__forward_is_argument__ = is_argument |
| 82 | self.__forward_is_class__ = is_class |
| 83 | self.__forward_module__ = module |
| 84 | self.__owner__ = owner |
| 85 | # These are always set to None here but may be non-None if a ForwardRef |
| 86 | # is created through __class__ assignment on a _Stringifier object. |
| 87 | self.__globals__ = None |
| 88 | # This may be either a cell object (for a ForwardRef referring to a single name) |
| 89 | # or a dict mapping cell names to cell objects (for a ForwardRef containing references |
| 90 | # to multiple names). |
| 91 | self.__cell__ = None |
| 92 | self.__extra_names__ = None |
| 93 | # These are initially None but serve as a cache and may be set to a non-None |
| 94 | # value later. |
| 95 | self.__code__ = None |
| 96 | self.__ast_node__ = None |
| 97 | |
| 98 | def __init_subclass__(cls, /, *args, **kwds): |
| 99 | raise TypeError("Cannot subclass ForwardRef") |
| 100 | |
| 101 | def evaluate( |
| 102 | self, |
| 103 | *, |
| 104 | globals=None, |
| 105 | locals=None, |
| 106 | type_params=None, |
| 107 | owner=None, |
| 108 | format=Format.VALUE, |
| 109 | ): |
| 110 | """Evaluate the forward reference and return the value. |
no outgoing calls
searching dependent graphs…