Skip to content

Commit 2d0a5c9

Browse files
committed
use a attribute to tell if the provider is null
1 parent 706456c commit 2d0a5c9

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/pip/_internal/network/auth.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class Credentials(NamedTuple):
3939
class KeyRingBaseProvider(ABC):
4040
"""Keyring base provider interface"""
4141

42+
has_keyring: bool
43+
4244
@abstractmethod
4345
def get_auth_info(self, url: str, username: Optional[str]) -> Optional[AuthInfo]:
4446
...
@@ -51,6 +53,8 @@ def save_auth_info(self, url: str, username: str, password: str) -> None:
5153
class KeyRingNullProvider(KeyRingBaseProvider):
5254
"""Keyring null provider"""
5355

56+
has_keyring = False
57+
5458
def get_auth_info(self, url: str, username: Optional[str]) -> Optional[AuthInfo]:
5559
return None
5660

@@ -61,6 +65,8 @@ def save_auth_info(self, url: str, username: str, password: str) -> None:
6165
class KeyRingPythonProvider(KeyRingBaseProvider):
6266
"""Keyring interface which uses locally imported `keyring`"""
6367

68+
has_keyring = True
69+
6470
def __init__(self) -> None:
6571
import keyring
6672

@@ -97,6 +103,8 @@ class KeyRingCliProvider(KeyRingBaseProvider):
97103
PATH.
98104
"""
99105

106+
has_keyring = True
107+
100108
def __init__(self, cmd: str) -> None:
101109
self.keyring = cmd
102110

@@ -359,7 +367,7 @@ def _prompt_for_password(
359367

360368
# Factored out to allow for easy patching in tests
361369
def _should_save_password_to_keyring(self) -> bool:
362-
if isinstance(get_keyring_provider(), KeyRingNullProvider):
370+
if not get_keyring_provider().has_keyring:
363371
return False
364372
return ask("Save credentials to keyring [y/N]: ", ["y", "n"]) == "y"
365373

@@ -432,9 +440,7 @@ def warn_on_401(self, resp: Response, **kwargs: Any) -> None:
432440
def save_credentials(self, resp: Response, **kwargs: Any) -> None:
433441
"""Response callback to save credentials on success."""
434442
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"
438444

439445
creds = self._credentials_to_save
440446
self._credentials_to_save = None

0 commit comments

Comments
 (0)