Temporal Typing

Behavioural specifications for Python, written as type annotations. typing says what values a program takes and gives. Temporal Typing says how its state may change: invariants, actions, contracts, fairness, temporal properties, and refinement, with the semantics of TLA⁺.

  • Plain Python. Declarations are passive: a specified class runs at zero cost, and mypy --strict and Pyright check it like any other class.
  • TLA⁺ semantics. Every declaration has a formal meaning in TLA⁺, checked by Tanny and Manyan.
  • No dependencies. Python 3.10 or later, and nothing else.

Installation#

bash
pip install temporal-typing
uv tool install tanny   # model checker (optional, no JVM)

At a Glance#

The first specification in Leslie Lamport's Specifying Systems is an hour clock showing only the hour from 1 to 12:

hour_clock.py
from dataclasses import dataclass
from typing import Annotated

from temporal_typing import Action, Init, Invariant, Model, State, prime, spec


@spec(
    init=Init(lambda s: s.hr in range(1, 13)),
    type_ok=Invariant(lambda s: s.hr in range(1, 13)),
)
@dataclass
class HourClock:
    hr: State[int]

    def tick(self) -> Annotated[
        None,
        Action(lambda s: prime(s.hr) == (s.hr + 1 if s.hr != 12 else 1)),
    ]:
        self.hr = self.hr + 1 if self.hr != 12 else 1


hour_clock_model = Model(HourClock)
Equivalent TLA⁺
---- MODULE HourClock ----
EXTENDS Integers
VARIABLE hr

HCini == hr \in (1 .. 12)
HCnxt == hr' = IF hr # 12 THEN hr + 1 ELSE 1
HC    == HCini /\ [][HCnxt]_hr

TypeOK == hr \in (1 .. 12)
====

Run the front-end validator or model-check it with Tanny:

bash
$ python -m temporal_typing examples/hour_clock.py
examples/hour_clock.py:31: hour_clock_model: HourClock; 1 action; invariants type_ok
1 file: 1 specification, 0 contracts, 1 model; 0 errors, 0 warnings

$ tanny check examples/hour_clock.py
PASS examples/hour_clock.py::hour_clock_model  12 states

The Declaration Vocabulary#

GroupDeclarationsLearn More
RolesConstant[T], State[T], Ghost[T], Pure[T], Annotated[T, Where(p)]Roles API
Class clauses (@spec)Assume, Init, Invariant, ActionInvariant, Property, FairnessClauses API
Method clausesAction, Requires, Ensures, Modifies, Refines[A]Actions & Conformance
Primitivesprime, unchanged, enabled, implies, choose, always, eventually, leads_to, weak_fairness, strong_fairnessPrimitives API
ModelsModel, ModelValue, SymmetryModels & Domains

Where to Start#