
Aug. 18, 2023
8:37 p.m.
Why not something like: from typing import Any, Generic, Mapping, TypeVar, TypedDict, cast T = TypeVar("T", bound=Mapping[str, Any]) class Item(Generic[T]): def __init__(self) -> None: self.stash: T = cast(T, {}) class MyStash(TypedDict): my_plugin_attr: int def pytest_runtest_setup(item: Item[MyStash]) -> None: item.stash["my_plugin_attr"] = 8 def pytest_runtest_teardown(item: Item[MyStash]) -> None: print(item.stash["my_plugin_attr"]) # Maybe del item.stash["my_plugin_attr"] # type error here because the key is required This works in both pyright and mypy.