r"""``Stash`` is a type-safe heterogeneous mutable mapping that allows keys and value types to be defined separately from where it (the ``Stash``) is created. Usually you will be given an object which has a ``Stash``, for example :class:`~pytest.Config` or a :class:`~_pytest.nodes.N
| 25 | |
| 26 | |
| 27 | class Stash: |
| 28 | r"""``Stash`` is a type-safe heterogeneous mutable mapping that |
| 29 | allows keys and value types to be defined separately from |
| 30 | where it (the ``Stash``) is created. |
| 31 | |
| 32 | Usually you will be given an object which has a ``Stash``, for example |
| 33 | :class:`~pytest.Config` or a :class:`~_pytest.nodes.Node`: |
| 34 | |
| 35 | .. code-block:: python |
| 36 | |
| 37 | stash: Stash = some_object.stash |
| 38 | |
| 39 | If a module or plugin wants to store data in this ``Stash``, it creates |
| 40 | :class:`StashKey`\s for its keys (at the module level): |
| 41 | |
| 42 | .. code-block:: python |
| 43 | |
| 44 | # At the top-level of the module |
| 45 | some_str_key = StashKey[str]() |
| 46 | some_bool_key = StashKey[bool]() |
| 47 | |
| 48 | To store information: |
| 49 | |
| 50 | .. code-block:: python |
| 51 | |
| 52 | # Value type must match the key. |
| 53 | stash[some_str_key] = "value" |
| 54 | stash[some_bool_key] = True |
| 55 | |
| 56 | To retrieve the information: |
| 57 | |
| 58 | .. code-block:: python |
| 59 | |
| 60 | # The static type of some_str is str. |
| 61 | some_str = stash[some_str_key] |
| 62 | # The static type of some_bool is bool. |
| 63 | some_bool = stash[some_bool_key] |
| 64 | """ |
| 65 | |
| 66 | __slots__ = ("_storage",) |
| 67 | |
| 68 | def __init__(self) -> None: |
| 69 | self._storage: Dict[StashKey[Any], object] = {} |
| 70 | |
| 71 | def __setitem__(self, key: StashKey[T], value: T) -> None: |
| 72 | """Set a value for key.""" |
| 73 | self._storage[key] = value |
| 74 | |
| 75 | def __getitem__(self, key: StashKey[T]) -> T: |
| 76 | """Get the value for key. |
| 77 | |
| 78 | Raises ``KeyError`` if the key wasn't set before. |
| 79 | """ |
| 80 | return cast(T, self._storage[key]) |
| 81 | |
| 82 | def get(self, key: StashKey[T], default: D) -> Union[T, D]: |
| 83 | """Get the value for key, or return default if the key wasn't set |
| 84 | before.""" |
no outgoing calls