From 31dc8fab04687d9a89f4aef1806990ff78dc4c63 Mon Sep 17 00:00:00 2001 From: imacat Date: Tue, 7 Feb 2023 14:12:22 +0800 Subject: [PATCH] Changed the type hint of the "current_user" pseudo property of the AbstractUserUtils class to return None when the user has not logged in. --- src/accounting/utils/user.py | 9 +++++---- tests/test_site/__init__.py | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/accounting/utils/user.py b/src/accounting/utils/user.py index f7c20ad..c50d6f6 100644 --- a/src/accounting/utils/user.py +++ b/src/accounting/utils/user.py @@ -50,10 +50,11 @@ class AbstractUserUtils(t.Generic[T], ABC): @property @abstractmethod - def current_user(self) -> T: - """Returns the current user. + def current_user(self) -> T | None: + """Returns the currently logged-in user. - :return: The current user. + :return: The currently logged-in user, or None if the user has not + logged in """ @abstractmethod @@ -117,7 +118,7 @@ def get_user_pk(username: str) -> int: return __user_utils.get_pk(__user_utils.get_by_username(username)) -def get_current_user() -> user_cls: +def get_current_user() -> user_cls | None: """Returns the currently logged-in user. The result is cached in the current request. diff --git a/tests/test_site/__init__.py b/tests/test_site/__init__.py index 941af29..7455172 100644 --- a/tests/test_site/__init__.py +++ b/tests/test_site/__init__.py @@ -80,7 +80,7 @@ def create_app(is_testing: bool = False) -> Flask: return auth.User.id @property - def current_user(self) -> auth.User: + def current_user(self) -> auth.User | None: return auth.current_user() def get_by_username(self, username: str) -> auth.User | None: