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.
_______________________________________________
Typing-sig mailing list -- typing-sig@python.org
To unsubscribe send an email to typing-sig-leave@python.org
https://urldefense.proofpoint.com/v2/url?u=https-3A__mail.python.org_mailman3_lists_typing-2Dsig.python.org_&d=DwICAg&c=slrrB7dE8n7gBJbeO0g-IQ&r=b_yQYQzY71v5dLCdkk1wmw&m=5ntSUirKeWQyuYLfBTAig-TC6heCSuTs9cY17oBAP850OY4BFYPX2TQO_2CM4aJ2&s=5RE34exckx5IBK_jehZ_M7l2-vvZPxOe86F4LzVbwa8&e=
Member address: zl3464@nyu.edu