From 3dd88387ad7c8470c93480aed0e55b26351b6508 Mon Sep 17 00:00:00 2001 From: Zoheb Shaikh Date: Thu, 17 Oct 2024 14:15:41 +0100 Subject: [PATCH] type checking fixes --- src/blueapi/service/authentication.py | 13 ++++++++----- tests/unit_tests/service/test_authentication.py | 7 ++++--- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/blueapi/service/authentication.py b/src/blueapi/service/authentication.py index abc83caae..53931ae71 100644 --- a/src/blueapi/service/authentication.py +++ b/src/blueapi/service/authentication.py @@ -61,7 +61,7 @@ def print_user_info(self, token: str) -> None: class TokenManager(ABC): @abstractmethod - def save_token(self, token: dict[str, Any]): ... + def save_token(self, token: dict[str, Any]) -> None: ... @abstractmethod def load_token(token) -> dict[str, Any] | None: ... @abstractmethod @@ -119,6 +119,7 @@ def from_config( client_config, CLITokenManager(Path(client_config.token_file_path)), ) + return None def get_token(self) -> dict[str, Any] | None: return self._token_manager.load_token() @@ -139,11 +140,13 @@ def refresh_auth_token(self) -> dict[str, Any] | None: ) if response.status_code == HTTPStatus.OK: token = response.json() - self._token_manager.save_token(token) - return token + if token: + self._token_manager.save_token(token) + return token + return None def get_device_code(self): - response: requests.Response = requests.post( + response = requests.post( self._server_config.token_url, data={ "client_id": self._client_config.client_id, @@ -151,7 +154,7 @@ def get_device_code(self): "audience": self._client_config.client_audience, }, ) - response_data: dict[str, str] = response.json() + response_data = response.json() if response.status_code == 200: return response_data["device_code"] raise Exception("Failed to get device code.") diff --git a/tests/unit_tests/service/test_authentication.py b/tests/unit_tests/service/test_authentication.py index cd2a6f3fa..02c8973a0 100644 --- a/tests/unit_tests/service/test_authentication.py +++ b/tests/unit_tests/service/test_authentication.py @@ -39,9 +39,10 @@ def mock_server_config(mock_requests_get) -> OAuthServerConfig: @pytest.fixture -def mock_session_manager(mock_client_config, mock_server_config) -> SessionManager: - session_manager = SessionManager.from_config(mock_server_config, mock_client_config) - return session_manager +def mock_session_manager( + mock_client_config: OAuthClientConfig, mock_server_config: OAuthServerConfig +) -> SessionManager | None: + return SessionManager.from_config(mock_server_config, mock_client_config) @pytest.fixture