Added typing fof KeyringAttributes

This should hopefully shut mypy up.

Contributes to CURA-7180
This commit is contained in:
jelle Spijker 2021-04-07 11:06:06 +02:00 committed by Jelle Spijker
parent 1e5d7623cb
commit 33a812d696
No known key found for this signature in database
GPG key ID: 6662DC033BE6B99A

View file

@ -1,6 +1,6 @@
# Copyright (c) 2021 Ultimaker B.V. # Copyright (c) 2021 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher. # Cura is released under the terms of the LGPLv3 or higher.
from typing import Type, TYPE_CHECKING from typing import Type, TYPE_CHECKING, Optional, List
import keyring import keyring
from keyring.backend import KeyringBackend from keyring.backend import KeyringBackend
@ -20,14 +20,15 @@ if Platform.isWindows() and hasattr(sys, "frozen"):
keyring.set_keyring(WinVaultKeyring()) keyring.set_keyring(WinVaultKeyring())
# Even if errors happen, we don't want this stored locally: # Even if errors happen, we don't want this stored locally:
DONT_EVER_STORE_LOCALLY = ["refresh_token"] DONT_EVER_STORE_LOCALLY: List[str] = ["refresh_token"]
class KeyringAttribute: class KeyringAttribute:
""" """
Descriptor for attributes that need to be stored in the keyring. With Fallback behaviour to the preference cfg file Descriptor for attributes that need to be stored in the keyring. With Fallback behaviour to the preference cfg file
""" """
def __get__(self, instance: Type["BaseModel"], owner: type) -> str: def __get__(self, instance: BaseModel, owner: type) -> Optional[str]:
if self._store_secure: if self._store_secure: # type: ignore
try: try:
value = keyring.get_password("cura", self._keyring_name) value = keyring.get_password("cura", self._keyring_name)
return value if value != "" else None return value if value != "" else None
@ -38,7 +39,7 @@ class KeyringAttribute:
else: else:
return getattr(instance, self._name) return getattr(instance, self._name)
def __set__(self, instance: Type["BaseModel"], value: str): def __set__(self, instance: BaseModel, value: Optional[str]):
if self._store_secure: if self._store_secure:
setattr(instance, self._name, None) setattr(instance, self._name, None)
try: try: