session#

In-memory view of the files that check-dev-files inspects and modifies.

The Session follows the Unit of Work pattern together with its companion Identity Map: every managed file is loaded once and, on exit, written back only if it changed. Each check reports its modifications either by mutating one of the managed containers (whose own changelog is collected here) or by appending to Session.changelog directly.

Resources are discovered lazily through Session.get(); adding another file type therefore requires implementing ModifiableResource, without modifying Session.

class Session(precommit: ModifiablePrecommit | None = None, pyproject: ModifiablePyproject | None = None)[source]#

Bases: AbstractContextManager

Shared, mutable working-tree state for all check hooks.

Load the managed files with load() (or pass them in directly, e.g. in tests) and use the session as a context manager so that changed files are dumped once on exit.

changelog: Changelog[source]#

Change messages that do not belong to one of the managed containers.

classmethod load(precommit: ModifiablePrecommit | None = None) Session[source]#

Create a lazy session, optionally with an injected pre-commit resource.

get(resource: type[R], /) R[source]#

Return the one session-owned instance of resource, loading it lazily.

get_path(path: Path | str, /) ModifiablePath[source]#

Return the session-owned generic resource for one working-tree path.

property precommit: ModifiablePrecommit[source]#

The managed .pre-commit-config.yaml file.

property pyproject: ModifiablePyproject | None[source]#

The managed pyproject.toml file, if the repository has one.

property pixi: ModifiablePixi[source]#

The managed pixi.toml file.

collect_changes() list[str][source]#

Aggregate every reported change.

Call this inside the context (before the files are dumped on exit). The order mirrors the historical flat dispatch: free-form messages first (in the order the checks ran), then the container changelogs.

flush() list[str][source]#

Write each changed resource at most once and return the run changelog.