-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Turn State into a Mapping
#3036
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Kludex
wants to merge
3
commits into
main
Choose a base branch
from
state-mapping
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+24
−5
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,9 +3,10 @@ | |
| import json | ||
| from collections.abc import AsyncGenerator, Iterator, Mapping | ||
| from http import cookies as http_cookies | ||
| from typing import TYPE_CHECKING, Any, NoReturn, cast | ||
| from typing import TYPE_CHECKING, Any, Generic, NoReturn, cast | ||
|
|
||
| import anyio | ||
| from typing_extensions import TypeVar | ||
|
|
||
| from starlette._utils import AwaitableOrContextManager, AwaitableOrContextManagerWrapper | ||
| from starlette.datastructures import URL, Address, FormData, Headers, QueryParams, State | ||
|
|
@@ -68,7 +69,10 @@ class ClientDisconnect(Exception): | |
| pass | ||
|
|
||
|
|
||
| class HTTPConnection(Mapping[str, Any]): | ||
| StateT = TypeVar("StateT", bound=Mapping[str, Any] | State, default=State) | ||
|
|
||
|
|
||
| class HTTPConnection(Mapping[str, Any], Generic[StateT]): | ||
| """ | ||
| A base class for incoming HTTP connections, that is used to provide | ||
| any functionality that is common to both `Request` and `WebSocket`. | ||
|
|
@@ -172,14 +176,14 @@ def user(self) -> Any: | |
| return self.scope["user"] | ||
|
|
||
| @property | ||
| def state(self) -> State: | ||
| def state(self) -> StateT: | ||
| if not hasattr(self, "_state"): | ||
| # Ensure 'state' has an empty dict if it's not already populated. | ||
| self.scope.setdefault("state", {}) | ||
| # Create a state instance with a reference to the dict in which it should | ||
| # store info | ||
| self._state = State(self.scope["state"]) | ||
| return self._state | ||
| return cast(StateT, self._state) | ||
|
|
||
| def url_for(self, name: str, /, **path_params: Any) -> URL: | ||
| url_path_provider: Router | Starlette | None = self.scope.get("router") or self.scope.get("app") | ||
|
|
@@ -197,7 +201,7 @@ async def empty_send(message: Message) -> NoReturn: | |
| raise RuntimeError("Send channel has not been made available") | ||
|
|
||
|
|
||
| class Request(HTTPConnection): | ||
| class Request(HTTPConnection[StateT]): | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are missing the same in |
||
| _form: FormData | None | ||
|
|
||
| def __init__(self, scope: Scope, receive: Receive = empty_receive, send: Send = empty_send): | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if this will pass the TypedDict check in some static type checkers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which one you don't think it will not? We test with mypy here, but pyright doesn't seem to be failing (I use it on my environment).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's great to be able to pass the inspection.