Skip to content

Commit

Permalink
Extract method for 'is_pypi' to consolidate repeated behavior and doc…
Browse files Browse the repository at this point in the history
…umentation.
  • Loading branch information
jaraco committed Jun 26, 2024
1 parent 5bf3f38 commit 7386ee0
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions twine/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,8 @@ def choose(cls, interactive: bool) -> Type["Resolver"]:
@property
@functools.lru_cache()
def username(self) -> Optional[str]:
if cast(str, self.config["repository"]).startswith(
(utils.DEFAULT_REPOSITORY, utils.TEST_REPOSITORY)
):
# As of 2024-01-01, PyPI requires API tokens for uploads, meaning
# that the username is invariant.
if self.is_pypi():
# Username is invariant.
return "__token__"

return utils.get_userpass_value(
Expand Down Expand Up @@ -97,20 +94,23 @@ def password_from_keyring_or_prompt(self) -> str:
logger.info("password set from keyring")
return password

# As of 2024-01-01, PyPI requires API tokens for uploads;
# specialize the prompt to clarify that an API token must be provided.
if cast(str, self.config["repository"]).startswith(
(utils.DEFAULT_REPOSITORY, utils.TEST_REPOSITORY)
):
prompt = "API token"
else:
prompt = "password"
# Prompt for API token when required.
what = "API token" if self.is_pypi() else "password"

return self.prompt(prompt, getpass.getpass)
return self.prompt(what, getpass.getpass)

def prompt(self, what: str, how: Callable[..., str]) -> str:
return how(f"Enter your {what}: ")

def is_pypi(self) -> bool:
"""
As of 2024-01-01, PyPI requires API tokens for uploads.
"""
return cast(str, self.config["repository"]).startswith((
utils.DEFAULT_REPOSITORY,
utils.TEST_REPOSITORY,
))


class Private(Resolver):
def prompt(self, what: str, how: Optional[Callable[..., str]] = None) -> str:
Expand Down

0 comments on commit 7386ee0

Please sign in to comment.