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

chore(typing): add more descriptive typing to Context #2226

Merged
merged 2 commits into from
Apr 5, 2024
Merged
Changes from 1 commit
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
20 changes: 20 additions & 0 deletions falcon/util/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from typing import KeysView
from typing import Optional
from typing import Tuple
from typing import TYPE_CHECKING
from typing import ValuesView


Expand Down Expand Up @@ -141,6 +142,25 @@ class Context:
True
"""

# NOTE(vytas): Define synthetic attr access methods (under TYPE_CHECKING)
# merely to let mypy know this is a namespace object.
if TYPE_CHECKING:

def __getattr__(self, name: str) -> Any:
vytas7 marked this conversation as resolved.
Show resolved Hide resolved
try:
return self.__dict__[name]
except KeyError:
raise AttributeError(name) from None

def __setattr__(self, name: str, value: Any) -> None:
self.__dict__[name] = value

def __delattr__(self, name: str) -> None:
try:
del self.__dict__[name]
except KeyError:
raise AttributeError(name) from None

def __contains__(self, key: str) -> bool:
return self.__dict__.__contains__(key)

Expand Down