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

Add private API documentation #2060

Merged
merged 7 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Use reST docstring formatting where appropriate
  • Loading branch information
chrysle authored and webknjaz committed Dec 16, 2024
commit 712d2e40c23c80ab6d5c9f6bfff2a937bc24099b
1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
("py:class", "_ImportLibDist"),
("py:class", "PackageMetadata"),
("py:class", "importlib.*"),
("py:class", "IndexContent"),
("py:exc", "click.*"),
]

Expand Down
11 changes: 8 additions & 3 deletions piptools/_compat/pip_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,12 @@ def _from_pkg_resources(cls, dist: _PkgResourcesDist) -> Distribution:

@classmethod
def _from_importlib(cls, dist: _ImportLibDist) -> Distribution:
"""Mimics pkg_resources.Distribution.requires for the case of no
extras. This doesn't fulfill that API's `extras` parameter but
satisfies the needs of pip-tools."""
"""Mimic pkg_resources.Distribution.requires for the case of no
extras.

This doesn't fulfill that API's ``extras`` parameter but
satisfies the needs of pip-tools.
"""
reqs = (Requirement.parse(req) for req in (dist._dist.requires or ()))
requires = [
req
Expand All @@ -63,6 +66,8 @@ def _from_importlib(cls, dist: _ImportLibDist) -> Distribution:


class FileLink(Link): # type: ignore[misc]
"""Wrapper for ``pip``'s ``Link`` class."""

_url: str

@property
Expand Down
51 changes: 35 additions & 16 deletions piptools/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,10 @@ class BaseResolver(metaclass=ABCMeta):

@abstractmethod
def resolve(self, max_rounds: int) -> set[InstallRequirement]:
"""
r"""
Find concrete package versions for all the given InstallRequirements
and their recursive dependencies and return a set of pinned
``InstallRequirement``'s.
and their recursive dependencies.
:returns: a set of pinned ``InstallRequirement``\ s.
"""

def resolve_hashes(
Expand Down Expand Up @@ -199,9 +199,24 @@ def __init__(
allow_unsafe: bool = False,
unsafe_packages: set[str] | None = None,
) -> None:
"""
Make sure the legacy resolver is enabled and no backtracking resolver
is present.
"""Initialize LegacyResolver.

:param constraints: the constraints given
:type constraints: Iterable[InstallRequirement]
:param existing_constraints: constraints already present
:param repository: the repository to get the constraints from
:type repository: BaseRepository
:param cache: the cache to be used
:param prereleases: whether prereleases should be taken into account when resolving
(default is :py:data:`False`)
:param clear_caches: whether to clear repository and dependency caches before resolving
(default is :py:data:`False`)
:param allow_unsafe: whether unsafe packages should be allowed in the resulting requirements
(default is :py:data:`False`)
:param unsafe_packages: packages to be considered as unsafe
(default is :py:data:`None`)
:type unsafe_packages: set[str]
:raises: ``PipToolsError`` if the legacy resolver is not enabled
"""
self.our_constraints = set(constraints)
self.their_constraints: set[InstallRequirement] = set()
Expand Down Expand Up @@ -230,13 +245,15 @@ def constraints(self) -> set[InstallRequirement]:

def resolve(self, max_rounds: int = 10) -> set[InstallRequirement]:
r"""
Find concrete package versions for all the given InstallRequirements
Find concrete package versions for all the given ``InstallRequirement``\ s
and their recursive dependencies and return a set of pinned
``InstallRequirement``\ s.

Resolves constraints one round at a time, until they don't change
anymore. Protects against infinite loops by breaking out after a max
number rounds.
anymore.

:param max_rounds: break out of resolution process after the given number of rounds
to prevent infinite loops (default is 10)
"""
if self.clear_caches:
self.dependency_cache.clear()
Expand Down Expand Up @@ -324,9 +341,9 @@ def _resolve_one_round(self) -> tuple[bool, set[InstallRequirement]]:
package versions. Some of these constraints may be new
or updated.

Returns whether new constraints appeared in this round. If no
constraints were added or changed, this indicates a stable
configuration.
:returns: whether new constraints appeared in this round. If no
constraints were added or changed, this indicates a stable
configuration.
"""
# Sort this list for readability of terminal output
constraints = sorted(self.constraints, key=key_from_ireq)
Expand Down Expand Up @@ -536,8 +553,9 @@ def resolve(self, max_rounds: int = 10) -> set[InstallRequirement]:
Resolve given ireqs.

Find concrete package versions for all the given InstallRequirements
and their recursive dependencies and return a set of pinned
``InstallRequirement``\ s.
and their recursive dependencies.

:returns: A set of pinned ``InstallRequirement``\ s.
"""
with update_env_context_manager(
PIP_EXISTS_ACTION="i"
Expand Down Expand Up @@ -644,8 +662,9 @@ def _do_resolve(
"""
Resolve dependencies based on resolvelib ``Resolver``.

Return :py:data:`True` on successful resolution, otherwise remove problematic
requirements from existing constraints and return false.
:returns: :py:data:`True` on successful resolution, otherwise removes
problematic requirements from existing constraints and
returns :py:data:`False`.
"""
try:
resolver.resolve(
Expand Down
7 changes: 4 additions & 3 deletions piptools/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@
def dependency_tree(
installed_keys: Mapping[str, Distribution], root_key: str
) -> set[str]:
"""Calculate the dependency tree for a package
"""Calculate the dependency tree for a package.

Return a collection of all of the package's dependencies.
Uses a DFS traversal algorithm.

``installed_keys`` should be a {key: requirement} mapping, e.g.
{'django': from_line('django==1.8')}
``root_key`` should be the key to return the dependency tree for.
:param root_key: the key to return the dependency tree for
:type root_key: str
"""
dependencies = set()
queue: Deque[Distribution] = collections.deque()
Expand Down Expand Up @@ -127,7 +128,7 @@ def diff_key_from_ireq(ireq: InstallRequirement) -> str:

For URL requirements, only provide a useful key if the url includes
a hash, e.g. #sha1=..., in any of the supported hash algorithms.
Otherwise return ireq.link so the key will not match and the package will
Otherwise return ``ireq.link`` so the key will not match and the package will
reinstall. Reinstall is necessary to ensure that packages will reinstall
if the contents at the URL have changed but the version has not.
"""
Expand Down
2 changes: 1 addition & 1 deletion piptools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ def override_defaults_from_config_file(
file, returning the ``pathlib.Path`` of that config file if specified or
discovered.

Return :py:data:`None` if no such file is found.
:returns: :py:data:`None` if no such file is found.
chrysle marked this conversation as resolved.
Show resolved Hide resolved

``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``
Expand Down
3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ skip_install = true

[testenv:pip-compile-docs]
description = compile requirements for the documentation
changedir = {[testenv:build-docs]changedir}
commands_pre =
commands = python -m piptools compile --strip-extras --allow-unsafe --quiet docs/requirements.in {posargs}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd leave this out of this PR. It's not related to documenting things.

commands = python -m piptools compile --strip-extras --allow-unsafe --quiet requirements.in {posargs}

[testenv:build-docs]
description = build the documentation
Expand Down