Skip to content

Manifests and versions

manifest

The package manifest, qvr.toml.

A package is a directory with a qvr.toml manifest::

[package]
name = "discourse"
version = "1.2.0"
modules = ["discourse.score", "discourse.model"]
source = "src"

[holes]
Worlds = { default = "worlds.boolean" }

[links]
Lexicon = "lexicon.core"

[dependencies]
worlds = { path = "../worlds" }
lexicon = { git = "https://example.org/lexicon.git", rev = "v2.0.0" }
grammar = { version = "1.4.0", index = "main" }

[indexes]
main = "https://example.org/qvr-index.git"

The package's name is the first segment of every module path it provides, so the modules of two packages never share a path. modules lists the modules other packages may import; the package's other modules are private to it. holes lists the package's holes (declared in its modules with hole P : S) and the module that fills each when a dependent links none. links fills holes for builds of this package. A dependency comes from a directory (path), a git repository (git, at rev: a commit, a tag, or a branch), or a package index (version, the minimum version the package was built against, looked up in one of indexes).

MANIFEST_NAME module-attribute

MANIFEST_NAME = 'qvr.toml'

The manifest's file name.

LOCKFILE_NAME module-attribute

LOCKFILE_NAME = 'qvr.lock'

The lockfile's file name, beside the manifest.

TomlValue

TomlValue = str | int | float | bool | list[TomlValue] | dict[str, TomlValue]

A value of a parsed TOML document.

ManifestError

Bases: ValueError

A manifest that does not describe a package.

Its message says what is wrong, naming the file and the key.

DependencySource didactic-model

DependencySource()

Bases: TaggedUnion

Where a dependency comes from.

PathSource didactic-model

PathSource(*, path: str, kind: Literal['path'] = 'path')

Bases: DependencySource

A dependency found in a directory.

ATTRIBUTE DESCRIPTION
path

The directory, relative to the depending package's directory, in POSIX spelling.

TYPE: str

GitSource didactic-model

GitSource(*, url: str, rev: str = '', kind: Literal['git'] = 'git')

Bases: DependencySource

A dependency found in a git repository.

ATTRIBUTE DESCRIPTION
url

The repository, as git clone takes it.

TYPE: str

rev

A commit, tag, or branch; the repository's default branch when empty.

TYPE: str

IndexSource didactic-model

IndexSource(*, index: str, minimum: Version, kind: Literal['index'] = 'index')

Bases: DependencySource

A dependency looked up in a package index.

ATTRIBUTE DESCRIPTION
index

The index's name in the depending package's [indexes].

TYPE: str

minimum

The version the dependent was built against; any later version on its compatibility line satisfies it.

TYPE: Version

Dependency didactic-model

Dependency(*, name: str, source: DependencySource)

Bases: Model

One dependency of a package.

ATTRIBUTE DESCRIPTION
name

The depended-on package's name.

TYPE: str

source

Where it comes from.

TYPE: DependencySource

HoleSpec didactic-model

HoleSpec(*, path: str, default: str = '')

Bases: Model

A hole a package exposes.

ATTRIBUTE DESCRIPTION
path

The hole's module path.

TYPE: str

default

The module that fills it when no link does; empty for none.

TYPE: str

LinkSpec didactic-model

LinkSpec(*, hole: str, module: str)

Bases: Model

A hole a package fills for its own builds.

ATTRIBUTE DESCRIPTION
hole

The hole's module path.

TYPE: str

module

The filling module's path.

TYPE: str

IndexSpec didactic-model

IndexSpec(*, name: str, url: str)

Bases: Model

A package index a manifest may look dependencies up in.

ATTRIBUTE DESCRIPTION
name

The name dependencies refer to it by.

TYPE: str

url

The index's git repository.

TYPE: str

Manifest didactic-model

Manifest(*, name: str, version: Version, modules: tuple[str, ...] = (), source: str = '.', holes: tuple[HoleSpec, ...] = (), links: tuple[LinkSpec, ...] = (), dependencies: tuple[Dependency, ...] = (), indexes: tuple[IndexSpec, ...] = ())

Bases: Model

A parsed qvr.toml.

ATTRIBUTE DESCRIPTION
name

The package's name, the first segment of its module paths.

TYPE: str

version

The package's version.

TYPE: Version

modules

The modules dependents may import, sorted.

TYPE: tuple[str, ...]

source

The directory below the package's directory that module paths are relative to (. for the package's directory itself).

TYPE: str

holes

The holes the package exposes, by path.

TYPE: tuple[HoleSpec, ...]

links

The links, by hole path.

TYPE: tuple[LinkSpec, ...]

dependencies

The dependencies, by name.

TYPE: tuple[Dependency, ...]

indexes

The indexes, by name.

TYPE: tuple[IndexSpec, ...]

dependency

dependency(name: str) -> Dependency | None

The dependency on a package.

PARAMETER DESCRIPTION
name

The package's name.

TYPE: str

RETURNS DESCRIPTION
Dependency | None

The dependency, or None when the package is not depended on.

Source code in src/quivers/packages/manifest.py
def dependency(self, name: str) -> Dependency | None:
    """The dependency on a package.

    Parameters
    ----------
    name : str
        The package's name.

    Returns
    -------
    Dependency | None
        The dependency, or ``None`` when the package is not depended on.
    """
    for dependency in self.dependencies:
        if dependency.name == name:
            return dependency
    return None

index_url

index_url(name: str) -> str

The repository of a named index.

PARAMETER DESCRIPTION
name

The index's name.

TYPE: str

RETURNS DESCRIPTION
str

Its repository.

RAISES DESCRIPTION
ManifestError

If the manifest declares no such index.

Source code in src/quivers/packages/manifest.py
def index_url(self, name: str) -> str:
    """The repository of a named index.

    Parameters
    ----------
    name : str
        The index's name.

    Returns
    -------
    str
        Its repository.

    Raises
    ------
    ManifestError
        If the manifest declares no such index.
    """
    for index in self.indexes:
        if index.name == name:
            return index.url
    raise ManifestError(
        f"package {self.name} names the index {name!r}, which its [indexes] "
        "table does not declare"
    )

provides

provides(module: tuple[str, ...]) -> bool

Whether a module path is in this package's namespace.

PARAMETER DESCRIPTION
module

A module path's segments.

TYPE: tuple[str, ...]

RETURNS DESCRIPTION
bool

Whether its first segment is the package's name.

Source code in src/quivers/packages/manifest.py
def provides(self, module: tuple[str, ...]) -> bool:
    """Whether a module path is in this package's namespace.

    Parameters
    ----------
    module : tuple[str, ...]
        A module path's segments.

    Returns
    -------
    bool
        Whether its first segment is the package's name.
    """
    return bool(module) and module[0] == self.name

parse_manifest

parse_manifest(text: str, *, where: str = MANIFEST_NAME) -> Manifest

Read a manifest from its text.

PARAMETER DESCRIPTION
text

The TOML text.

TYPE: str

where

The file name diagnostics name.

TYPE: str DEFAULT: MANIFEST_NAME

RETURNS DESCRIPTION
Manifest

The manifest.

RAISES DESCRIPTION
ManifestError

If the text is not TOML, a key is unknown or mistyped, a name is not a package name, a module is outside the package's namespace, or a dependency names an undeclared index.

Source code in src/quivers/packages/manifest.py
def parse_manifest(text: str, *, where: str = MANIFEST_NAME) -> Manifest:
    """Read a manifest from its text.

    Parameters
    ----------
    text : str
        The TOML text.
    where : str
        The file name diagnostics name.

    Returns
    -------
    Manifest
        The manifest.

    Raises
    ------
    ManifestError
        If the text is not TOML, a key is unknown or mistyped, a name is not
        a package name, a module is outside the package's namespace, or a
        dependency names an undeclared index.
    """
    try:
        document: dict[str, TomlValue] = tomllib.loads(text)
    except tomllib.TOMLDecodeError as error:
        raise ManifestError(f"{where}: {error}") from error
    _refuse_unknown(
        document, ("package", "holes", "links", "dependencies", "indexes"), where
    )
    package = _table(document, "package", where)
    if not package:
        raise ManifestError(f"{where}: a manifest needs a [package] table")
    _refuse_unknown(
        package, ("name", "version", "modules", "source"), f"{where}: package"
    )
    name = _string(package, "name", f"{where}: package")
    if not _NAME.fullmatch(name):
        raise ManifestError(
            f"{where}: package.name {name!r} is not a package name: a lowercase "
            "identifier, the first segment of every module path the package "
            "provides"
        )
    try:
        version = Version.parse(_string(package, "version", f"{where}: package"))
    except VersionError as error:
        raise ManifestError(f"{where}: package.version: {error}") from error
    modules_value = package.get("modules", [])
    if not isinstance(modules_value, list) or not all(
        isinstance(item, str) for item in modules_value
    ):
        raise ManifestError(f"{where}: package.modules must be a list of module paths")
    modules: list[str] = []
    for item in modules_value:
        assert isinstance(item, str)
        path = _module_path(item, f"{where}: package.modules")
        if path.split(".")[0] != name:
            raise ManifestError(
                f"{where}: package.modules lists {path!r}, which is outside the "
                f"package's namespace: its modules are {name} and {name}.*"
            )
        if path in modules:
            raise ManifestError(f"{where}: package.modules lists {path!r} twice")
        modules.append(path)
    source = (
        _string(package, "source", f"{where}: package") if "source" in package else "."
    )
    if Path(source).is_absolute() or ".." in Path(source).parts:
        raise ManifestError(
            f"{where}: package.source {source!r} must be a directory inside the package"
        )
    holes: list[HoleSpec] = []
    for path, value in sorted(_table(document, "holes", where).items()):
        here = f"{where}: holes.{path}"
        _module_path(path, here)
        if not isinstance(value, dict):
            raise ManifestError(f'{here} must be a table, e.g. {{ default = "m" }}')
        _refuse_unknown(value, ("default",), here)
        default = _string(value, "default", here) if "default" in value else ""
        if default:
            _module_path(default, f"{here}.default")
        holes.append(HoleSpec(path=path, default=default))
    links: list[LinkSpec] = []
    for path, value in sorted(_table(document, "links", where).items()):
        here = f"{where}: links.{path}"
        _module_path(path, here)
        if not isinstance(value, str):
            raise ManifestError(f"{here} must be the filling module's path")
        links.append(LinkSpec(hole=path, module=_module_path(value, here)))
    indexes: list[IndexSpec] = []
    for index, value in sorted(_table(document, "indexes", where).items()):
        if not isinstance(value, str):
            raise ManifestError(
                f"{where}: indexes.{index} must be the index repository's URL"
            )
        indexes.append(IndexSpec(name=index, url=value))
    dependencies: list[Dependency] = []
    for dependency_name, value in sorted(
        _table(document, "dependencies", where).items()
    ):
        dependency = _dependency(dependency_name, value, where)
        if dependency_name == name:
            raise ManifestError(f"{where}: package {name} cannot depend on itself")
        origin = dependency.source
        if isinstance(origin, IndexSource):
            declared = tuple(index.name for index in indexes)
            if not origin.index:
                if len(declared) != 1:
                    raise ManifestError(
                        f"{where}: dependencies.{dependency_name} must name its "
                        'index (index = "..."): the manifest declares '
                        + (
                            "no [indexes]"
                            if not declared
                            else f"several: {', '.join(declared)}"
                        )
                    )
                dependency = dependency.with_(source=origin.with_(index=declared[0]))
            elif origin.index not in declared:
                raise ManifestError(
                    f"{where}: dependencies.{dependency_name} names the index "
                    f"{origin.index!r}, which [indexes] does not declare"
                )
        dependencies.append(dependency)
    return Manifest(
        name=name,
        version=version,
        modules=tuple(sorted(modules)),
        source=Path(source).as_posix(),
        holes=tuple(holes),
        links=tuple(links),
        dependencies=tuple(dependencies),
        indexes=tuple(indexes),
    )

read_manifest

read_manifest(directory: Path) -> Manifest

Read the manifest of a package directory.

PARAMETER DESCRIPTION
directory

The package's directory.

TYPE: Path

RETURNS DESCRIPTION
Manifest

Its manifest.

RAISES DESCRIPTION
ManifestError

If the directory has no manifest or the manifest is malformed.

Source code in src/quivers/packages/manifest.py
def read_manifest(directory: Path) -> Manifest:
    """Read the manifest of a package directory.

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

    Returns
    -------
    Manifest
        Its manifest.

    Raises
    ------
    ManifestError
        If the directory has no manifest or the manifest is malformed.
    """
    path = directory / MANIFEST_NAME
    try:
        text = path.read_text(encoding="utf-8")
    except FileNotFoundError as error:
        raise ManifestError(f"{directory} has no {MANIFEST_NAME}") from error
    except OSError as error:
        raise ManifestError(f"{path}: {error}") from error
    return parse_manifest(text, where=str(path))

find_package_root

find_package_root(start: Path) -> Path | None

The nearest directory at or above a path that holds a manifest.

PARAMETER DESCRIPTION
start

A file or directory.

TYPE: Path

RETURNS DESCRIPTION
Path | None

The package's directory, or None when no directory above has a qvr.toml.

Source code in src/quivers/packages/manifest.py
def find_package_root(start: Path) -> Path | None:
    """The nearest directory at or above a path that holds a manifest.

    Parameters
    ----------
    start : Path
        A file or directory.

    Returns
    -------
    Path | None
        The package's directory, or ``None`` when no directory above has a
        ``qvr.toml``.
    """
    current = start.resolve()
    if current.is_file():
        current = current.parent
    for directory in (current, *current.parents):
        if (directory / MANIFEST_NAME).is_file():
            return directory
    return None

versions

Semantic versions, compatibility lines, and version requirements.

A package version is MAJOR.MINOR.PATCH. Two versions are compatible when they share a compatibility line: the leftmost nonzero component and everything before it (1.4.2 is on line 1, 0.3.7 on 0.3, 0.0.4 on 0.0.4). A change that breaks dependents moves a package to a new line; any other change stays on its line. A dependency names the minimum version it was built against, and any later version on the same line satisfies it, which is what minimal version selection needs: a requirement is a lower bound within a line, never a range.

CHANGE_LEVELS module-attribute

CHANGE_LEVELS: tuple[ChangeLevel, ...] = ('none', 'patch', 'minor', 'major')

The change levels from least to most.

ChangeLevel

ChangeLevel = Literal['major', 'minor', 'patch', 'none']

How much a change to a package moves its version: major breaks dependents, minor adds to the interface (or changes the meaning of a program fit to data), patch changes bodies only, none changes nothing.

VersionError

Bases: ValueError

A version or a requirement that does not parse.

Its message says what is malformed, quoting the text.

Version didactic-model

Version(*, major: int, minor: int, patch: int)

Bases: Model

A semantic version MAJOR.MINOR.PATCH.

ATTRIBUTE DESCRIPTION
major, minor, patch

The components.

TYPE: int

parse classmethod

parse(text: str) -> Version

Read a version.

PARAMETER DESCRIPTION
text

MAJOR.MINOR.PATCH with no leading zeros.

TYPE: str

RETURNS DESCRIPTION
Version

The version.

RAISES DESCRIPTION
VersionError

If the text is not a version.

Source code in src/quivers/packages/versions.py
@classmethod
def parse(cls, text: str) -> Version:
    """Read a version.

    Parameters
    ----------
    text : str
        ``MAJOR.MINOR.PATCH`` with no leading zeros.

    Returns
    -------
    Version
        The version.

    Raises
    ------
    VersionError
        If the text is not a version.
    """
    match = _VERSION.fullmatch(text.strip())
    if match is None:
        raise VersionError(
            f"{text!r} is not a version: write MAJOR.MINOR.PATCH, e.g. 1.2.0"
        )
    return cls(
        major=int(match.group(1)),
        minor=int(match.group(2)),
        patch=int(match.group(3)),
    )

key

key() -> tuple[int, int, int]

The version as a sortable key.

RETURNS DESCRIPTION
tuple[int, int, int]

(major, minor, patch).

Source code in src/quivers/packages/versions.py
def key(self) -> tuple[int, int, int]:
    """The version as a sortable key.

    Returns
    -------
    tuple[int, int, int]
        ``(major, minor, patch)``.
    """
    return (self.major, self.minor, self.patch)

render

render() -> str

The version as text.

RETURNS DESCRIPTION
str

MAJOR.MINOR.PATCH.

Source code in src/quivers/packages/versions.py
def render(self) -> str:
    """The version as text.

    Returns
    -------
    str
        ``MAJOR.MINOR.PATCH``.
    """
    return f"{self.major}.{self.minor}.{self.patch}"

line

line() -> tuple[int, ...]

The compatibility line: components up to the leftmost nonzero one.

RETURNS DESCRIPTION
tuple[int, ...]

(major,) from 1.0.0 on, (0, minor) for 0.y.z with y > 0, and (0, 0, patch) below that.

Source code in src/quivers/packages/versions.py
def line(self) -> tuple[int, ...]:
    """The compatibility line: components up to the leftmost nonzero one.

    Returns
    -------
    tuple[int, ...]
        ``(major,)`` from ``1.0.0`` on, ``(0, minor)`` for ``0.y.z``
        with ``y > 0``, and ``(0, 0, patch)`` below that.
    """
    if self.major:
        return (self.major,)
    if self.minor:
        return (0, self.minor)
    return (0, 0, self.patch)

render_line

render_line() -> str

The compatibility line as text.

RETURNS DESCRIPTION
str

1.x for line (1,), 0.3.x for (0, 3), and the exact version for 0.0.z.

Source code in src/quivers/packages/versions.py
def render_line(self) -> str:
    """The compatibility line as text.

    Returns
    -------
    str
        ``1.x`` for line ``(1,)``, ``0.3.x`` for ``(0, 3)``, and the
        exact version for ``0.0.z``.
    """
    line = self.line()
    if len(line) == 3:
        return self.render()
    return ".".join(str(part) for part in line) + ".x"

compatible

compatible(other: Version) -> bool

Whether two versions share a compatibility line.

PARAMETER DESCRIPTION
other

Another version.

TYPE: Version

RETURNS DESCRIPTION
bool

Whether one may stand in for the other.

Source code in src/quivers/packages/versions.py
def compatible(self, other: Version) -> bool:
    """Whether two versions share a compatibility line.

    Parameters
    ----------
    other : Version
        Another version.

    Returns
    -------
    bool
        Whether one may stand in for the other.
    """
    return self.line() == other.line()

bumped

bumped(level: ChangeLevel) -> Version

The next version after a change of a given level.

The leftmost nonzero component is the one a breaking change moves, so on line 0.y a breaking change bumps y and any other change bumps the patch; on line 0.0.z every change bumps z.

PARAMETER DESCRIPTION
level

The change.

TYPE: ChangeLevel

RETURNS DESCRIPTION
Version

The version that publishes it (this version for none).

Source code in src/quivers/packages/versions.py
def bumped(self, level: ChangeLevel) -> Version:
    """The next version after a change of a given level.

    The leftmost nonzero component is the one a breaking change moves,
    so on line ``0.y`` a breaking change bumps ``y`` and any other change
    bumps the patch; on line ``0.0.z`` every change bumps ``z``.

    Parameters
    ----------
    level : ChangeLevel
        The change.

    Returns
    -------
    Version
        The version that publishes it (this version for ``none``).
    """
    if level == "none":
        return self
    if self.major:
        if level == "major":
            return Version(major=self.major + 1, minor=0, patch=0)
        if level == "minor":
            return Version(major=self.major, minor=self.minor + 1, patch=0)
        return Version(major=self.major, minor=self.minor, patch=self.patch + 1)
    if self.minor:
        if level == "major":
            return Version(major=0, minor=self.minor + 1, patch=0)
        return Version(major=0, minor=self.minor, patch=self.patch + 1)
    return Version(major=0, minor=0, patch=self.patch + 1)

level_to

level_to(later: Version) -> ChangeLevel

The largest change a later version may publish over this one.

PARAMETER DESCRIPTION
later

A version greater than this one.

TYPE: Version

RETURNS DESCRIPTION
ChangeLevel

major when the line changes; otherwise minor when the component after the line's leading one grows (on line 1.x, the minor component), else patch; none for equal versions.

RAISES DESCRIPTION
VersionError

If later is not greater than this version.

Source code in src/quivers/packages/versions.py
def level_to(self, later: Version) -> ChangeLevel:
    """The largest change a later version may publish over this one.

    Parameters
    ----------
    later : Version
        A version greater than this one.

    Returns
    -------
    ChangeLevel
        ``major`` when the line changes; otherwise ``minor`` when the
        component after the line's leading one grows (on line ``1.x``,
        the minor component), else ``patch``; ``none`` for equal
        versions.

    Raises
    ------
    VersionError
        If ``later`` is not greater than this version.
    """
    if later.key() == self.key():
        return "none"
    if later.key() < self.key():
        raise VersionError(
            f"{later.render()} does not follow {self.render()}: a published "
            "version only moves forward"
        )
    if not self.compatible(later):
        return "major"
    if self.major and later.minor != self.minor:
        return "minor"
    return "patch"

parse_requirement

parse_requirement(text: str) -> Version

Read a dependency's version requirement: the minimum version it needs.

PARAMETER DESCRIPTION
text

A version such as 1.2.0.

TYPE: str

RETURNS DESCRIPTION
Version

The minimum; any later version on its compatibility line satisfies the requirement.

RAISES DESCRIPTION
VersionError

If the text is a range or not a version: minimal version selection reads a requirement as the version the dependent was built against.

Source code in src/quivers/packages/versions.py
def parse_requirement(text: str) -> Version:
    """Read a dependency's version requirement: the minimum version it needs.

    Parameters
    ----------
    text : str
        A version such as ``1.2.0``.

    Returns
    -------
    Version
        The minimum; any later version on its compatibility line satisfies
        the requirement.

    Raises
    ------
    VersionError
        If the text is a range or not a version: minimal version selection
        reads a requirement as the version the dependent was built against.
    """
    stripped = text.strip()
    if any(mark in stripped for mark in _RANGE_MARKS):
        raise VersionError(
            f"{text!r} is a version range; a dependency names the minimum version "
            "it was built against (e.g. 1.2.0), and any later version on that "
            "compatibility line satisfies it"
        )
    return Version.parse(stripped)

level_at_least

level_at_least(level: ChangeLevel, floor: ChangeLevel) -> ChangeLevel

The larger of two change levels.

PARAMETER DESCRIPTION
level

Two levels.

TYPE: ChangeLevel

floor

Two levels.

TYPE: ChangeLevel

RETURNS DESCRIPTION
ChangeLevel

The one further along CHANGE_LEVELS.

Source code in src/quivers/packages/versions.py
def level_at_least(level: ChangeLevel, floor: ChangeLevel) -> ChangeLevel:
    """The larger of two change levels.

    Parameters
    ----------
    level, floor : ChangeLevel
        Two levels.

    Returns
    -------
    ChangeLevel
        The one further along `CHANGE_LEVELS`.
    """
    return level if CHANGE_LEVELS.index(level) >= CHANGE_LEVELS.index(floor) else floor