piptools.utils module#

piptools.utils._assign_config_to_cli_context(click_context: Context, cli_config_mapping: dict[str, Any]) None#
piptools.utils._build_direct_reference_best_efforts(ireq: InstallRequirement) str#

Return a string of a direct reference URI, whenever possible.

See https://www.python.org/dev/peps/pep-0508/

piptools.utils._convert_to_long_option(key: str) str#

Transform given some-key into --some-key.

piptools.utils._drop_extras(markers: list[_T]) list[_T]#
piptools.utils._invert_negative_bool_options_in_config(ctx: Context, config: dict[str, Any]) dict[str, Any]#
piptools.utils._normalize_config_key(key: str) str#

Transform given some-key into some_key.

piptools.utils._normalize_keys_in_config(config: dict[str, Any]) dict[str, Any]#
piptools.utils._validate_config(click_context: Context, config: dict[str, Any]) None#

Validate parsed config against click command params.

Raises:
  • click.NoSuchOption – if config contains unknown keys.

  • click.BadOptionUsage – if config contains invalid values.

piptools.utils.as_tuple(ireq: InstallRequirement) tuple[str, str, tuple[str, ...]]#

Pull out the (name: str, version:str, extras:(str)) tuple from the pinned InstallRequirement.

piptools.utils.comment(text: str) str#
piptools.utils.copy_install_requirement(template: InstallRequirement, **extra_kwargs: Any) InstallRequirement#

Make a copy of a template InstallRequirement with extra kwargs.

piptools.utils.dedup(iterable: Iterable[_T]) Iterable[_T]#

Deduplicate an iterable object like iter(set(iterable)) but order-preserved.

piptools.utils.drop_extras(ireq: InstallRequirement) None#

Remove “extra” markers (PEP-508) from requirement.

piptools.utils.flat_map(fn: Callable[[_T], Iterable[_S]], collection: Iterable[_T]) Iterator[_S]#

Map a function over a collection and flatten the result by one-level

piptools.utils.format_requirement(ireq: InstallRequirement, marker: Marker | None = None, hashes: set[str] | None = None) str#

Generic formatter for pretty printing InstallRequirements to the terminal in a less verbose way than using its __str__ method.

piptools.utils.format_specifier(ireq: InstallRequirement) str#

Generic formatter for pretty printing the specifier part of InstallRequirements to the terminal.

piptools.utils.get_cli_options(ctx: Context) dict[str, Parameter]#
piptools.utils.get_compile_command(click_ctx: Context) str#

Return a normalized compile command depending on cli context.

The command will be normalized by:
  • expanding options short to long

  • removing values that are already default

  • sorting the arguments

  • removing one-off arguments like ‘–upgrade’

  • removing arguments that don’t change build behaviour like ‘–verbose’

piptools.utils.get_hashes_from_ireq(ireq: InstallRequirement) set[str]#

Given an InstallRequirement, return a set of string hashes in the format “{algorithm}:{hash}”. Return an empty set if there are no hashes in the requirement options.

piptools.utils.get_pip_version_for_python_executable(python_executable: str) Version#

Return pip version for the given python executable.

piptools.utils.get_required_pip_specification() SpecifierSet#

Return pip version specifier requested by current pip-tools installation.

piptools.utils.get_sys_path_for_python_executable(python_executable: str) list[str]#

Return sys.path list for the given python executable.

piptools.utils.install_req_from_line(*args: Any, **kwargs: Any) InstallRequirement#
piptools.utils.is_path_relative_to(path1: Path, path2: Path) bool#

Return True if path1 is relative to path2.

piptools.utils.is_pinned_requirement(ireq: InstallRequirement) bool#

Return whether an InstallRequirement is a “pinned” requirement.

An InstallRequirement is considered pinned if:

  • Is not editable

  • It has exactly one specifier

  • That specifier is “==”

  • The version does not contain a wildcard

Examples:

django==1.8 # pinned django>1.8 # NOT pinned django~=1.8 # NOT pinned django==1.* # NOT pinned

piptools.utils.is_url_requirement(ireq: InstallRequirement) bool#

Return True if requirement was specified as a path or URL.

ireq.original_link will have been set by InstallRequirement.__init__

piptools.utils.key_from_ireq(ireq: InstallRequirement) str#

Get a standardized key for an InstallRequirement.

piptools.utils.key_from_req(req: InstallRequirement | Requirement | Requirement) str#

Get an all-lowercase version of the requirement’s name.

Note: If the argument is an instance of pip._internal.resolution.resolvelib.base.Requirement (like pip._internal.resolution.resolvelib.requirements.SpecifierRequirement), then the name might include an extras specification. Apply strip_extras() to the result of this function if you need the package name only.

Parameters:

req – the requirement the key is computed for

Returns:

the canonical name of the requirement

piptools.utils.lookup_table(values: Iterable[_VT], key: Callable[[_VT], _KT]) dict[_KT, set[_VT]]#

Build a dict-based lookup table (index) elegantly.

piptools.utils.lookup_table_from_tuples(values: Iterable[tuple[_KT, _VT]]) dict[_KT, set[_VT]]#

Build a dict-based lookup table (index) elegantly.

piptools.utils.make_install_requirement(name: str, version: str | Version, ireq: InstallRequirement) InstallRequirement#
piptools.utils.omit_list_value(lst: list[_T], value: _T) list[_T]#

Produce a new list with a given value skipped.

piptools.utils.override_defaults_from_config_file(ctx: Context, param: Parameter, value: str | None) Path | None#

Override click.Command defaults based on specified or discovered config file, returning the pathlib.Path of that config file if specified or discovered.

Returns:

None if no such file is found, else returns the path.

pip-tools will use the first config file found, searching in this order: an explicitly given config file, a .pip-tools.toml, a pyproject.toml file. Those files are searched for in the same directory as the requirements input file, or the current working directory if requirements come via stdin.

piptools.utils.parse_config_file(click_context: Context, config_file: Path) dict[str, Any]#
piptools.utils.select_config_file(src_files: tuple[str, ...]) Path | None#

Return the config file to use for defaults given src_files provided.

piptools.utils.strip_extras(name: str) str#

Strip extras from package name, e.g. pytest[testing] -> pytest.