Skip to main content

temporal-typing

Declare behavioural specifications in Python using the semantics of TLA⁺.

Python's standard typing module describes the types of values that a program accepts and produces. temporal-typing extends that idea to state transitions, invariants, function contracts, temporal properties, behavioural protocols, and model configurations—declared directly at the signature level using PEP 593 Annotated type annotations and ordinary Python expressions.

pip install temporal-typing

temporal-typing is purely a passive declaration library with zero third-party dependencies. It does not implement a compiler, static analyser, runtime inspector, model checker, or runtime verification engine; as typing leaves checking to mypy and Pyright, temporal-typing leaves it to separate checkers. A checker that model-checks these declarations with Manyan is planned as its own package.

Quick start

Signature-level specifications on an executable class

from dataclasses import dataclass
from typing import Annotated, ClassVar
from temporal_typing import (
    Action, ActionInvariant, Constant, Domain, Ensures, Ghost, Init, Invariant,
    Model, Modifies, Preserves, Requires, State, Where, prime,
)

Positive = Annotated[int, Where(lambda x: x > 0)]


@dataclass
class BankAccount:
    owner: Constant[str]
    balance: State[int] = 0
    ledger: Ghost[tuple[int, ...]] = ()

    init: ClassVar[Annotated[bool, Init(lambda s: s.balance == 0 and s.ledger == ())]]
    non_negative: ClassVar[Annotated[bool, Invariant(lambda s: s.balance >= 0)]]
    ledger_append_only: ClassVar[
        Annotated[bool, ActionInvariant(lambda s: len(prime(s.ledger)) >= len(s.ledger))]
    ]

    def deposit(self, amount: Positive) -> Annotated[
        None,
        Action(
            lambda s, amount: (
                prime(s.balance) == s.balance + amount
                and prime(s.ledger) == s.ledger + (amount,)
            )
        ),
        Preserves(lambda s: s.balance >= 0),
        Modifies("balance", "ledger"),
    ]:
        self.balance += amount

    def withdraw(self, amount: Positive) -> Annotated[
        Positive,
        Requires(lambda s, amount: amount <= s.balance),
        Action(
            lambda s, amount: (
                prime(s.balance) == s.balance - amount
                and prime(s.ledger) == s.ledger + (-amount,)
            )
        ),
        Ensures(lambda s, amount, result: result == amount),
        Modifies("balance", "ledger"),
    ]:
        self.balance -= amount
        return amount


model = Model(
    BankAccount,
    constants={"owner": "alice"},
    domains={
        "deposit.amount": Domain.range(1, 5),
        "withdraw.amount": Domain.range(1, 5),
    },
    state_constraint=lambda s: s.balance <= 10 and len(s.ledger) <= 3,
)

Annotated classes remain ordinary Python:

  • They execute natively with zero runtime overhead or predicate evaluation.
  • Specifications live at the signature level without polluting runtime attributes or methods.
  • They work with @dataclass, Protocol, Generic, inheritance, and from __future__ import annotations.
  • They pass mypy --strict and Pyright because every declaration uses standard PEP 593 Annotated metadata over ordinary Python types.

Pure signature-level protocol with fairness and liveness

from typing import Annotated, Optional, Protocol
from temporal_typing import (
    Action, Assume, Constant, Fairness, Init, Invariant, Model, ModelValue,
    Property, Requires, State, Symmetry, leads_to, prime, strong_fairness,
    unchanged, weak_fairness,
)


class Proc(ModelValue):
    pass


class Mutex(Protocol):
    procs: Constant[frozenset[Proc]]
    holder: State[Optional[Proc]]
    waiting: State[frozenset[Proc]]

    non_empty: Annotated[bool, Assume(lambda s: len(s.procs) > 0)]
    init: Annotated[bool, Init(lambda s: s.holder is None and s.waiting == frozenset())]
    fair: Annotated[
        bool,
        Fairness(
            lambda s: all(
                strong_fairness(s.acquire(p)) and weak_fairness(s.release(p))
                for p in s.procs
            )
        ),
    ]
    starvation_free: Annotated[
        bool,
        Property(lambda s: all(leads_to(p in s.waiting, s.holder == p) for p in s.procs)),
    ]

    def request(self, p: Proc) -> Annotated[
        bool,
        Requires(lambda s, p: p in s.procs and p not in s.waiting and s.holder != p),
        Action(lambda s, p: prime(s.waiting) == (s.waiting | {p}) and unchanged(s.holder)),
    ]: ...

    def acquire(self, p: Proc) -> Annotated[
        bool,
        Requires(lambda s, p: p in s.waiting and s.holder is None),
        Action(lambda s, p: prime(s.holder) == p and prime(s.waiting) == (s.waiting - {p})),
    ]: ...

    def release(self, p: Proc) -> Annotated[
        bool,
        Requires(lambda s, p: s.holder == p),
        Action(lambda s, p: prime(s.holder) is None and unchanged(s.waiting)),
    ]: ...


PROCS = Symmetry({Proc("p1"), Proc("p2"), Proc("p3")})
mutex_model = Model(Mutex, constants={"procs": PROCS}, domains={Proc: PROCS})

API overview

Value-level type qualifiers (PEP 593 Annotated aliases)

Annotation Underlying type TLA⁺ level Meaning
Constant[T] T Constant (0) CONSTANT — fixed across a behavior
State[T] T State (1) VARIABLE — mutable state variable
Ghost[T] T State (1) Auxiliary specification-only variable
Pure[T] T Constant/State (0–1) Pure side-effect-free operator

Specification, contract, framing, fairness, and refinement clauses

All clauses are frozen metadata dataclasses used inside Annotated[...]:

  • System declarations (optional predicate — inline or bound to the annotated method body):
    • Assume(predicate=None, description=None) — constant assumption (ASSUME)
    • Init(predicate=None, description=None) — initial-state predicate (Init)
    • Action(predicate=None, description=None) — atomic state transition (Next disjunct)
    • Invariant(predicate=None, description=None) — state invariant ([]I)
    • ActionInvariant(predicate=None, description=None) — two-state action invariant ([][A]_v)
    • Property(predicate=None, description=None) — temporal property (PROPERTY)
    • Fairness(predicate=None, description=None) — fairness assumption (WF / SF)
  • Contract clauses:
    • Where(predicate, description=None) — refinement type {x \in T : P(x)}
    • Requires(predicate, description=None) — precondition / action guard
    • Ensures(predicate, description=None) — postcondition
    • Preserves(predicate, description=None) — inductive preservation (P(s) => P(s'))
  • Framing and action fairness:
    • Modifies(*variables) — only listed state variables may change
    • Unchanged(*variables) — listed state variables remain UNCHANGED
    • WeakFairness(*variables) — weak fairness on the annotated action (WF_v(A))
    • StrongFairness(*variables) — strong fairness on the annotated action (SF_v(A))
  • Refinement:
    • Refines[Spec](**mapping) — refinement mapping (INSTANCE Spec WITH ...)

Expression primitives (passive TLA⁺ stubs)

  • Logical: implies(p, q), iff(p, q), choose(domain, predicate=None)
  • Action-level: old(x), prime(x), unchanged(*xs), enabled(action)
  • Temporal-level: always(p), eventually(p), leads_to(p, q), weak_fairness(action, *, on=None), strong_fairness(action, *, on=None)

All expression primitives are purely declarative and raise NotImplementedError if called directly at runtime.

Model configuration

  • ModelValue(name) — uninterpreted TLA⁺ model value (subclass for disjoint sorts)
  • Symmetry(iterable) — frozenset subclass marking a SYMMETRY set
  • Domain — finite domains (Domain.range(start, stop) inclusive, Domain.values(...), Domain.boolean())
  • Model(spec, *, constants=..., domains=..., ...) — frozen .cfg declaration

Documentation and examples

  • Specification Semantics — formal TLA⁺ semantics of every declaration, contract clause, refinement, expression mapping, and model configuration.
  • User Guide — practical patterns for signature-level specs, contracts, protocols, refinement, and models.
  • Examples — die_hard.py, bank_account.py, bounded_queue.py, mutex.py, two_phase_commit.py, semaphore.py, contracts.py.

Releasing

scripts/bumpversion.py minor      # preview
scripts/bumpversion.py minor -y   # commit, tag, push -> GitHub Actions publishes to PyPI

License

MIT; see LICENSE.

Metadata

Release files for temporal-typing 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for temporal-typing 0.2.0
File Size Uploaded Provenance Actions
temporal_typing-0.2.0.tar.gz 8.7 kB With provenance Details for temporal_typing-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for temporal-typing 0.2.0
File Interpreter ABI Platform Actions
temporal_typing-0.2.0-py3-none-any.whl Python 3 none any Details for temporal_typing-0.2.0-py3-none-any.whl

Total release size: 18.3 kB

Release files / temporal_typing-0.2.0.tar.gz

Download URL temporal_typing-0.2.0.tar.gz
Size 8.7 kB
Tags Source
SHA-256 checksum
How to use checksums
a2873a9269a8870e68da28ea5e84a9e83b92e23e50cd7c5f7280ead19e591b37
BLAKE2b-256 checksum
How to use checksums
1979e947f2e228a457808bb18aaa4838e3d25687885a77be7db7e9504705a9a3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.

Transparency log

Release files / temporal_typing-0.2.0-py3-none-any.whl

Download URL temporal_typing-0.2.0-py3-none-any.whl
Size 9.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
46753765819949f84d4dbf069cffd28fd9b7454e6c14fe307964953d90aad918
BLAKE2b-256 checksum
How to use checksums
476907492a0822a306c655fc51f0fd2cd2d149c293a3a3176494c96340dabb2c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.

Transparency log

Security for temporal-typing 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for temporal_typing-0.2.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page