| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from typing import Any |
| 4 |
from typing import cast |
| 5 |
from typing import Generic |
| 6 |
from typing import TypeVar |
| 7 |
|
| 8 |
|
| 9 |
__all__ = ["Stash", "StashKey"] |
| 10 |
|
| 11 |
|
| 12 |
T = TypeVar("T") |
| 13 |
D = TypeVar("D") |
| 14 |
|
| 15 |
|
| 16 |
class StashKey(Generic[T]): |
| 17 |
"""``StashKey`` is an object used as a key to a :class:`Stash`. |
| 18 |
|
| 19 |
A ``StashKey`` is associated with the type ``T`` of the value of the key. |
| 20 |
|
| 21 |
A ``StashKey`` is unique and cannot conflict with another key. |
| 22 |
|
| 23 |
.. versionadded:: 7.0 |
| 24 |
""" |
| 25 |
|
| 26 |
__slots__ = () |
| 27 |
|
| 28 |
|
| 29 |
class Stash: |
| 30 |
r"""``Stash`` is a type-safe heterogeneous mutable mapping that |
| 31 |
allows keys and value types to be defined separately from |
| 32 |
where it (the ``Stash``) is created. |
| 33 |
|
| 34 |
Usually you will be given an object which has a ``Stash``, for example |
| 35 |
:class:`~pytest.Config` or a :class:`~_pytest.nodes.Node`: |
| 36 |
|
| 37 |
.. code-block:: python |
| 38 |
|
| 39 |
stash: Stash = some_object.stash |
| 40 |
|
| 41 |
If a module or plugin wants to store data in this ``Stash``, it creates |
| 42 |
:class:`StashKey`\s for its keys (at the module level): |
| 43 |
|
| 44 |
.. code-block:: python |
| 45 |
|
| 46 |
# At the top-level of the module |
| 47 |
some_str_key = StashKey[str]() |
| 48 |
some_bool_key = StashKey[bool]() |
| 49 |
|
| 50 |
To store information: |
| 51 |
|
| 52 |
.. code-block:: python |
| 53 |
|
| 54 |
# Value type must match the key. |
| 55 |
stash[some_str_key] = "value" |
| 56 |
stash[some_bool_key] = True |
| 57 |
|
| 58 |
To retrieve the information: |
| 59 |
|
| 60 |
.. code-block:: python |
| 61 |
|
| 62 |
# The static type of some_str is str. |
| 63 |
some_str = stash[some_str_key] |
| 64 |
# The static type of some_bool is bool. |
| 65 |
some_bool = stash[some_bool_key] |
| 66 |
|
| 67 |
.. versionadded:: 7.0 |
| 68 |
""" |
| 69 |
|
| 70 |
__slots__ = ("_storage",) |
| 71 |
|
| 72 |
def __init__(self) -> None: |
| 73 |
self._storage: dict[StashKey[Any], object] = {} |
| 74 |
|
| 75 |
def __setitem__(self, key: StashKey[T], value: T) -> None: |
| 76 |
"""Set a value for key.""" |
| 77 |
self._storage[key] = value |
| 78 |
|
| 79 |
def __getitem__(self, key: StashKey[T]) -> T: |
| 80 |
"""Get the value for key. |
| 81 |
|
| 82 |
Raises ``KeyError`` if the key wasn't set before. |
| 83 |
""" |
| 84 |
return cast(T, self._storage[key]) |
| 85 |
|
| 86 |
def get(self, key: StashKey[T], default: D) -> T | D: |
| 87 |
"""Get the value for key, or return default if the key wasn't set |
| 88 |
before.""" |
| 89 |
try: |
| 90 |
return self[key] |
| 91 |
except KeyError: |
| 92 |
return default |
| 93 |
|
| 94 |
def setdefault(self, key: StashKey[T], default: T) -> T: |
| 95 |
"""Return the value of key if already set, otherwise set the value |
| 96 |
of key to default and return default.""" |
| 97 |
try: |
| 98 |
return self[key] |
| 99 |
except KeyError: |
| 100 |
self[key] = default |
| 101 |
return default |
| 102 |
|
| 103 |
def __delitem__(self, key: StashKey[T]) -> None: |
| 104 |
"""Delete the value for key. |
| 105 |
|
| 106 |
Raises ``KeyError`` if the key wasn't set before. |
| 107 |
""" |
| 108 |
del self._storage[key] |
| 109 |
|
| 110 |
def __contains__(self, key: StashKey[T]) -> bool: |
| 111 |
"""Return whether key was set.""" |
| 112 |
return key in self._storage |
| 113 |
|
| 114 |
def __len__(self) -> int: |
| 115 |
"""Return how many items exist in the stash.""" |
| 116 |
return len(self._storage) |