twine.utils module

twine.utils.get_config(path: str) Dict[str, Dict[str, str | None]][source]

Read repository configuration from a file (i.e. ~/.pypirc).


If the default config file doesn’t exist, return a default configuration for pypyi and testpypi.

twine.utils.sanitize_url(url: str) str[source]

Sanitize a URL.

Sanitize URLs, removing any user:password combinations and replacing them with asterisks. Returns the original URL if the string is a non-matching pattern.


url – str containing a URL to sanitize.


str either sanitized or as entered depending on pattern match.

twine.utils._validate_repository_url(repository_url: str) None[source]

Validate the given url for allowed schemes and components.

twine.utils.get_repository_from_config(config_file: str, repository: str, repository_url: str | None = None) Dict[str, str | None][source]

Get repository config command-line values or the .pypirc file.

twine.utils._config_from_repository_url(url: str) Dict[str, str | None][source]
twine.utils.normalize_repository_url(url: str) str[source]
twine.utils.get_file_size(filename: str) str[source]

Return the size of a file in KB, or MB if >= 1024 KB.

twine.utils.check_status_code(response: Response, verbose: bool) None[source]

Generate a helpful message based on the response from the repository.

Raise a custom exception for recognized errors. Otherwise, print the response content (based on the verbose option) before re-raising the HTTPError.

twine.utils.get_userpass_value(cli_value: str | None, config: Dict[str, str | None], key: str, prompt_strategy: Callable[[], str] | None = None) str | None[source]

Get a credential (e.g. a username or password) from the configuration.

Uses the following rules:

  1. If cli_value is specified, use that.

  2. If config[key] is specified, use that.

  3. If prompt_strategy is specified, use its return value.

  4. Otherwise return None

  • cli_value – The value supplied from the command line.

  • config – A dictionary of repository configuration values.

  • key – The credential to look up in config, e.g. "username" or "password".

  • prompt_strategy – An argumentless function to get the value, e.g. from keyring or by prompting the user.


The credential value, i.e. the username or password.

twine.utils.get_cacert(cli_value: str | None, config: Dict[str, str | None], *, key: str = 'ca_cert', prompt_strategy: Callable[[], str] | None = None) str | None

Get the CA bundle via get_userpass_value().

twine.utils.get_clientcert(cli_value: str | None, config: Dict[str, str | None], *, key: str = 'client_cert', prompt_strategy: Callable[[], str] | None = None) str | None

Get the client certificate via get_userpass_value().

class twine.utils.EnvironmentDefault[source]

Get values from environment variable.

__init__(env: str, required: bool = True, default: str | None = None, **kwargs: Any) None[source]
class twine.utils.EnvironmentFlag[source]

Set boolean flag from environment variable.

__init__(env: str, **kwargs: Any) None[source]
static bool_from_env(val: str | None) bool[source]

Allow ‘0’ and ‘false’ and ‘no’ to be False.