Gets the value of this :class:`RunVar` for the current run call.
(self, default: T | type[_NoValue] = _NoValue)
| 42 | _default: T | type[_NoValue] = attrs.field(default=_NoValue, alias="default") |
| 43 | |
| 44 | def get(self, default: T | type[_NoValue] = _NoValue) -> T: |
| 45 | """Gets the value of this :class:`RunVar` for the current run call.""" |
| 46 | try: |
| 47 | return cast("T", _run.GLOBAL_RUN_CONTEXT.runner._locals[self]) |
| 48 | except AttributeError: |
| 49 | raise RuntimeError("Cannot be used outside of a run context") from None |
| 50 | except KeyError: |
| 51 | # contextvars consistency |
| 52 | # `type: ignore` awaiting https://github.com/python/mypy/issues/15553 to be fixed & released |
| 53 | if default is not _NoValue: |
| 54 | return default # type: ignore[return-value] |
| 55 | |
| 56 | if self._default is not _NoValue: |
| 57 | return self._default # type: ignore[return-value] |
| 58 | |
| 59 | raise LookupError(self) from None |
| 60 | |
| 61 | def set(self, value: T) -> RunVarToken[T]: |
| 62 | """Sets the value of this :class:`RunVar` for this current run |
no outgoing calls