@@ -39,6 +39,8 @@ class Credentials(NamedTuple):
39
39
class KeyRingBaseProvider (ABC ):
40
40
"""Keyring base provider interface"""
41
41
42
+ has_keyring : bool
43
+
42
44
@abstractmethod
43
45
def get_auth_info (self , url : str , username : Optional [str ]) -> Optional [AuthInfo ]:
44
46
...
@@ -51,6 +53,8 @@ def save_auth_info(self, url: str, username: str, password: str) -> None:
51
53
class KeyRingNullProvider (KeyRingBaseProvider ):
52
54
"""Keyring null provider"""
53
55
56
+ has_keyring = False
57
+
54
58
def get_auth_info (self , url : str , username : Optional [str ]) -> Optional [AuthInfo ]:
55
59
return None
56
60
@@ -61,6 +65,8 @@ def save_auth_info(self, url: str, username: str, password: str) -> None:
61
65
class KeyRingPythonProvider (KeyRingBaseProvider ):
62
66
"""Keyring interface which uses locally imported `keyring`"""
63
67
68
+ has_keyring = True
69
+
64
70
def __init__ (self ) -> None :
65
71
import keyring
66
72
@@ -97,6 +103,8 @@ class KeyRingCliProvider(KeyRingBaseProvider):
97
103
PATH.
98
104
"""
99
105
106
+ has_keyring = True
107
+
100
108
def __init__ (self , cmd : str ) -> None :
101
109
self .keyring = cmd
102
110
@@ -359,7 +367,7 @@ def _prompt_for_password(
359
367
360
368
# Factored out to allow for easy patching in tests
361
369
def _should_save_password_to_keyring (self ) -> bool :
362
- if isinstance ( get_keyring_provider (), KeyRingNullProvider ) :
370
+ if not get_keyring_provider (). has_keyring :
363
371
return False
364
372
return ask ("Save credentials to keyring [y/N]: " , ["y" , "n" ]) == "y"
365
373
@@ -432,9 +440,7 @@ def warn_on_401(self, resp: Response, **kwargs: Any) -> None:
432
440
def save_credentials (self , resp : Response , ** kwargs : Any ) -> None :
433
441
"""Response callback to save credentials on success."""
434
442
keyring = get_keyring_provider ()
435
- assert not isinstance (
436
- keyring , KeyRingNullProvider
437
- ), "should never reach here without keyring"
443
+ assert keyring .has_keyring , "should never reach here without keyring"
438
444
439
445
creds = self ._credentials_to_save
440
446
self ._credentials_to_save = None
0 commit comments