Skip to main content

context.public_contract

context.public_contract(spec, name)

The public contract of a unit: module docstring, signatures and docstrings, no bodies.

Every expression other than a parameter, an annotation, a default value, a decorator, a base class or a docstring is replaced by ...; a class-level assignment keeps its targets and annotation and has ... for its value. A name of public that a top-level assignment binds is shown as NAME = <value> when the value is a literal (ast.literal_eval accepts it) and as NAME = ... otherwise, with its annotation; that value is contract because the name is declared public. A name has one stub or none: a name that is bound more than once is NAME = ... at its first binding, a type alias is shown as written, and a name that only an import or a statement inside if or try binds has no stub.

Parameters

Name Type Description Default
spec RegenSpec The loaded spec. required
name str Dotted module name, or its unique suffix. required

Returns

Name Type Description
str str Python source of the contract stubs.

Examples

from pathlib import Path
from tempfile import TemporaryDirectory

from babregen import load_spec, make_sample_repo, public_contract

with TemporaryDirectory() as tmp:
    spec = load_spec(make_sample_repo(Path(tmp) / "demo"))
    print(public_contract(spec, "core.calc"))
"""Calculator."""


def add(a: int, b: int) -> int:
    """Add two numbers."""
    ...