Skip to content

Resolution

sources

Packages on disk and where they come from.

A Package is a manifest and the directory its modules live in, together with how it was found: a directory named by a path dependency, a commit of a git repository, an index entry (which names a repository and a commit), or the standard library that ships with quivers. PackageSources materializes each kind, reading git repositories through the project cache.

Package didactic-model

Package(*, manifest: Manifest, directory: str, source: SourceKind, location: str = '', commit: str = '', revision: str = '', implicit: bool = False, published: tuple[LockedModule, ...] = ())

Bases: Model

A package's manifest and the place its modules are.

ATTRIBUTE DESCRIPTION
manifest

Its manifest.

TYPE: Manifest

directory

Its directory, absolute.

TYPE: str

source

How it was found.

TYPE: SourceKind

location

For a path dependency, the directory relative to the root package; for a git dependency, the repository; for an indexed one, the index's repository; empty for the root and the standard library.

TYPE: str

commit

The commit of its source, for git and index.

TYPE: str

revision

For git, the revision the depending manifest names.

TYPE: str

implicit

Whether every package depends on it without saying so (the standard library).

TYPE: bool

published

For an indexed version, the module hashes its index entry publishes.

TYPE: tuple[LockedModule, ...]

name property

name: str

The package's name.

RETURNS DESCRIPTION
str

manifest.name.

version property

version: Version

The package's version.

RETURNS DESCRIPTION
Version

manifest.version.

module_root

module_root() -> Path

The directory module paths of this package are relative to.

RETURNS DESCRIPTION
Path

<directory>/<manifest.source>.

Source code in src/quivers/packages/sources.py
def module_root(self) -> Path:
    """The directory module paths of this package are relative to.

    Returns
    -------
    Path
        ``<directory>/<manifest.source>``.
    """
    return Path(self.directory) / self.manifest.source

module_paths

module_paths() -> tuple[str, ...]

Every module of the package: each .qvr file in its namespace.

RETURNS DESCRIPTION
tuple[str, ...]

The dotted module paths, sorted.

Source code in src/quivers/packages/sources.py
def module_paths(self) -> tuple[str, ...]:
    """Every module of the package: each ``.qvr`` file in its namespace.

    Returns
    -------
    tuple[str, ...]
        The dotted module paths, sorted.
    """
    root = self.module_root()
    namespace = root / self.name
    found: set[str] = set()
    candidates = [root / f"{self.name}{MODULE_SUFFIX}"]
    if namespace.is_dir():
        candidates.extend(namespace.rglob("*" + MODULE_SUFFIX))
    for file in candidates:
        if not file.is_file():
            continue
        relative = file.relative_to(root).with_suffix("")
        dotted = ".".join(relative.parts)
        try:
            parse_module_path(dotted)
        except ValueError:
            continue
        found.add(dotted)
    return tuple(sorted(found))

describe

describe() -> str

The package as diagnostics name it.

RETURNS DESCRIPTION
str

name version and, for a dependency, where it came from.

Source code in src/quivers/packages/sources.py
def describe(self) -> str:
    """The package as diagnostics name it.

    Returns
    -------
    str
        ``name version`` and, for a dependency, where it came from.
    """
    text = f"{self.name} {self.version.render()}"
    if self.source == "path":
        return f"{text} (path {self.location})"
    if self.source == "git":
        return f"{text} (git {self.location} at {self.commit[:12]})"
    if self.source == "index":
        return f"{text} (index {self.location})"
    if self.source == "builtin":
        return f"{text} (the standard library)"
    return text

PackageSources

PackageSources(root: Path, cache: GitCache)

Materialize packages from their sources.

PARAMETER DESCRIPTION
root

The root package's directory, which path dependencies are recorded relative to.

TYPE: Path

cache

The project's git cache.

TYPE: GitCache

Source code in src/quivers/packages/sources.py
def __init__(self, root: Path, cache: GitCache) -> None:
    self.root = root.resolve()
    self.cache = cache

root_package

root_package() -> Package

The root package.

RETURNS DESCRIPTION
Package

The package whose manifest is in the root directory.

RAISES DESCRIPTION
ManifestError

If its manifest is missing or malformed.

Source code in src/quivers/packages/sources.py
def root_package(self) -> Package:
    """The root package.

    Returns
    -------
    Package
        The package whose manifest is in the root directory.

    Raises
    ------
    ManifestError
        If its manifest is missing or malformed.
    """
    return Package(
        manifest=read_manifest(self.root), directory=str(self.root), source="path"
    )

directory

directory(directory: Path) -> Package

The package in a directory, as a path dependency.

PARAMETER DESCRIPTION
directory

The package's directory.

TYPE: Path

RETURNS DESCRIPTION
Package

The package, located relative to the root package.

RAISES DESCRIPTION
ManifestError

If the directory has no manifest or it is malformed.

Source code in src/quivers/packages/sources.py
def directory(self, directory: Path) -> Package:
    """The package in a directory, as a path dependency.

    Parameters
    ----------
    directory : Path
        The package's directory.

    Returns
    -------
    Package
        The package, located relative to the root package.

    Raises
    ------
    ManifestError
        If the directory has no manifest or it is malformed.
    """
    resolved = directory.resolve()
    relative = Path(_relative(resolved, self.root))
    return Package(
        manifest=read_manifest(resolved),
        directory=str(resolved),
        source="path",
        location=relative.as_posix(),
    )

git_manifest

git_manifest(url: str, commit: str) -> Manifest

The manifest at a commit of a repository, without extracting it.

PARAMETER DESCRIPTION
url

The repository.

TYPE: str

commit

The commit.

TYPE: str

RETURNS DESCRIPTION
Manifest

The manifest at the repository's top.

RAISES DESCRIPTION
ManifestError

If the commit has no manifest or it is malformed.

GitError

If the commit cannot be read.

Source code in src/quivers/packages/sources.py
def git_manifest(self, url: str, commit: str) -> Manifest:
    """The manifest at a commit of a repository, without extracting it.

    Parameters
    ----------
    url : str
        The repository.
    commit : str
        The commit.

    Returns
    -------
    Manifest
        The manifest at the repository's top.

    Raises
    ------
    ManifestError
        If the commit has no manifest or it is malformed.
    GitError
        If the commit cannot be read.
    """
    text = self.cache.read(url, commit, MANIFEST_NAME)
    where = f"{url} at {commit[:12]}: {MANIFEST_NAME}"
    if text is None:
        raise ManifestError(f"{where} does not exist: the repository is no package")
    return parse_manifest(text, where=where)

git

git(url: str, commit: str, *, source: SourceKind = 'git', revision: str = '') -> Package

The package at a commit of a repository, extracted into the cache.

PARAMETER DESCRIPTION
url

The repository.

TYPE: str

commit

The commit.

TYPE: str

source

git for a git dependency, index for an indexed version.

TYPE: SourceKind DEFAULT: 'git'

revision

The revision the depending manifest names.

TYPE: str DEFAULT: ''

RETURNS DESCRIPTION
Package

The package.

RAISES DESCRIPTION
ManifestError

If the commit has no manifest or it is malformed.

GitError

If the commit cannot be read.

Source code in src/quivers/packages/sources.py
def git(
    self,
    url: str,
    commit: str,
    *,
    source: SourceKind = "git",
    revision: str = "",
) -> Package:
    """The package at a commit of a repository, extracted into the cache.

    Parameters
    ----------
    url : str
        The repository.
    commit : str
        The commit.
    source : SourceKind
        ``git`` for a git dependency, ``index`` for an indexed version.
    revision : str
        The revision the depending manifest names.

    Returns
    -------
    Package
        The package.

    Raises
    ------
    ManifestError
        If the commit has no manifest or it is malformed.
    GitError
        If the commit cannot be read.
    """
    directory = self.cache.checkout(url, commit)
    return Package(
        manifest=read_manifest(directory),
        directory=str(directory),
        source=source,
        location=url,
        commit=commit,
        revision=revision,
    )

standard_library

standard_library(root: Path = STANDARD_LIBRARY_ROOT) -> Package

The standard library, the package every package depends on.

Its version is the version of quivers it ships with, and every module below std/ is exposed.

PARAMETER DESCRIPTION
root

The directory its std modules live in.

TYPE: Path DEFAULT: STANDARD_LIBRARY_ROOT

RETURNS DESCRIPTION
Package

The package.

Source code in src/quivers/packages/sources.py
def standard_library(root: Path = STANDARD_LIBRARY_ROOT) -> Package:
    """The standard library, the package every package depends on.

    Its version is the version of quivers it ships with, and every module
    below ``std/`` is exposed.

    Parameters
    ----------
    root : Path
        The directory its ``std`` modules live in.

    Returns
    -------
    Package
        The package.
    """
    manifest = Manifest(
        name=STANDARD_LIBRARY_NAME,
        version=Version.parse(quivers.__version__.split("+")[0].split(".dev")[0]),
    )
    package = Package(
        manifest=manifest,
        directory=str(root.resolve()),
        source="builtin",
        implicit=True,
    )
    return package.with_(manifest=manifest.with_(modules=package.module_paths()))

resolve

Dependency resolution by minimal version selection.

Every requirement on an indexed package names the minimum version its dependent was built against. Resolution walks the requirement graph from the root package, visiting every version any requirement names together with that version's own requirements, and selects for each package the largest minimum any visited requirement names [@cox2018minimal]. There is no search and no backtracking: the selection is a function of the manifests and the index, so it is the same on every machine and never moves to a newer version on its own.

A package from a directory, a git commit, or the standard library is pinned: its version is the one its manifest says, every requirement on it must name that same source, and every indexed requirement on it must be satisfied by that version. Two requirements on one package that no single version satisfies (two compatibility lines, a pinned version older than a requirement, two different sources) are refused with an explanation in the style of PubGrub [@pubgrub2018solver]: a chain of "because" steps from the two conflicting requirements up to the root.

ResolutionError

Bases: ValueError

Requirements no build satisfies.

Its message says the explanation, one derivation step per line.

Origin didactic-model

Origin(*, kind: str, location: str = '', commit: str = '')

Bases: Model

Where one requirement says a package comes from.

ATTRIBUTE DESCRIPTION
kind

path, git, index, or builtin.

TYPE: str

location

The absolute directory, the repository, or the index's repository.

TYPE: str

commit

The git commit, for git.

TYPE: str

pinned

pinned() -> bool

Whether this origin fixes one version.

RETURNS DESCRIPTION
bool

True for a directory, a git commit, and the standard library.

Source code in src/quivers/packages/resolve.py
def pinned(self) -> bool:
    """Whether this origin fixes one version.

    Returns
    -------
    bool
        True for a directory, a git commit, and the standard library.
    """
    return self.kind != "index"

Node didactic-model

Node(*, name: str, version: Version, origin: Origin)

Bases: Model

A package at one version, as the requirement graph visits it.

ATTRIBUTE DESCRIPTION
name

The package.

TYPE: str

version

Its version.

TYPE: Version

origin

Where it comes from.

TYPE: Origin

describe

describe() -> str

The node as explanations name it.

RETURNS DESCRIPTION
str

name version.

Source code in src/quivers/packages/resolve.py
def describe(self) -> str:
    """The node as explanations name it.

    Returns
    -------
    str
        ``name version``.
    """
    return f"{self.name} {self.version.render()}"

Requirement didactic-model

Requirement(*, dependent: Node | None = None, name: str, origin: Origin, minimum: Version | None = None, written: str)

Bases: Model

One dependency edge of the requirement graph.

ATTRIBUTE DESCRIPTION
dependent

The package that requires; None for the root.

TYPE: Node | None

name

The package required.

TYPE: str

origin

Where the dependent says it comes from.

TYPE: Origin

minimum

For an indexed requirement, the minimum version.

TYPE: Version | None

written

The requirement as explanations name it (worlds 1.x from 1.2.0, worlds from path ../worlds).

TYPE: str

Edge didactic-model

Edge(*, package: str, dependencies: tuple[str, ...] = ())

Bases: Model

The dependencies of one package of a resolved build.

ATTRIBUTE DESCRIPTION
package

The package.

TYPE: str

dependencies

The packages it depends on, sorted.

TYPE: tuple[str, ...]

Resolution didactic-model

Resolution(*, root: Package, packages: tuple[Package, ...] = (), edges: tuple[Edge, ...] = (), indexes: tuple[LockedIndex, ...] = ())

Bases: Model

A resolved build.

ATTRIBUTE DESCRIPTION
root

The root package.

TYPE: Package

packages

Every other package of the build, by name.

TYPE: tuple[Package, ...]

edges

Each package's dependencies after selection.

TYPE: tuple[Edge, ...]

indexes

The index commits read, by name.

TYPE: tuple[LockedIndex, ...]

package

package(name: str) -> Package | None

The selected package of a name, the root included.

PARAMETER DESCRIPTION
name

A package name.

TYPE: str

RETURNS DESCRIPTION
Package | None

The package, or None when the build has none.

Source code in src/quivers/packages/resolve.py
def package(self, name: str) -> Package | None:
    """The selected package of a name, the root included.

    Parameters
    ----------
    name : str
        A package name.

    Returns
    -------
    Package | None
        The package, or ``None`` when the build has none.
    """
    if name == self.root.name:
        return self.root
    for package in self.packages:
        if package.name == name:
            return package
    return None

dependencies_of

dependencies_of(name: str) -> tuple[str, ...]

The packages one package depends on, the implicit ones included.

PARAMETER DESCRIPTION
name

A package of the build.

TYPE: str

RETURNS DESCRIPTION
tuple[str, ...]

Their names, sorted.

Source code in src/quivers/packages/resolve.py
def dependencies_of(self, name: str) -> tuple[str, ...]:
    """The packages one package depends on, the implicit ones included.

    Parameters
    ----------
    name : str
        A package of the build.

    Returns
    -------
    tuple[str, ...]
        Their names, sorted.
    """
    for edge in self.edges:
        if edge.package == name:
            return edge.dependencies
    return ()

all_packages

all_packages() -> tuple[Package, ...]

The root and every dependency.

RETURNS DESCRIPTION
tuple[Package, ...]

The root first, then the rest by name.

Source code in src/quivers/packages/resolve.py
def all_packages(self) -> tuple[Package, ...]:
    """The root and every dependency.

    Returns
    -------
    tuple[Package, ...]
        The root first, then the rest by name.
    """
    return (self.root, *self.packages)

Resolver

Resolver(sources: PackageSources, *, locked: Lockfile | None = None, standard_library_root: Path = STANDARD_LIBRARY_ROOT)

Resolve a root package's requirements by minimal version selection.

PARAMETER DESCRIPTION
sources

Materializes packages.

TYPE: PackageSources

locked

A lockfile whose index and git commits to read instead of the newest ones, so a locked build resolves offline to the same build.

TYPE: Lockfile | None DEFAULT: None

standard_library_root

Where the standard library's modules are.

TYPE: Path DEFAULT: STANDARD_LIBRARY_ROOT

Source code in src/quivers/packages/resolve.py
def __init__(
    self,
    sources: PackageSources,
    *,
    locked: Lockfile | None = None,
    standard_library_root: Path = STANDARD_LIBRARY_ROOT,
) -> None:
    self.sources = sources
    self.cache: GitCache = sources.cache
    self.locked = locked
    self.standard_library_root = standard_library_root
    self._indexes: dict[str, PackageIndex] = {}
    self._index_names: dict[str, str] = {}
    self._git_manifests: dict[tuple[str, str], Manifest] = {}
    self._git_commits: dict[tuple[str, str], str] = {}
    self._git_revisions: dict[tuple[str, str], str] = {}

resolve

resolve() -> Resolution

Resolve the root package's build.

RETURNS DESCRIPTION
Resolution

Every selected package, its dependencies, and the index commits read.

RAISES DESCRIPTION
ResolutionError

If no build satisfies the requirements, with the explanation.

ManifestError

If a manifest is malformed.

GitError

If a repository cannot be read.

Source code in src/quivers/packages/resolve.py
def resolve(self) -> Resolution:
    """Resolve the root package's build.

    Returns
    -------
    Resolution
        Every selected package, its dependencies, and the index commits
        read.

    Raises
    ------
    ResolutionError
        If no build satisfies the requirements, with the explanation.
    ManifestError
        If a manifest is malformed.
    GitError
        If a repository cannot be read.
    """
    root = self.sources.root_package()
    if root.manifest.dependency(STANDARD_LIBRARY_NAME) is not None:
        raise ResolutionError(
            f"package {root.name} names {STANDARD_LIBRARY_NAME} as a dependency; the "
            "standard library ships with quivers and every package depends on it"
        )
    by_node: dict[str, Requirement] = {}
    visited: list[tuple[Requirement, Node]] = []
    pending = self._requirements(root.manifest, Path(root.directory), None)
    while pending:
        requirement = pending.pop(0)
        node, manifest, directory = self._visit(requirement, by_node)
        visited.append((requirement, node))
        key = _node_key(node)
        if key in by_node:
            continue
        by_node[key] = requirement
        pending.extend(self._node_requirements(node, manifest, directory))
    selected = self._select(visited, by_node, root.name)
    return self._materialize(root, selected, visited)

explain_conflict

explain_conflict(left: Requirement, right: Requirement, reason: str, by_node: Mapping[str, Requirement], root: str) -> str

Explain two requirements no single version satisfies.

The explanation derives an incompatibility from the two requirements and lifts it, one dependency edge at a time, until both sides are the root, as PubGrub reports a failure.

PARAMETER DESCRIPTION
left

The conflicting requirements.

TYPE: Requirement

right

The conflicting requirements.

TYPE: Requirement

reason

Why no version satisfies both.

TYPE: str

by_node

The requirement that first reached each visited node.

TYPE: Mapping[str, Requirement]

root

The root package's name.

TYPE: str

RETURNS DESCRIPTION
str

The explanation, one step per line.

Source code in src/quivers/packages/resolve.py
def explain_conflict(
    left: Requirement,
    right: Requirement,
    reason: str,
    by_node: Mapping[str, Requirement],
    root: str,
) -> str:
    """Explain two requirements no single version satisfies.

    The explanation derives an incompatibility from the two requirements
    and lifts it, one dependency edge at a time, until both sides are the
    root, as PubGrub reports a failure.

    Parameters
    ----------
    left, right : Requirement
        The conflicting requirements.
    reason : str
        Why no version satisfies both.
    by_node : Mapping[str, Requirement]
        The requirement that first reached each visited node.
    root : str
        The root package's name.

    Returns
    -------
    str
        The explanation, one step per line.
    """
    who = {"left": _who(left, root), "right": _who(right, root)}
    lines = [
        f"Because {who['left']} depends on {left.written}",
        f"  and {who['right']} depends on {right.written},",
    ]
    if who["left"] == who["right"]:
        lines.append(f"  {who['left']} cannot be satisfied ({reason}).")
    else:
        lines.append(f"  {who['left']} is incompatible with {who['right']} ({reason}).")
    steps = [("left", step) for step in reversed(_chain(left, by_node)[:-1])]
    steps.extend(("right", step) for step in reversed(_chain(right, by_node)[:-1]))
    _lift(steps, who, root, lines, "{left} is incompatible with {right}")
    return "\n".join(lines)

explain_missing

explain_missing(requirement: Requirement, reason: str, by_node: Mapping[str, Requirement], root: str) -> str

Explain a requirement nothing satisfies.

PARAMETER DESCRIPTION
requirement

The requirement.

TYPE: Requirement

reason

Why nothing satisfies it.

TYPE: str

by_node

The requirement that first reached each visited node.

TYPE: Mapping[str, Requirement]

root

The root package's name.

TYPE: str

RETURNS DESCRIPTION
str

The explanation, one step per line.

Source code in src/quivers/packages/resolve.py
def explain_missing(
    requirement: Requirement,
    reason: str,
    by_node: Mapping[str, Requirement],
    root: str,
) -> str:
    """Explain a requirement nothing satisfies.

    Parameters
    ----------
    requirement : Requirement
        The requirement.
    reason : str
        Why nothing satisfies it.
    by_node : Mapping[str, Requirement]
        The requirement that first reached each visited node.
    root : str
        The root package's name.

    Returns
    -------
    str
        The explanation, one step per line.
    """
    who = {"left": _who(requirement, root)}
    lines = [
        f"Because {who['left']} depends on {requirement.written}",
        f"  and {reason},",
        f"  {who['left']} is forbidden.",
    ]
    steps = [("left", step) for step in reversed(_chain(requirement, by_node)[:-1])]
    _lift(steps, who, root, lines, "{left} is forbidden")
    return "\n".join(lines)