Contracts

Inheritance diagram of dbt_contracts.contracts.Contract, dbt_contracts.contracts.ChildContract, dbt_contracts.contracts.ParentContract, dbt_contracts.contracts.ModelContract, dbt_contracts.contracts.SourceContract, dbt_contracts.contracts.ColumnContract, dbt_contracts.contracts.MacroContract, dbt_contracts.contracts.MacroArgumentContract, dbt_contracts.contracts.ContractContext, dbt_contracts.contracts.ContractPart

Classes

Contract([manifest, catalog, conditions, ...])

Composes the various ContractPart objects that make a contract for specific types of dbt objects within a manifest.

ChildContract([manifest, catalog, ...])

Composes the terms and conditions that make a contract for specific types of dbt child objects within a manifest.

ParentContract([manifest, catalog, ...])

ModelContract([manifest, catalog, ...])

SourceContract([manifest, catalog, ...])

ColumnContract([manifest, catalog, ...])

MacroContract([manifest, catalog, ...])

MacroArgumentContract([manifest, catalog, ...])

ContractContext([manifest, catalog])

Context for a contract to run within.

ContractPart()

A part of a contract.

class dbt_contracts.contracts.Contract(manifest: Manifest = None, catalog: CatalogArtifact = None, conditions: MutableSequence[ContractCondition] = (), terms: MutableSequence[T] = (), generator: G | None = None)

Bases: Generic

Composes the various ContractPart objects that make a contract for specific types of dbt objects within a manifest.

A Contract gets the dbt objects from the dbt artifacts and passes them to the ContractPart objects for processing.

property config_key: str

The key used to identify this contract in a configuration.

property context: ContractContext

Generate a context object from the current loaded dbt artifacts

abstract property filtered_items: Iterable[I]

Get all the items that this contract can process from the manifest filtered according to the given conditions.

classmethod from_dict(config: Mapping[str, Any], manifest: Manifest = None, catalog: CatalogArtifact = None) → Self

Create a contract from a given configuration.

Parameters:
  • config – The configuration to create the contract from.

  • manifest – The dbt manifest to extract items from.

  • catalog – The dbt catalog to extract information on database objects from.

Returns:

The contract.

abstractmethod generate() → dict[Path, int]

Generate the properties files for the items in this contract.

Returns:

Map of the paths of properties to save to the number of items updated for that path.

abstract property items: Iterable[I]

Get all the items that this contract can process from the manifest.

property needs_catalog: bool

Do any of the terms in this contract require a catalog to execute

property needs_manifest: bool

Do any of the terms in this contract require a manifest to execute

abstractmethod validate(terms: Collection[str] = ()) → list[I]

Validate the terms of this contract against the filtered items.

Parameters:

terms – Only run the terms with these names.

Returns:

The valid items.

classmethod validate_conditions(conditions: ContractCondition | Collection[ContractCondition]) → bool

.Validate that all the given conditions are supported by this contract.

classmethod validate_terms(terms: ContractTerm | Collection[ContractTerm]) → bool

.Validate that all the given terms are supported by this contract.

manifest

The dbt manifest to extract items from

catalog

The dbt catalog to extract information on database objects from

conditions

The conditions to apply to items when filtering items to process

terms

The terms to apply to items when validating items

generator

The generator to use when creating properties files from database objects

class dbt_contracts.contracts.ChildContract(manifest: Manifest = None, catalog: CatalogArtifact = None, conditions: MutableSequence[ContractCondition] = (), terms: MutableSequence[ChildContractTerm] = (), generator: G | None = None, parent: ParentContract[I, P, ParentPropertiesGenerator | None] = None)

Bases: Contract[tuple[I, P], ChildContractTerm, G], Generic

Composes the terms and conditions that make a contract for specific types of dbt child objects within a manifest.

property config_key: str

The key used to identify this contract in a configuration.

property filtered_items: Iterable[tuple[I, P]]

Get all the items that this contract can process from the manifest filtered according to the given conditions.

generate() → dict[Path, int]

Generate the properties files for the items in this contract.

Returns:

Map of the paths of properties to save to the number of items updated for that path.

abstract property items: Iterable[tuple[I, P]]

Get all the items that this contract can process from the manifest.

validate(terms: Collection[str] = ()) → list[tuple[I, P]]

Validate the terms of this contract against the filtered items.

Parameters:

terms – Only run the terms with these names.

Returns:

The valid items.

parent

The contract object representing the parent contract for this child contract.

class dbt_contracts.contracts.ParentContract(manifest: Manifest = None, catalog: CatalogArtifact = None, conditions: MutableSequence[ContractCondition] = (), terms: MutableSequence[T] = (), generator: G | None = None)

Bases: Contract[P, ContractTerm, G], Generic

child_config_key
create_child_contract(conditions: MutableSequence[ContractCondition] = (), terms: MutableSequence[ChildContractTerm] = (), generator: GC = None) → ChildContract[I, P, GC] | None

Create a child contract from this parent contract.

create_child_contract_from_dict(config: Mapping[str, Any]) → list[ChildContract[I, P]]

Create a child contract from this parent contract.

property filtered_items: Iterable[P]

Get all the items that this contract can process from the manifest filtered according to the given conditions.

generate() → dict[Path, int]

Generate the properties files for the items in this contract.

Returns:

Map of the paths of properties to save to the number of items updated for that path.

validate(terms: Collection[str] = ()) → list[P]

Validate the terms of this contract against the filtered items.

Parameters:

terms – Only run the terms with these names.

Returns:

The valid items.

class dbt_contracts.contracts.ModelContract(manifest: Manifest = None, catalog: CatalogArtifact = None, conditions: MutableSequence[ContractCondition] = (), terms: MutableSequence[T] = (), generator: G | None = None)

Bases: ParentContract[ColumnInfo, ModelNode, ModelPropertiesGenerator]

property items: Iterable[ModelNode]

Get all the items that this contract can process from the manifest.

class dbt_contracts.contracts.SourceContract(manifest: Manifest = None, catalog: CatalogArtifact = None, conditions: MutableSequence[ContractCondition] = (), terms: MutableSequence[T] = (), generator: G | None = None)

Bases: ParentContract[ColumnInfo, SourceDefinition, SourcePropertiesGenerator]

property items: Iterable[SourceDefinition]

Get all the items that this contract can process from the manifest.

class dbt_contracts.contracts.ColumnContract(manifest: Manifest = None, catalog: CatalogArtifact = None, conditions: MutableSequence[ContractCondition] = (), terms: MutableSequence[ChildContractTerm] = (), generator: G | None = None, parent: ParentContract[I, P, ParentPropertiesGenerator | None] = None)

Bases: ChildContract[ColumnInfo, T, ColumnPropertiesGenerator], Generic

property items: Iterable[tuple[ColumnInfo, T]]

Get all the items that this contract can process from the manifest.

class dbt_contracts.contracts.MacroContract(manifest: Manifest = None, catalog: CatalogArtifact = None, conditions: MutableSequence[ContractCondition] = (), terms: MutableSequence[T] = (), generator: G | None = None)

Bases: ParentContract[MacroArgument, Macro, None]

property items: Iterable[Macro]

Get all the items that this contract can process from the manifest.

class dbt_contracts.contracts.MacroArgumentContract(manifest: Manifest = None, catalog: CatalogArtifact = None, conditions: MutableSequence[ContractCondition] = (), terms: MutableSequence[ChildContractTerm] = (), generator: G | None = None, parent: ParentContract[I, P, ParentPropertiesGenerator | None] = None)

Bases: ChildContract[MacroArgument, Macro, None]

property items: Iterable[tuple[MacroArgument, Macro]]

Get all the items that this contract can process from the manifest.

class dbt_contracts.contracts.ContractContext(manifest: Manifest | None = None, catalog: CatalogArtifact | None = None)

Bases: object

Context for a contract to run within. Stores artifacts for the loaded DBT project and handles logging of results.

add_result(name: str, message: str, item: ItemT, parent: ParentT = None, **kwargs) → None

Create and add a new Result to the current list

Parameters:
  • name – The name to give to the generated result.

  • message – The result message.

  • item – The item that produced the result.

  • parent – The parent of the item that produced the result if available.

  • kwargs – Other result kwargs to pass to the result

catalog: CatalogArtifact | None = None
manifest: Manifest | None = None
properties: ClassVar[PropertiesIO] = <dbt_contracts.properties.PropertiesIO object>
property results: list[Result]

The list of stored results from term validations.

class dbt_contracts.contracts.ContractPart

Bases: BaseModelConfig

A part of a contract.

A ContractPart may only process the items it is given. It does not know how to get those items from any dbt artifacts. These items must be provided by a Contract object.

property name: str

The name of this contract part in snake_case.

Modules

conditions

generators

matchers

result

terms

utils