Skip to content

readonly-view-mutation (MUT120)

Family: Mutation & State (MUT) · Heuristic · Opt-in · Experimental · Severity error (a bug) · No automatic fix

Opt-in: changing a value held through a read-only view (a protocol or class whose members are all properties without a setter) where a type checker does not see it: a helper that does not see the view's type rebinds one of its properties, or code changes in place the container a property returns.

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 only when select or extend-select names its code or name. It has not been measured yet. See Rule Precision.

Experimental

It is new: its reports are heuristic until a review of its reports on real code bases certifies its claim. 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 changes of a value that the code holds through a read-only view, where a type checker does not see them. A read-only view is a class, a typing.Protocol or not, whose members are all properties without a setter or deleter: no plain attribute, no method, no __setattr__, along its bases in the project, and no other base than object, abc.ABC, Generic, or Protocol. A value is held through the view when its type is the view by annotation, as for MUT108: a parameter, a variable, an element of a Sequence[View] another view returns.

A type checker reports a write of a view's property made through the view (view.required = True), but not two other ways to change the value:

  • handing the view to a helper that does not see its type (an unannotated or Any parameter), which rebinds or deletes one of the view's properties (p.required = ..., del p.required, setattr(p, "required", ...)), itself or through further helpers. The call that hands the view over is reported, with the calls down to the write as related locations;
  • changing in place the container a property returns, when the property's annotation is a mutable container type (view.extras["k"] = v, view.items.append(x)), directly, through a local alias, or through helpers (fill(view.extras), reset(view) changing p.extras), as MUT108 does for frozen fields. Through a helper, a property annotated with a read-only type (Sequence, Mapping, AbstractSet) counts too.

The rule does not report:

  • a write of a property through the view itself, or through a helper whose parameter is annotated with the view: the type checker reports it there. A helper that sees the view and hands it on to one that does not is reported at its own call;
  • a change made through a value of a class that implements the view (field: Field, not field: FieldView);
  • a direct change of a container a property annotated Sequence / Mapping / AbstractSet returns: the type checker reports it;
  • a copy (extras = dict(view.extras), fill(list(view.items))), and a container inside the one the property returns (view.tables["a"].append(1));
  • a class with a setter or deleter, a plain attribute (required: bool in a protocol), a method, or a functools.cached_property: it is no read-only view;
  • an attribute the view does not declare (p.cache = {} in a helper).

Out of scope, both for a protocol that classes in the project implement: a property whose getter returns one attribute of their own in every implementation (return self._extras), which heki follows to that attribute, one the view does not declare; and the container of a property handed to a helper (fill(view.extras)), which heki does not tie to one class. Such changes are not reported.

What a report states

A report states that a value the code holds through a read-only view is changed: a helper that does not see the view's type rebinds or deletes one of its properties, or the container a property returns is changed in place.

Its premises:

  • Read-only view: every member of the class and of its bases in the project is a property without a setter or deleter. Established from the declarations.
  • Held through the view: the value's type is the view, by annotation (assumption R2).
  • Changed: an attribute store or deletion in a helper whose parameter has no type a checker reads, or an item store or mutator method on the container a property returns. Open through a helper: a change on one of its paths is enough.

Why is this bad?

A read-only view hands code the facts it may read and nothing it may change: callers pass their live objects through it, often while other code relies on them being final. A type checker keeps the promise only as far as it sees the view's type. A helper without annotations, or a container a property returns, lets a change through, and the change reaches every other holder of the object.

Example

from typing import Protocol


class FieldView(Protocol):
    @property
    def required(self) -> bool: ...

    @property
    def extras(self) -> dict[str, object]: ...


def _flip(field):
    field.required = not field.required


def decide(field: FieldView) -> None:
    _flip(field)  # MUT120: `_flip` rebinds the view's property `required`
    field.extras["init"] = False  # MUT120: changes the container `extras` returns

Use instead:

from collections.abc import Mapping
from typing import Protocol


class FieldView(Protocol):
    @property
    def required(self) -> bool: ...

    @property
    def extras(self) -> Mapping[str, object]: ...


def decide(field: FieldView) -> dict[str, object]:
    return {**field.extras, "init": False, "required": not field.required}

Fix safety

No automatic fix.

Options

  • lint.extend-select = ["MUT120"] (or --extend-select MUT120) enables the rule. It is opt-in: selecting the MUT prefix does not enable it.
  • No other configuration: the rule reads standard typing and abc declarations only.