Skip to content

Workspaces

workspace

Building a package and its dependencies.

A Workspace is a root package (the directory holding qvr.toml) and the build its manifest resolves to. It provides:

  • PackageLoader, which finds a module path in the package whose name is its first segment, so every module of the build is found without search roots;
  • the visibility rule: a module imports the modules of its own package, and the exposed modules of the packages its package depends on; any other import is refused, naming the import;
  • the link set: every package's default links, overridden by the root manifest's [links];
  • Workspace.lock, which resolves the manifest afresh, checks every module of every package, and writes qvr.lock with each module's content hashes;
  • Workspace.build, which resolves against the lockfile (offline once the pinned commits are cached), refuses a lockfile the manifest has moved away from, refuses any module whose hashes differ from their pins, and checks every module of the root package.

PackageErrorCode

PackageErrorCode = Literal['package-module-private', 'package-not-a-dependency', 'package-module-outside', 'package-hole-unlisted', 'package-hole-unknown', 'package-link-conflict', 'package-pin-mismatch', 'package-lock-stale', 'package-module-missing']

The codes a package diagnostic carries.

PackageError

PackageError(code: PackageErrorCode, message: str, location: SourceLocation | None = None)

Bases: ValueError

A build that violates a package rule or disagrees with its pins.

PARAMETER DESCRIPTION
code

The stable code.

TYPE: PackageErrorCode

message

What is wrong.

TYPE: str

location

Where, when the refusal points at source.

TYPE: SourceLocation | None DEFAULT: None

Source code in src/quivers/packages/workspace.py
def __init__(
    self,
    code: PackageErrorCode,
    message: str,
    location: SourceLocation | None = None,
) -> None:
    self.code: PackageErrorCode = code
    self.message = message
    self.location = location
    prefix = f"{location.render()}: " if location is not None else ""
    super().__init__(f"{prefix}error[{code}]: {message}")

PackageLoader

PackageLoader(packages: Iterable[Package])

Find a module in the package its path's first segment names.

PARAMETER DESCRIPTION
packages

Every package of the build.

TYPE: Iterable[Package]

Source code in src/quivers/packages/workspace.py
def __init__(self, packages: Iterable[Package]) -> None:
    self.packages = {package.name: package for package in packages}
    self._loaders = {
        name: FileSystemLoader((package.module_root(),))
        for name, package in self.packages.items()
    }

find

find(path: tuple[str, ...]) -> tuple[ModuleSource, ...]

The source of a module path, from its package.

PARAMETER DESCRIPTION
path

The module path's segments.

TYPE: tuple[str, ...]

RETURNS DESCRIPTION
tuple[ModuleSource, ...]

The module's source, or nothing when no package of the build provides it.

Source code in src/quivers/packages/workspace.py
def find(self, path: tuple[str, ...]) -> tuple[ModuleSource, ...]:
    """The source of a module path, from its package.

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

    Returns
    -------
    tuple[ModuleSource, ...]
        The module's source, or nothing when no package of the build
        provides it.
    """
    loader = self._loaders.get(path[0]) if path else None
    return () if loader is None else loader.find(path)

available

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

Every module the build's packages provide.

RETURNS DESCRIPTION
tuple[str, ...]

The dotted paths, sorted.

Source code in src/quivers/packages/workspace.py
def available(self) -> tuple[str, ...]:
    """Every module the build's packages provide.

    Returns
    -------
    tuple[str, ...]
        The dotted paths, sorted.
    """
    return tuple(
        sorted(
            path
            for package in self.packages.values()
            for path in package.module_paths()
        )
    )

package_of

package_of(path: tuple[str, ...]) -> Package | None

The package providing a module path.

PARAMETER DESCRIPTION
path

The module path's segments.

TYPE: tuple[str, ...]

RETURNS DESCRIPTION
Package | None

The package named by its first segment, if the build has one.

Source code in src/quivers/packages/workspace.py
def package_of(self, path: tuple[str, ...]) -> Package | None:
    """The package providing a module path.

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

    Returns
    -------
    Package | None
        The package named by its first segment, if the build has one.
    """
    return self.packages.get(path[0]) if path else None

BuiltModule didactic-model

BuiltModule(*, path: str, package: str, interface: ModuleInterface, core: str, holes: tuple[str, ...] = (), qiec: QiecModule | None = field(opaque=True, extras=OPAQUE_BY_VALUE), flat: FlatProgram = field(opaque=True, extras=OPAQUE_BY_VALUE))

Bases: ValueModel

One module of a package, checked.

ATTRIBUTE DESCRIPTION
path

The module's path.

TYPE: str

package

Its package's name.

TYPE: str

interface

Its public interface.

TYPE: ModuleInterface

core

The hash of its checked declarations.

TYPE: str

qiec

The checked core of its flat program (or, for a module that needs unfilled holes, of its checking program); None for a module that declares nothing with a core form (only signatures, say).

TYPE: QiecModule | None

flat

Its flat program.

TYPE: FlatProgram

holes

The holes its graph has that modules of its own package declare.

TYPE: tuple[str, ...]

pin

pin() -> LockedModule

The module's pin.

RETURNS DESCRIPTION
LockedModule

Its path and hashes.

Source code in src/quivers/packages/workspace.py
def pin(self) -> LockedModule:
    """The module's pin.

    Returns
    -------
    LockedModule
        Its path and hashes.
    """
    return LockedModule(
        path=self.path, core=self.core, interface=self.interface.digest()
    )

Workspace

Workspace(root: Path, *, cache: GitCache | None = None, standard_library_root: Path = STANDARD_LIBRARY_ROOT)

A root package and its build.

PARAMETER DESCRIPTION
root

The root package's directory.

TYPE: Path

cache

The git cache; the project's own (<root>/.qvr/packages) when omitted.

TYPE: GitCache | 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/workspace.py
def __init__(
    self,
    root: Path,
    *,
    cache: GitCache | None = None,
    standard_library_root: Path = STANDARD_LIBRARY_ROOT,
) -> None:
    self.root = root.resolve()
    self.cache = cache if cache is not None else GitCache.for_package(self.root)
    self.sources = PackageSources(self.root, self.cache)
    self.standard_library_root = standard_library_root

discover classmethod

discover(start: Path, *, cache: GitCache | None = None, standard_library_root: Path = STANDARD_LIBRARY_ROOT) -> Workspace

The workspace of the package containing a path.

PARAMETER DESCRIPTION
start

A file or directory inside the package.

TYPE: Path

cache

The git cache, as for the constructor.

TYPE: GitCache | None DEFAULT: None

standard_library_root

Where the standard library's modules are.

TYPE: Path DEFAULT: STANDARD_LIBRARY_ROOT

RETURNS DESCRIPTION
Workspace

The workspace.

RAISES DESCRIPTION
ManifestError

If no directory at or above start has a qvr.toml.

Source code in src/quivers/packages/workspace.py
@classmethod
def discover(
    cls,
    start: Path,
    *,
    cache: GitCache | None = None,
    standard_library_root: Path = STANDARD_LIBRARY_ROOT,
) -> Workspace:
    """The workspace of the package containing a path.

    Parameters
    ----------
    start : Path
        A file or directory inside the package.
    cache : GitCache | None
        The git cache, as for the constructor.
    standard_library_root : Path
        Where the standard library's modules are.

    Returns
    -------
    Workspace
        The workspace.

    Raises
    ------
    ManifestError
        If no directory at or above ``start`` has a ``qvr.toml``.
    """
    root = find_package_root(start)
    if root is None:
        raise ManifestError(
            f"no qvr.toml at or above {start}: a package is a directory with a "
            "qvr.toml manifest"
        )
    return cls(root, cache=cache, standard_library_root=standard_library_root)

resolve

resolve(*, locked: Lockfile | None = None) -> Resolution

Resolve the root manifest.

PARAMETER DESCRIPTION
locked

A lockfile whose index and git commits to read; the newest ones when omitted.

TYPE: Lockfile | None DEFAULT: None

RETURNS DESCRIPTION
Resolution

The build.

RAISES DESCRIPTION
ResolutionError

If no build satisfies the requirements.

Source code in src/quivers/packages/workspace.py
def resolve(self, *, locked: Lockfile | None = None) -> Resolution:
    """Resolve the root manifest.

    Parameters
    ----------
    locked : Lockfile | None
        A lockfile whose index and git commits to read; the newest ones
        when omitted.

    Returns
    -------
    Resolution
        The build.

    Raises
    ------
    ResolutionError
        If no build satisfies the requirements.
    """
    return Resolver(
        self.sources,
        locked=locked,
        standard_library_root=self.standard_library_root,
    ).resolve()

locked_build

locked_build(locked: Lockfile) -> Resolution

The build a lockfile records, read from the lockfile alone.

The manifest is not consulted beyond the root package itself: each package is materialized from its pinned source (a directory, a git commit, the index entry at the locked index commit, or the standard library), so the build migrated from stays reachable after the manifest has moved on.

PARAMETER DESCRIPTION
locked

The lockfile.

TYPE: Lockfile

RETURNS DESCRIPTION
Resolution

The locked build.

RAISES DESCRIPTION
PackageError

If a locked package no longer has the version the lockfile records.

GitError

If a pinned commit cannot be read.

Source code in src/quivers/packages/workspace.py
def locked_build(self, locked: Lockfile) -> Resolution:
    """The build a lockfile records, read from the lockfile alone.

    The manifest is not consulted beyond the root package itself: each
    package is materialized from its pinned source (a directory, a git
    commit, the index entry at the locked index commit, or the standard
    library), so the build migrated *from* stays reachable after the
    manifest has moved on.

    Parameters
    ----------
    locked : Lockfile
        The lockfile.

    Returns
    -------
    Resolution
        The locked build.

    Raises
    ------
    PackageError
        If a locked package no longer has the version the lockfile
        records.
    GitError
        If a pinned commit cannot be read.
    """
    root = self.sources.root_package()
    packages: list[Package] = []
    for pinned in locked.packages:
        if pinned.source == "builtin":
            package = standard_library(self.standard_library_root)
        elif pinned.source == "path":
            package = self.sources.directory(self.root / pinned.location)
        elif pinned.source == "git":
            package = self.sources.git(
                pinned.location, pinned.commit, revision=pinned.revision
            )
        else:
            index_record = next(
                (item for item in locked.indexes if item.url == pinned.location),
                None,
            )
            if index_record is None:
                raise PackageError(
                    "package-lock-stale",
                    f"qvr.lock pins {pinned.name} from the index {pinned.location}, "
                    "which it records no commit of",
                )
            index = PackageIndex.open(
                index_record.name,
                index_record.url,
                self.cache,
                commit=index_record.commit,
            )
            entry = index.entry(pinned.name, pinned.version)
            package = self.sources.git(entry.url, entry.commit, source="index")
            package = package.with_(
                location=pinned.location, published=entry.modules
            )
        if package.version != pinned.version:
            raise PackageError(
                "package-lock-stale",
                f"qvr.lock pins {pinned.name} {pinned.version.render()}, but its "
                f"source now holds {package.describe()}",
            )
        packages.append(package)
    edges = [Edge(package=root.name, dependencies=locked.dependencies)]
    edges.extend(
        Edge(package=pinned.name, dependencies=pinned.dependencies)
        for pinned in locked.packages
    )
    return Resolution(
        root=root,
        packages=tuple(sorted(packages, key=lambda item: item.name)),
        edges=tuple(sorted(edges, key=lambda item: item.package)),
        indexes=locked.indexes,
    )

loader

loader(resolution: Resolution) -> PackageLoader

The loader of a resolved build.

PARAMETER DESCRIPTION
resolution

The build.

TYPE: Resolution

RETURNS DESCRIPTION
PackageLoader

Finds every module of every package.

Source code in src/quivers/packages/workspace.py
def loader(self, resolution: Resolution) -> PackageLoader:
    """The loader of a resolved build.

    Parameters
    ----------
    resolution : Resolution
        The build.

    Returns
    -------
    PackageLoader
        Finds every module of every package.
    """
    return PackageLoader(resolution.all_packages())
links(resolution: Resolution) -> dict[str, str]

The links of a build: default links, then the root's own.

PARAMETER DESCRIPTION
resolution

The build.

TYPE: Resolution

RETURNS DESCRIPTION
dict[str, str]

Hole path to filling module.

RAISES DESCRIPTION
PackageError

If two packages give one hole different defaults and the root does not link it.

Source code in src/quivers/packages/workspace.py
def links(self, resolution: Resolution) -> dict[str, str]:
    """The links of a build: default links, then the root's own.

    Parameters
    ----------
    resolution : Resolution
        The build.

    Returns
    -------
    dict[str, str]
        Hole path to filling module.

    Raises
    ------
    PackageError
        If two packages give one hole different defaults and the root
        does not link it.
    """
    own = {link.hole: link.module for link in resolution.root.manifest.links}
    defaults: dict[str, tuple[str, str]] = {}
    for package in resolution.all_packages():
        for hole in package.manifest.holes:
            if not hole.default or hole.path in own:
                continue
            prior = defaults.get(hole.path)
            if prior is not None and prior[0] != hole.default:
                raise PackageError(
                    "package-link-conflict",
                    f"packages {prior[1]} and {package.name} give the hole "
                    f"{hole.path} different defaults ({prior[0]} and "
                    f"{hole.default}); link it in [links] of "
                    f"{resolution.root.name}",
                )
            defaults.setdefault(hole.path, (hole.default, package.name))
    merged = {path: module for path, (module, _package) in defaults.items()}
    merged.update(own)
    return dict(sorted(merged.items()))

load

load(module: str, resolution: Resolution, *, extra_links: Mapping[str, str] | None = None, overlay: Mapping[str, str] | None = None) -> ModuleGraph

Load the module graph rooted at one module of the build.

PARAMETER DESCRIPTION
module

The root module's path.

TYPE: str

resolution

The build.

TYPE: Resolution

extra_links

Links beyond the build's (an experiment's), which win.

TYPE: Mapping[str, str] | None DEFAULT: None

overlay

Module texts, by path, read instead of the packages' files (an editor's buffers, a migration's output).

TYPE: Mapping[str, str] | None DEFAULT: None

RETURNS DESCRIPTION
ModuleGraph

The checked graph, linked with the build's links for the holes it has.

RAISES DESCRIPTION
PackageError

If no package provides the module, or an import or link crosses a package boundary the rules forbid.

ModuleError

If the graph does not load or resolve.

Source code in src/quivers/packages/workspace.py
def load(
    self,
    module: str,
    resolution: Resolution,
    *,
    extra_links: Mapping[str, str] | None = None,
    overlay: Mapping[str, str] | None = None,
) -> ModuleGraph:
    """Load the module graph rooted at one module of the build.

    Parameters
    ----------
    module : str
        The root module's path.
    resolution : Resolution
        The build.
    extra_links : Mapping[str, str] | None
        Links beyond the build's (an experiment's), which win.
    overlay : Mapping[str, str] | None
        Module texts, by path, read instead of the packages' files (an
        editor's buffers, a migration's output).

    Returns
    -------
    ModuleGraph
        The checked graph, linked with the build's links for the holes
        it has.

    Raises
    ------
    PackageError
        If no package provides the module, or an import or link
        crosses a package boundary the rules forbid.
    ModuleError
        If the graph does not load or resolve.
    """
    loader = self.loader(resolution)
    finder: ModuleLoader = (
        ChainLoader((MappingLoader(dict(overlay), label="<overlay>"), loader))
        if overlay
        else loader
    )
    path = parse_module_path(module)
    found = finder.find(path)
    if not found:
        raise PackageError(
            "package-module-missing",
            f"no package of the build provides module {module}",
        )
    source = found[0]
    unlinked = load_graph(source, finder)
    links = dict(self.links(resolution))
    links.update(extra_links or {})
    # Link only the holes some module imports: a hole a module merely
    # declares is needed by no code of this graph.
    holes = {
        ".".join(resolved.module.path)
        for checked in unlinked.checked.values()
        for resolved in checked.imports
        if isinstance(resolved.module, HoleModule)
    }
    relevant = {hole: filler for hole, filler in links.items() if hole in holes}
    graph = load_graph(source, finder, links=relevant) if relevant else unlinked
    self._check_visibility(graph, resolution, loader)
    return graph

build_module

build_module(module: str, resolution: Resolution, *, extra_links: Mapping[str, str] | None = None, overlay: Mapping[str, str] | None = None) -> BuiltModule

Check one module of the build as a root.

PARAMETER DESCRIPTION
module

The module's path.

TYPE: str

resolution

The build.

TYPE: Resolution

extra_links

Links beyond the build's.

TYPE: Mapping[str, str] | None DEFAULT: None

overlay

Module texts read instead of the packages' files.

TYPE: Mapping[str, str] | None DEFAULT: None

RETURNS DESCRIPTION
BuiltModule

The checked module and its hashes. A module that needs holes no link fills is hashed by its checking program, which checks it against the holes' signatures.

RAISES DESCRIPTION
PackageError

If an import crosses a package boundary the rules forbid.

ModuleError

If the graph does not load or resolve.

CompileError

If the module does not compile.

Source code in src/quivers/packages/workspace.py
def build_module(
    self,
    module: str,
    resolution: Resolution,
    *,
    extra_links: Mapping[str, str] | None = None,
    overlay: Mapping[str, str] | None = None,
) -> BuiltModule:
    """Check one module of the build as a root.

    Parameters
    ----------
    module : str
        The module's path.
    resolution : Resolution
        The build.
    extra_links : Mapping[str, str] | None
        Links beyond the build's.
    overlay : Mapping[str, str] | None
        Module texts read instead of the packages' files.

    Returns
    -------
    BuiltModule
        The checked module and its hashes. A module that needs holes no
        link fills is hashed by its checking program, which checks it
        against the holes' signatures.

    Raises
    ------
    PackageError
        If an import crosses a package boundary the rules forbid.
    ModuleError
        If the graph does not load or resolve.
    CompileError
        If the module does not compile.
    """
    graph = self.load(module, resolution, extra_links=extra_links, overlay=overlay)
    flat = interpret(graph)
    label = graph.loaded[graph.root].source.label
    checking = check_module_language(flat, module_name=module, file_path=label)
    if flat.unfilled:
        # A module that needs holes no link fills checks against the
        # holes' signatures alone; its checking program is its core.
        checked = checking
    else:
        compiler = Compiler(flat.module, module_name=module, file_path=label)
        compiler.check()
        checked = compiler.qiec_module
        if checked is not None and flat.laws:
            check_signature_laws(flat, checked)
    loader = self.loader(resolution)
    provider = loader.package_of(parse_module_path(module))
    name = provider.name if provider is not None else ""
    holes = tuple(
        sorted(
            ".".join(hole.path)
            for hole, declaration in graph.holes.items()
            if isinstance(declaration.module, SourceModule)
            and declaration.module.path[:1] == (name,)
        )
    )
    return BuiltModule(
        path=module,
        package=name,
        interface=graph.checked[graph.root].interface,
        core=module_core_hash(checked),
        qiec=checked,
        flat=flat,
        holes=holes,
    )

build_package

build_package(package: Package, resolution: Resolution) -> tuple[BuiltModule, ...]

Check the modules of one package that the build pins.

The root package's every module is checked; a dependency's exposed modules are (its private modules are checked through them).

PARAMETER DESCRIPTION
package

A package of the build.

TYPE: Package

resolution

The build.

TYPE: Resolution

RETURNS DESCRIPTION
tuple[BuiltModule, ...]

The checked modules, by path.

RAISES DESCRIPTION
PackageError

If an exposed module does not exist or a hole is unlisted.

ModuleError

If a module does not load or resolve.

CompileError

If a module does not compile.

Source code in src/quivers/packages/workspace.py
def build_package(
    self, package: Package, resolution: Resolution
) -> tuple[BuiltModule, ...]:
    """Check the modules of one package that the build pins.

    The root package's every module is checked; a dependency's exposed
    modules are (its private modules are checked through them).

    Parameters
    ----------
    package : Package
        A package of the build.
    resolution : Resolution
        The build.

    Returns
    -------
    tuple[BuiltModule, ...]
        The checked modules, by path.

    Raises
    ------
    PackageError
        If an exposed module does not exist or a hole is unlisted.
    ModuleError
        If a module does not load or resolve.
    CompileError
        If a module does not compile.
    """
    available = package.module_paths()
    for exposed in package.manifest.modules:
        if exposed not in available:
            raise PackageError(
                "package-module-missing",
                f"package {package.name} exposes {exposed}, which is no file "
                f"below {package.module_root()}",
            )
    modules = (
        available
        if package.name == resolution.root.name
        else package.manifest.modules
    )
    built = tuple(self.build_module(path, resolution) for path in modules)
    self._check_holes(package, built)
    return built

pinned_modules

pinned_modules(package: Package, resolution: Resolution) -> tuple[BuiltModule, ...]

The checked modules a lockfile pins for one dependency.

The standard library ships with quivers, so its version (the release it ships with) identifies its content: it is not checked or pinned module by module. Every other dependency's exposed modules are.

PARAMETER DESCRIPTION
package

A dependency of the build.

TYPE: Package

resolution

The build.

TYPE: Resolution

RETURNS DESCRIPTION
tuple[BuiltModule, ...]

Its pinned modules, by path.

RAISES DESCRIPTION
PackageError

If a module violates a package rule.

ModuleError

If a module does not load or resolve.

CompileError

If a module does not compile.

Source code in src/quivers/packages/workspace.py
def pinned_modules(
    self, package: Package, resolution: Resolution
) -> tuple[BuiltModule, ...]:
    """The checked modules a lockfile pins for one dependency.

    The standard library ships with quivers, so its version (the
    release it ships with) identifies its content: it is not checked
    or pinned module by module. Every other dependency's exposed
    modules are.

    Parameters
    ----------
    package : Package
        A dependency of the build.
    resolution : Resolution
        The build.

    Returns
    -------
    tuple[BuiltModule, ...]
        Its pinned modules, by path.

    Raises
    ------
    PackageError
        If a module violates a package rule.
    ModuleError
        If a module does not load or resolve.
    CompileError
        If a module does not compile.
    """
    if package.source == "builtin":
        return ()
    return self.build_package(package, resolution)

lock

lock() -> Lockfile

Resolve the manifest afresh, check every package, and pin it.

RETURNS DESCRIPTION
Lockfile

The lockfile, also written beside the manifest.

RAISES DESCRIPTION
ResolutionError

If no build satisfies the requirements.

PackageError

If a module violates a package rule, or an indexed version's modules differ from the hashes its index entry publishes.

ModuleError

If a module does not load or resolve.

CompileError

If a module does not compile.

Source code in src/quivers/packages/workspace.py
def lock(self) -> Lockfile:
    """Resolve the manifest afresh, check every package, and pin it.

    Returns
    -------
    Lockfile
        The lockfile, also written beside the manifest.

    Raises
    ------
    ResolutionError
        If no build satisfies the requirements.
    PackageError
        If a module violates a package rule, or an indexed version's
        modules differ from the hashes its index entry publishes.
    ModuleError
        If a module does not load or resolve.
    CompileError
        If a module does not compile.
    """
    resolution = self.resolve()
    lockfile = self._lockfile(resolution)
    (self.root / LOCKFILE_NAME).write_text(
        render_lockfile(lockfile), encoding="utf-8"
    )
    return lockfile

build

build() -> tuple[Lockfile, tuple[BuiltModule, ...]]

Build the root package against its lockfile.

RETURNS DESCRIPTION
tuple[Lockfile, tuple[BuiltModule, ...]]

The lockfile and the root package's checked modules.

RAISES DESCRIPTION
PackageError

If the package has no lockfile, the manifest has moved away from it (package-lock-stale), or a module's hashes differ from its pins (package-pin-mismatch).

ResolutionError

If no build satisfies the requirements.

ModuleError

If a module does not load or resolve.

CompileError

If a module does not compile.

Source code in src/quivers/packages/workspace.py
def build(self) -> tuple[Lockfile, tuple[BuiltModule, ...]]:
    """Build the root package against its lockfile.

    Returns
    -------
    tuple[Lockfile, tuple[BuiltModule, ...]]
        The lockfile and the root package's checked modules.

    Raises
    ------
    PackageError
        If the package has no lockfile, the manifest has moved away
        from it (``package-lock-stale``), or a module's hashes differ
        from its pins (``package-pin-mismatch``).
    ResolutionError
        If no build satisfies the requirements.
    ModuleError
        If a module does not load or resolve.
    CompileError
        If a module does not compile.
    """
    locked = read_lockfile(self.root)
    if locked is None:
        raise PackageError(
            "package-lock-stale",
            f"{self.root / LOCKFILE_NAME} does not exist; run `qvr lock` first",
        )
    resolution = self.resolve(locked=locked)
    self._check_against(locked, resolution)
    for package in resolution.packages:
        pinned = locked.package(package.name)
        assert pinned is not None
        built = self.pinned_modules(package, resolution)
        _compare_pins(
            package, pinned.modules, tuple(m.pin() for m in built), "qvr.lock pins"
        )
    return locked, self.build_package(resolution.root, resolution)

module_core_hash

module_core_hash(module: QiecModule | None) -> str

The hash of a root module's own checked declarations.

The root's declarations are the ones with bare names in its flat program; each enters by its declaration kind and content hash, so renaming, reformatting, and comments leave the hash unchanged.

PARAMETER DESCRIPTION
module

The checked core of a flat program; None for a module with no core declarations, which hashes as the empty set.

TYPE: QiecModule | None

RETURNS DESCRIPTION
str

The digest.

Source code in src/quivers/packages/workspace.py
def module_core_hash(module: QiecModule | None) -> str:
    """The hash of a root module's own checked declarations.

    The root's declarations are the ones with bare names in its flat
    program; each enters by its declaration kind and content hash, so
    renaming, reformatting, and comments leave the hash unchanged.

    Parameters
    ----------
    module : QiecModule | None
        The checked core of a flat program; ``None`` for a module with no
        core declarations, which hashes as the empty set.

    Returns
    -------
    str
        The digest.
    """
    if module is None:
        return tagged_digest("package-module", [])
    own = sorted(
        f"{declaration.kind}:{declaration.hash}"
        for declaration in module_hashes(module).declarations
        if split_module(declaration.name)[0] is None
    )
    return tagged_digest("package-module", list(own))

package_module_of

package_module_of(file: str | Path) -> tuple[Workspace, str] | None

The package a module file belongs to, and the file's module path.

A .qvr file is a module of a package when the nearest qvr.toml at or above it declares a package whose namespace holds the file below the package's module root: coins/model.qvr below a package named coins whose source is . is the module coins.model.

PARAMETER DESCRIPTION
file

The file.

TYPE: str | Path

RETURNS DESCRIPTION
tuple[Workspace, str] | None

The package's workspace and the module path, or None when no manifest is above the file or the file lies outside the package's namespace.

RAISES DESCRIPTION
ManifestError

If the nearest manifest is malformed.

Source code in src/quivers/packages/workspace.py
def package_module_of(file: str | Path) -> tuple[Workspace, str] | None:
    """The package a module file belongs to, and the file's module path.

    A ``.qvr`` file is a module of a package when the nearest ``qvr.toml``
    at or above it declares a package whose namespace holds the file below
    the package's module root: ``coins/model.qvr`` below a package named
    ``coins`` whose ``source`` is ``.`` is the module ``coins.model``.

    Parameters
    ----------
    file : str | Path
        The file.

    Returns
    -------
    tuple[Workspace, str] | None
        The package's workspace and the module path, or ``None`` when no
        manifest is above the file or the file lies outside the package's
        namespace.

    Raises
    ------
    ManifestError
        If the nearest manifest is malformed.
    """
    path = Path(file).resolve()
    if not path.name.endswith(".qvr"):
        return None
    directory = find_package_root(path)
    if directory is None:
        return None
    workspace = Workspace(directory)
    package = workspace.sources.root_package()
    module_root = package.module_root().resolve()
    if not path.is_relative_to(module_root):
        return None
    parts = path.relative_to(module_root).with_suffix("").parts
    if not parts or parts[0] != package.name:
        return None
    return workspace, ".".join(parts)

package_build

package_build(workspace: Workspace) -> Resolution

The build a package's modules are read in.

PARAMETER DESCRIPTION
workspace

The package's workspace.

TYPE: Workspace

RETURNS DESCRIPTION
Resolution

The build its qvr.lock records, or the one its manifest resolves to when it has no lockfile.

RAISES DESCRIPTION
LockfileError

If the lockfile is malformed.

PackageError

If a locked package no longer has the version the lockfile records.

ResolutionError

If no build satisfies the manifest's requirements.

Source code in src/quivers/packages/workspace.py
def package_build(workspace: Workspace) -> Resolution:
    """The build a package's modules are read in.

    Parameters
    ----------
    workspace : Workspace
        The package's workspace.

    Returns
    -------
    Resolution
        The build its ``qvr.lock`` records, or the one its manifest resolves
        to when it has no lockfile.

    Raises
    ------
    LockfileError
        If the lockfile is malformed.
    PackageError
        If a locked package no longer has the version the lockfile records.
    ResolutionError
        If no build satisfies the manifest's requirements.
    """
    locked = read_lockfile(workspace.root)
    return workspace.locked_build(locked) if locked is not None else workspace.resolve()

load_package_file

load_package_file(file: str | Path, *, links: Mapping[str, str] | None = None) -> ModuleGraph | None

The module graph rooted at a file, read in its package's build.

PARAMETER DESCRIPTION
file

The root module's file.

TYPE: str | Path

links

Links beyond the build's (an experiment's), which win.

TYPE: Mapping[str, str] | None DEFAULT: None

RETURNS DESCRIPTION
ModuleGraph | None

The checked graph, or None when the file is no module of a package (package_module_of).

RAISES DESCRIPTION
ManifestError

If the package's manifest is malformed.

PackageError

If an import or link crosses a package boundary the rules forbid, or the lockfile does not match the packages.

ModuleError

If the graph does not load or resolve.

Source code in src/quivers/packages/workspace.py
def load_package_file(
    file: str | Path, *, links: Mapping[str, str] | None = None
) -> ModuleGraph | None:
    """The module graph rooted at a file, read in its package's build.

    Parameters
    ----------
    file : str | Path
        The root module's file.
    links : Mapping[str, str] | None
        Links beyond the build's (an experiment's), which win.

    Returns
    -------
    ModuleGraph | None
        The checked graph, or ``None`` when the file is no module of a
        package (`package_module_of`).

    Raises
    ------
    ManifestError
        If the package's manifest is malformed.
    PackageError
        If an import or link crosses a package boundary the rules forbid,
        or the lockfile does not match the packages.
    ModuleError
        If the graph does not load or resolve.
    """
    found = package_module_of(file)
    if found is None:
        return None
    workspace, module = found
    return workspace.load(module, package_build(workspace), extra_links=links)