Skip to content

path-boundary-violation (EFF202)

Family: Execution Effects (EFF) · Heuristic · Runs when its contract is declared · Experimental · Severity error (a bug) · No automatic fix

Reading, copying, or listing a file whose path is built from a constant path below a root of a declared path-boundaries contract, in one of its scopes.

Heuristic

This rule is heuristic: a premise of its claim can be open ("What a report states" lists them), so a report can be wrong about the code. It runs when its contract is declared. It has not been measured yet. See Rule Precision.

Experimental

It is new, and so is its contract: the keys of path-boundaries, the paths heki places, and the calls it counts as reading, copying, or listing can still change as projects try it. Experimental features can still change in behavior or precision, or be removed, in any release, and each change is listed in the release notes. An opt-in rule that is not experimental is stable and only off by default. See Experimental Features.

What it does

Checks the functions of the scopes of a [[tool.heki.path-boundaries]] contract, less its exempt-scopes, and reports each call that reads, copies, or lists (forbid) a file or directory whose path is built from a constant path at or below one of its roots, directories relative to the project root.

The calls are the catalogued ones that read (open and Path.open in a mode that reads, Path.read_text, Path.read_bytes), copy from (shutil.copy, shutil.copy2, shutil.copyfile, shutil.copytree, Path.copy, Path.copy_into), or list (os.listdir, os.scandir, os.walk, glob.glob, glob.iglob, Path.iterdir, Path.glob, Path.rglob, Path.walk) a path.

heki places the path from how the function builds it: __file__ is the module's file, and a module constant (EXPECTED = Path(__file__).parent / "data" / "expected") has the path its module built, also when another module imports it. It follows locals, /, joinpath, os.path.join, Path(...), .parent, os.path.dirname, with_name, with_suffix, resolve, str, a loop over a tuple or list display, and the unpacking of one. A text heki does not know after the root (EXPECTED / name with name: str) is taken as one relative name. A call heki cannot resolve is not seen: with name of unknown class, EXPECTED / name is not known to be a path, since name may define __rtruediv__.

The result of a function sanitizers names may be read or copied, though it names a file below a root; listing it is still reported. With unresolved = "report", a call on a path heki cannot place (a parameter, the result of another call, a path relative to the working directory, a mode of open it cannot read) is reported too.

What a report states

A report states that the call reads, copies, or lists a path that heki built at or below the root it names, or the result of a sanitizer that it lists, or, under unresolved = "report", a path heki cannot place.

A premise can be open: a name heki does not know after the root is taken as a relative name, though an absolute one would replace the path.

Why is this bad?

A project can keep some files for one purpose only: the expected outputs of tests are compared through one helper, and a test that reads or lists them on its own couples to their layout.

Example

[[tool.heki.path-boundaries]]
name = "expected-outputs"
roots = ["tests/data/expected"]
scopes = ["tests.*"]
forbid = ["read", "copy", "list"]
sanitizers = ["tests.conftest.stored_expected"]
# tests/test_a.py
from pathlib import Path

from tests.conftest import stored_expected

EXPECTED = Path(__file__).parent / "data" / "expected"


def test_a():
    (EXPECTED / "a.txt").read_text()  # EFF202
    stored_expected("a.txt").read_text()
    list(stored_expected("d").glob("*"))  # EFF202

Fix safety

No automatic fix.

Options

  • [[tool.heki.path-boundaries]]:
  • roots: directories relative to the project root, with / between their names.
  • scopes, exempt-scopes: modules, classes, or functions (dotted patterns).
  • forbid: read, copy, list.
  • sanitizers: project functions (dotted patterns) whose result may be read or copied.
  • unresolved: "ignore" (default) or "report".
  • message, fail: as for the other contracts.