Replies: 3 comments 6 replies
-
You can make Here's how _ScopeStateT = TypeVar('_ScopeStateT')
class Scope(TypedDict, Generic[_ScopeStateT]):
state: _ScopeStateT
class Request(Generic[_ScopeStateT]):
scope: Scope[_ScopeStateT] And then: async def homepage(request: Request[LifespanState]) -> Response:
reveal_type(request.scope["state"]["potato"]) # N: Revealed type is "builtins.str" |
Beta Was this translation helpful? Give feedback.
5 replies
-
I really want to improve the experience of FastAPI/Starlette users, and having something like this would be great. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi there 👋
I'm one of the maintainers of Starlette, and I need help.
In Starlette (ASGI), we have Lifespan events (startup and shutdown). There, you can store data that will be used on your endpoints (see line
yield {"potato": "potato"}
below for how to store).And... There are two ways to retrieve this
potato
in the endpoint:request.scope["state"]["potato"]
, sincestate
is a dictionary.request.state.potato
, sinceRequest.state
actually wraps the above dictionary, see Request/State for more details.The first is an ASGI concept, and the second is specific to Starlette. Deprecating the second is not an option, but we can tweak the implementation as much as wanted if it doesn't break our users' applications.
Anyway... Is there a way to type hint the Request/State in a way the following
reveal_type
s shows the type ofpotato
?Note
Title may be misleading - change it if you want. 👀
Beta Was this translation helpful? Give feedback.
All reactions