From 64af3cfd0aa9c74c160560e8089a5a03699dfd58 Mon Sep 17 00:00:00 2001 From: Vytautas Liuolia Date: Fri, 5 Apr 2024 22:33:42 +0200 Subject: [PATCH] chore(typing): add more descriptive typing to `Context` (#2226) * chore(typing): add more descriptive typing to `Context` * chore(typing): define only stubs when under `TYPE_CHECKING` --- falcon/util/structures.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/falcon/util/structures.py b/falcon/util/structures.py index fc7ba2a88..5a9e51176 100644 --- a/falcon/util/structures.py +++ b/falcon/util/structures.py @@ -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 @@ -141,6 +142,19 @@ 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: + ... + + def __setattr__(self, name: str, value: Any) -> None: + ... + + def __delattr__(self, name: str) -> None: + ... + def __contains__(self, key: str) -> bool: return self.__dict__.__contains__(key)