stash.py

Overview

The [stash.py](/projects/286/67434) file implements a type-safe, heterogeneous mutable mapping system called `Stash` and a key representation class `StashKey` used to index values within the `Stash`. The main purpose of this file is to provide a utility that allows storing and retrieving values of different types in a single shared container without compromising type safety.

This is particularly useful in modular or plugin-based systems where different modules may want to store data in a common object without risking key collisions or type mismatches. Each key (`StashKey`) is unique and associated with a specific value type, ensuring that values retrieved from the `Stash` match the expected type.

The file is designed with static type checking in mind, leveraging Python generics and typing hints to enforce and document the expected types of keys and values.


Classes

StashKey[T]

A generic key type used to index values of type `T` in a `Stash`.


Stash

A type-safe, heterogeneous mutable mapping that allows storing and retrieving values of arbitrary types keyed by `StashKey`s.


Methods

__init__(self) -> None

__setitem__(self, key: StashKey[T], value: T) -> None

__getitem__(self, key: StashKey[T]) -> T

get(self, key: StashKey[T], default: D) -> T | D

setdefault(self, key: StashKey[T], default: T) -> T

__delitem__(self, key: StashKey[T]) -> None

__contains__(self, key: StashKey[T]) -> bool

__len__(self) -> int


Implementation Details


Interaction with Other System Components


Usage Example

from stash import Stash, StashKey

# Module-level keys
user_id_key = StashKey[int]()
logged_in_key = StashKey[bool]()

# Creating a stash instance (usually provided by some system component)
stash = Stash()

# Storing values
stash[user_id_key] = 1234
stash[logged_in_key] = True

# Retrieving values
user_id: int = stash[user_id_key]
is_logged_in: bool = stash.get(logged_in_key, False)

# Checking existence
if user_id_key in stash:
    print(f"User ID is {stash[user_id_key]}")

# Using setdefault
default_user = stash.setdefault(user_id_key, 0)

Mermaid Class Diagram

classDiagram
    class StashKey~T~ {
        <<generic>>
    }

    class Stash {
        -_storage: dict[StashKey[Any], object]
        +__init__()
        +__setitem__(key: StashKey[T], value: T)
        +__getitem__(key: StashKey[T]) T
        +get(key: StashKey[T], default: D) T|D
        +setdefault(key: StashKey[T], default: T) T
        +__delitem__(key: StashKey[T])
        +__contains__(key: StashKey[T]) bool
        +__len__() int
    }

Summary

The [stash.py](/projects/286/67434) file offers a simple yet powerful mechanism to store and retrieve heterogeneously typed values with strong type safety guarantees, facilitating modular, decoupled data sharing inside larger systems or frameworks. Its design ensures that keys are unique and strongly typed, preventing runtime errors and simplifying static analysis. This makes it ideal for plugin architectures and complex applications needing a flexible shared state container.