Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correct the way to decide if keyring is available #11774

Merged
merged 3 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/11774.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Correct the way to decide if keyring is available.
14 changes: 10 additions & 4 deletions src/pip/_internal/network/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class Credentials(NamedTuple):
class KeyRingBaseProvider(ABC):
"""Keyring base provider interface"""

has_keyring: bool

@abstractmethod
def get_auth_info(self, url: str, username: Optional[str]) -> Optional[AuthInfo]:
...
Expand All @@ -51,6 +53,8 @@ def save_auth_info(self, url: str, username: str, password: str) -> None:
class KeyRingNullProvider(KeyRingBaseProvider):
"""Keyring null provider"""

has_keyring = False

def get_auth_info(self, url: str, username: Optional[str]) -> Optional[AuthInfo]:
return None

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

has_keyring = True

def __init__(self) -> None:
import keyring

Expand Down Expand Up @@ -97,6 +103,8 @@ class KeyRingCliProvider(KeyRingBaseProvider):
PATH.
"""

has_keyring = True

def __init__(self, cmd: str) -> None:
self.keyring = cmd

Expand Down Expand Up @@ -359,7 +367,7 @@ def _prompt_for_password(

# Factored out to allow for easy patching in tests
def _should_save_password_to_keyring(self) -> bool:
if get_keyring_provider() is None:
if not get_keyring_provider().has_keyring:
return False
return ask("Save credentials to keyring [y/N]: ", ["y", "n"]) == "y"

Expand Down Expand Up @@ -432,9 +440,7 @@ def warn_on_401(self, resp: Response, **kwargs: Any) -> None:
def save_credentials(self, resp: Response, **kwargs: Any) -> None:
"""Response callback to save credentials on success."""
keyring = get_keyring_provider()
assert not isinstance(
keyring, KeyRingNullProvider
), "should never reach here without keyring"
assert keyring.has_keyring, "should never reach here without keyring"

creds = self._credentials_to_save
self._credentials_to_save = None
Expand Down