Skip to content
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

Improve performance of State #2463

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 9 additions & 8 deletions starlette/datastructures.py
Expand Up @@ -692,14 +692,15 @@ def __init__(self, state: dict[str, typing.Any] | None = None):
super().__setattr__("_state", state)

def __setattr__(self, key: typing.Any, value: typing.Any) -> None:
self._state[key] = value

def __getattr__(self, key: typing.Any) -> typing.Any:
try:
return self._state[key]
except KeyError:
message = "'{}' object has no attribute '{}'"
raise AttributeError(message.format(self.__class__.__name__, key))
object.__getattribute__(self, "_state")[key] = value

def __getattribute__(self, key: typing.Any) -> typing.Any:
state = object.__getattribute__(self, "_state")

if key in state:
return state[key]

return object.__getattribute__(self, key)

def __delattr__(self, key: typing.Any) -> None:
del self._state[key]