metaflow-contracts 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,66 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ pull_request:
7
+
8
+ jobs:
9
+ lint:
10
+ name: Lint & type check
11
+ runs-on: ubuntu-latest
12
+ steps:
13
+ - uses: actions/checkout@v4
14
+
15
+ - uses: actions/setup-python@v5
16
+ with:
17
+ python-version: "3.12"
18
+
19
+ - name: Install lint tools
20
+ run: pip install "ruff>=0.4" "mypy>=1.10" "pydantic>=2.0"
21
+
22
+ - name: ruff check
23
+ run: ruff check .
24
+
25
+ - name: ruff format
26
+ run: ruff format --check .
27
+
28
+ - name: mypy
29
+ run: mypy metaflow_contracts
30
+
31
+ test:
32
+ name: "test / py${{ matrix.python-version }} / ${{ matrix.extras }}"
33
+ runs-on: ubuntu-latest
34
+ needs: lint
35
+ strategy:
36
+ fail-fast: false
37
+ matrix:
38
+ python-version: ["3.10", "3.11", "3.12"]
39
+ extras: ["dev", "dev,pydantic", "dev,beartype", "dev,pydantic,beartype"]
40
+
41
+ steps:
42
+ - uses: actions/checkout@v4
43
+
44
+ - uses: actions/setup-python@v5
45
+ with:
46
+ python-version: ${{ matrix.python-version }}
47
+ cache: pip
48
+
49
+ - name: Install package
50
+ run: pip install -e ".[${{ matrix.extras }}]"
51
+
52
+ - name: Run tests with coverage
53
+ run: |
54
+ pytest \
55
+ --cov=metaflow_contracts \
56
+ --cov-report=xml \
57
+ --cov-report=term-missing \
58
+ --cov-fail-under=90 \
59
+ -v
60
+
61
+ - name: Upload coverage
62
+ if: matrix.python-version == '3.12' && matrix.extras == 'dev,pydantic,beartype'
63
+ uses: codecov/codecov-action@v4
64
+ with:
65
+ files: coverage.xml
66
+ fail_ci_if_error: false
@@ -0,0 +1,27 @@
1
+ name: Publish
2
+
3
+ on:
4
+ push:
5
+ tags: ["v*"]
6
+
7
+ jobs:
8
+ publish:
9
+ runs-on: ubuntu-latest
10
+ environment: pypi
11
+ permissions:
12
+ id-token: write
13
+
14
+ steps:
15
+ - uses: actions/checkout@v4
16
+
17
+ - uses: actions/setup-python@v5
18
+ with:
19
+ python-version: "3.12"
20
+
21
+ - name: Build
22
+ run: |
23
+ pip install build
24
+ python -m build
25
+
26
+ - name: Publish to PyPI
27
+ uses: pypa/gh-action-pypi-publish@release/v1
@@ -0,0 +1,39 @@
1
+ name: Release
2
+
3
+ on:
4
+ workflow_dispatch:
5
+ inputs:
6
+ version:
7
+ description: "Version to release (e.g. 0.1.0)"
8
+ required: true
9
+
10
+ jobs:
11
+ release:
12
+ runs-on: ubuntu-latest
13
+ permissions:
14
+ contents: write
15
+
16
+ steps:
17
+ - uses: actions/checkout@v4
18
+
19
+ - name: Validate version matches pyproject.toml
20
+ run: |
21
+ PYPROJECT_VERSION=$(python3 -c "
22
+ import re, pathlib
23
+ text = pathlib.Path('pyproject.toml').read_text()
24
+ print(re.search(r'version\s*=\s*\"([^\"]+)\"', text).group(1))
25
+ ")
26
+ if [ "$PYPROJECT_VERSION" != "${{ github.event.inputs.version }}" ]; then
27
+ echo "::error::Version mismatch: pyproject.toml has $PYPROJECT_VERSION but release requested ${{ github.event.inputs.version }}"
28
+ exit 1
29
+ fi
30
+
31
+ - name: Create tag and GitHub Release
32
+ env:
33
+ GH_TOKEN: ${{ github.token }}
34
+ run: |
35
+ git tag "v${{ github.event.inputs.version }}"
36
+ git push origin "v${{ github.event.inputs.version }}"
37
+ gh release create "v${{ github.event.inputs.version }}" \
38
+ --title "v${{ github.event.inputs.version }}" \
39
+ --generate-notes
@@ -0,0 +1,18 @@
1
+ __pycache__/
2
+ *.py[cod]
3
+ *$py.class
4
+ *.egg-info/
5
+ dist/
6
+ build/
7
+ .eggs/
8
+ *.egg
9
+ .venv/
10
+ venv/
11
+ .env
12
+ .pytest_cache/
13
+ .coverage
14
+ htmlcov/
15
+ .mypy_cache/
16
+ .ruff_cache/
17
+ .hypothesis/
18
+ *.so
@@ -0,0 +1,184 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship made available under
36
+ the License, as indicated by a copyright notice that is included in
37
+ or attached to the work (an example is provided in the Appendix below).
38
+
39
+ "Derivative Works" shall mean any work, whether in Source or Object
40
+ form, that is based on (or derived from) the Work and for which the
41
+ editorial revisions, annotations, elaborations, or other modifications
42
+ represent, as a whole, an original work of authorship. For the purposes
43
+ of this License, Derivative Works shall not include works that remain
44
+ separable from, or merely link (or bind by name) to the interfaces of,
45
+ the Work and Derivative Works thereof.
46
+
47
+ "Contribution" shall mean, as submitted to the Licensor for inclusion
48
+ in the Work by the copyright owner or by an individual or Legal Entity
49
+ authorized to submit on behalf of the copyright owner. For the purposes
50
+ of this definition, "submitted" means any form of electronic, verbal,
51
+ or written communication sent to the Licensor or its representatives,
52
+ including but not limited to communication on electronic mailing lists,
53
+ source code control systems, and issue tracking systems that are managed
54
+ by, or on behalf of, the Licensor for the purpose of recording and
55
+ discussing information related to the Work, but excluding communication
56
+ that is conspicuously marked or designated in writing by the copyright
57
+ owner as "Not a Contribution."
58
+
59
+ "Contributor" shall mean Licensor and any Legal Entity on behalf of
60
+ whom a Contribution has been received by the Licensor and included
61
+ within the Work.
62
+
63
+ 2. Grant of Copyright License. Subject to the terms and conditions of
64
+ this License, each Contributor hereby grants to You a perpetual,
65
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
66
+ copyright license to reproduce, prepare Derivative Works of,
67
+ publicly display, publicly perform, sublicense, and distribute the
68
+ Work and such Derivative Works in Source or Object form.
69
+
70
+ 3. Grant of Patent License. Subject to the terms and conditions of
71
+ this License, each Contributor hereby grants to You a perpetual,
72
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
73
+ (except as stated in this section) patent license to make, have made,
74
+ use, offer to sell, sell, import, and otherwise transfer the Work,
75
+ where such license applies only to those patent contributions
76
+ Licensable by such Contributor that are necessarily infringed by their
77
+ Contribution(s) alone or by the combination of their Contribution(s)
78
+ with the Work to which such Contribution(s) was submitted. If You
79
+ institute patent litigation against any entity (including a cross-claim
80
+ or counterclaim in a lawsuit) alleging that the Work and/or a
81
+ Contribution incorporated within the Work constitutes direct or
82
+ contributory patent infringement, then any patent licenses granted to
83
+ You under this License for that Work shall terminate as of the date
84
+ such litigation is filed.
85
+
86
+ 4. Redistribution. You may reproduce and distribute copies of the
87
+ Work or Derivative Works thereof in any medium, with or without
88
+ modifications, and in Source or Object form, provided that You
89
+ meet the following conditions:
90
+
91
+ (a) You must give any other recipients of the Work or Derivative
92
+ Works a copy of this License; and
93
+
94
+ (b) You must cause any modified files to carry prominent notices
95
+ stating that You changed the files; and
96
+
97
+ (c) You must retain, in the Source form of any Derivative Works
98
+ that You distribute, all copyright, patent, trademark, and
99
+ attribution notices from the Source form of the Work,
100
+ excluding those notices that do not pertain to any part of
101
+ the Derivative Works; and
102
+
103
+ (d) If the Work includes a "NOTICE" text file as part of its
104
+ distribution, You must include a readable copy of the
105
+ attribution notices contained within such NOTICE file, in
106
+ at least one of the following places: within a NOTICE text
107
+ file distributed as part of the Derivative Works; within
108
+ the Source form or documentation, if provided along with the
109
+ Derivative Works; or, within a display generated by the
110
+ Derivative Works, if and wherever such third-party notices
111
+ normally appear. The contents of the NOTICE file are for
112
+ informational purposes only and do not modify the License.
113
+ You may add Your own attribution notices within Derivative
114
+ Works that You distribute, alongside or in addition to the
115
+ NOTICE text from the Work, provided that such additional
116
+ attribution notices cannot be construed as modifying the License.
117
+
118
+ You may add Your own license statement for Your modifications and
119
+ may provide additional grant of rights to use, copy, modify, merge,
120
+ publish, distribute, sublicense, and/or sell copies of the Work,
121
+ provided Your use, reproduction, and distribution of the Work
122
+ otherwise complies with the conditions stated in this License.
123
+
124
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
125
+ any Contribution intentionally submitted for inclusion in the Work
126
+ by You to the Licensor shall be under the terms and conditions of
127
+ this License, without any additional terms or conditions.
128
+ Notwithstanding the above, nothing herein shall supersede or modify
129
+ the terms of any separate license agreement you may have executed
130
+ with Licensor regarding such Contributions.
131
+
132
+ 6. Trademarks. This License does not grant permission to use the trade
133
+ names, trademarks, service marks, or product names of the Licensor,
134
+ except as required for reasonable and customary use in describing the
135
+ origin of the Work and reproducing the content of the NOTICE file.
136
+
137
+ 7. Disclaimer of Warranty. Unless required by applicable law or
138
+ agreed to in writing, Licensor provides the Work (and each
139
+ Contributor provides its Contributions) on an "AS IS" BASIS,
140
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
141
+ implied, including, without limitation, any warranties or conditions
142
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
143
+ PARTICULAR PURPOSE. You are solely responsible for determining the
144
+ appropriateness of using or reproducing the Work and assume any
145
+ risks associated with Your exercise of permissions under this License.
146
+
147
+ 8. Limitation of Liability. In no event and under no legal theory,
148
+ whether in tort (including negligence), contract, or otherwise,
149
+ unless required by applicable law (such as deliberate and grossly
150
+ negligent acts) or agreed to in writing, shall any Contributor be
151
+ liable to You for damages, including any direct, indirect, special,
152
+ incidental, or exemplary damages of any character arising as a
153
+ result of this License or out of the use or inability to use the
154
+ Work (including but not limited to damages for loss of goodwill,
155
+ work stoppage, computer failure or malfunction, or all other
156
+ commercial damages or losses), even if such Contributor has been
157
+ advised of the possibility of such damages.
158
+
159
+ 9. Accepting Warranty or Additional Liability. While redistributing
160
+ the Work or Derivative Works thereof, You may choose to offer,
161
+ and charge a fee for, acceptance of support, warranty, indemnity,
162
+ or other liability obligations and/or rights consistent with this
163
+ License. However, in accepting such obligations, You may offer only
164
+ conditions consistent on Your behalf, and on your sole responsibility,
165
+ not on behalf of any other Contributor, and only if You agree to
166
+ indemnify, defend, and hold each Contributor harmless for any liability
167
+ incurred by, or claims asserted against, such Contributor by reason
168
+ of your accepting any warranty or additional liability.
169
+
170
+ END OF TERMS AND CONDITIONS
171
+
172
+ Copyright 2026 Nissan Pow
173
+
174
+ Licensed under the Apache License, Version 2.0 (the "License");
175
+ you may not use this file except in compliance with the License.
176
+ You may obtain a copy of the License at
177
+
178
+ http://www.apache.org/licenses/LICENSE-2.0
179
+
180
+ Unless required by applicable law or agreed to in writing, software
181
+ distributed under the License is distributed on an "AS IS" BASIS,
182
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
183
+ See the License for the specific language governing permissions and
184
+ limitations under the License.
@@ -0,0 +1,19 @@
1
+ Metadata-Version: 2.4
2
+ Name: metaflow-contracts
3
+ Version: 0.1.0
4
+ Summary: Input/output validation contracts for Metaflow steps
5
+ License-File: LICENSE
6
+ Requires-Python: >=3.10
7
+ Requires-Dist: metaflow
8
+ Provides-Extra: beartype
9
+ Requires-Dist: beartype>=0.18; extra == 'beartype'
10
+ Provides-Extra: dev
11
+ Requires-Dist: beartype>=0.18; extra == 'dev'
12
+ Requires-Dist: hypothesis>=6.0; extra == 'dev'
13
+ Requires-Dist: mypy>=1.10; extra == 'dev'
14
+ Requires-Dist: pydantic>=2.0; extra == 'dev'
15
+ Requires-Dist: pytest-cov>=5.0; extra == 'dev'
16
+ Requires-Dist: pytest>=8.0; extra == 'dev'
17
+ Requires-Dist: ruff>=0.4; extra == 'dev'
18
+ Provides-Extra: pydantic
19
+ Requires-Dist: pydantic>=2.0; extra == 'pydantic'
@@ -0,0 +1,142 @@
1
+ # metaflow-contracts
2
+
3
+ [![CI](https://github.com/npow/metaflow-contracts/actions/workflows/ci.yml/badge.svg)](https://github.com/npow/metaflow-contracts/actions/workflows/ci.yml)
4
+ [![PyPI](https://img.shields.io/pypi/v/metaflow-contracts)](https://pypi.org/project/metaflow-contracts/)
5
+ [![License: Apache-2.0](https://img.shields.io/badge/License-Apache--2.0-blue.svg)](LICENSE)
6
+ [![Python 3.10+](https://img.shields.io/badge/python-3.10+-blue.svg)](https://www.python.org/downloads/)
7
+
8
+ Catch bad data between Metaflow steps before it corrupts your pipeline.
9
+
10
+ ## The problem
11
+
12
+ Metaflow passes data between steps as untyped artifacts on `self`. A step produces the wrong type — a string where downstream expects a float, a `None` that slipped through — and the failure surfaces two steps later with a confusing traceback pointing nowhere near the cause. There's no built-in way for a step to declare what it promises to produce.
13
+
14
+ ## Quick start
15
+
16
+ ```bash
17
+ pip install metaflow-contracts
18
+ ```
19
+
20
+ ```python
21
+ from metaflow import FlowSpec, step
22
+ from metaflow_contracts import contract
23
+
24
+ class MyFlow(FlowSpec):
25
+
26
+ @step
27
+ @contract(outputs={"scores": list[float]})
28
+ def start(self):
29
+ self.scores = load_scores()
30
+ self.next(self.classify)
31
+
32
+ @step
33
+ @contract(outputs={"label": str, "confidence": float})
34
+ def classify(self):
35
+ self.label, self.confidence = model.predict(self.scores)
36
+ self.next(self.end)
37
+
38
+ @step
39
+ def end(self):
40
+ print(self.label, self.confidence)
41
+ ```
42
+
43
+ If `scores` is the wrong type when `start` finishes, the run fails immediately at that step — not somewhere downstream:
44
+
45
+ ```
46
+ ContractViolationError in step 'start' [output]: 'scores' expected list, got str
47
+ ```
48
+
49
+ ## Install
50
+
51
+ ```bash
52
+ # Core — plain Python type hints
53
+ pip install metaflow-contracts
54
+
55
+ # With Pydantic support
56
+ pip install "metaflow-contracts[pydantic]"
57
+
58
+ # With beartype for generic types (list[int], dict[str, float], Optional, Union…)
59
+ pip install "metaflow-contracts[beartype]"
60
+
61
+ # Everything
62
+ pip install "metaflow-contracts[pydantic,beartype]"
63
+ ```
64
+
65
+ ## Usage
66
+
67
+ ### Output contracts (primary pattern)
68
+
69
+ Put the contract on the step that produces the data. Errors point at the source.
70
+
71
+ ```python
72
+ @step
73
+ @contract(outputs={"label": str, "confidence": float})
74
+ def classify(self):
75
+ self.label = model.predict(self.scores)
76
+ self.confidence = model.score(self.scores)
77
+ self.next(self.end)
78
+ ```
79
+
80
+ ```
81
+ # Wrong type: ContractViolationError in step 'classify' [output]: 'confidence' expected float, got str
82
+ # Missing field: ContractViolationError in step 'classify' [output]: 'label' is missing (expected str)
83
+ ```
84
+
85
+ ### Pydantic models
86
+
87
+ Use a Pydantic model when you want field-level validators or already have schemas defined elsewhere.
88
+
89
+ ```python
90
+ from pydantic import BaseModel, field_validator
91
+
92
+ class ClassifyOutput(BaseModel):
93
+ label: str
94
+ confidence: float
95
+
96
+ @field_validator("confidence")
97
+ @classmethod
98
+ def must_be_probability(cls, v: float) -> float:
99
+ if not 0.0 <= v <= 1.0:
100
+ raise ValueError("must be between 0 and 1")
101
+ return v
102
+
103
+ @step
104
+ @contract(outputs=ClassifyOutput)
105
+ def classify(self):
106
+ self.label = "cat"
107
+ self.confidence = 1.5 # raises: confidence must be between 0 and 1
108
+ self.next(self.end)
109
+ ```
110
+
111
+ ### Input contracts (defensive pattern)
112
+
113
+ Use `inputs=` when consuming artifacts from steps you don't own — third-party flows or fan-in joins where you can't add an output contract upstream.
114
+
115
+ ```python
116
+ @step
117
+ @contract(inputs={"raw_data": list[dict]}, outputs={"result": float})
118
+ def join(self):
119
+ self.result = aggregate(self.raw_data)
120
+ self.next(self.end)
121
+ ```
122
+
123
+ ## How it works
124
+
125
+ `@contract` wraps the step. Input contracts run before the body; output contracts run after. On failure, `ContractViolationError` is raised with the step name, phase (`input`/`output`), field, expected type, and actual type.
126
+
127
+ Plain dict specs use `beartype` for generic checking when available, falling back to `isinstance` for simple types. Pydantic specs delegate to `model_validate`. Both backends are interchangeable — you can mix them freely across steps.
128
+
129
+ ## Development
130
+
131
+ ```bash
132
+ git clone git@github.com:npow/metaflow-contracts.git
133
+ cd metaflow-contracts
134
+ pip install -e ".[dev]"
135
+ pytest # 108 tests, 94%+ coverage
136
+ ruff check . # lint
137
+ mypy metaflow_contracts # type check
138
+ ```
139
+
140
+ ## License
141
+
142
+ Apache 2.0 — see [LICENSE](LICENSE).
@@ -0,0 +1,4 @@
1
+ from .contract import contract
2
+ from .errors import ContractViolationError
3
+
4
+ __all__ = ["ContractViolationError", "contract"]
@@ -0,0 +1,4 @@
1
+ from .plain import validate_plain
2
+ from .pydantic import is_pydantic_model, validate_pydantic
3
+
4
+ __all__ = ["is_pydantic_model", "validate_plain", "validate_pydantic"]
@@ -0,0 +1,47 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import Any
4
+
5
+ from ..errors import ContractViolationError
6
+
7
+ try:
8
+ from beartype.door import is_bearable
9
+
10
+ _beartype_available = True
11
+ except ImportError:
12
+ _beartype_available = False
13
+
14
+ _MISSING = object()
15
+
16
+
17
+ def _type_name(t: Any) -> str:
18
+ return getattr(t, "__name__", None) or repr(t)
19
+
20
+
21
+ def _check(value: Any, expected_type: Any) -> bool:
22
+ if _beartype_available:
23
+ return bool(is_bearable(value, expected_type))
24
+ return isinstance(value, expected_type)
25
+
26
+
27
+ def validate_plain(
28
+ flow_self: Any,
29
+ spec: dict[str, Any],
30
+ phase: str,
31
+ step_name: str,
32
+ ) -> None:
33
+ for field, expected_type in spec.items():
34
+ value = getattr(flow_self, field, _MISSING)
35
+ if value is _MISSING:
36
+ raise ContractViolationError(
37
+ step_name,
38
+ phase,
39
+ f"'{field}' is missing (expected {_type_name(expected_type)})",
40
+ )
41
+ if not _check(value, expected_type):
42
+ raise ContractViolationError(
43
+ step_name,
44
+ phase,
45
+ f"'{field}' expected {_type_name(expected_type)}, "
46
+ f"got {type(value).__name__}",
47
+ )
@@ -0,0 +1,37 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import Any
4
+
5
+ from ..errors import ContractViolationError
6
+
7
+ _MISSING = object()
8
+
9
+
10
+ def is_pydantic_model(obj: Any) -> bool:
11
+ try:
12
+ from pydantic import BaseModel
13
+
14
+ return isinstance(obj, type) and issubclass(obj, BaseModel)
15
+ except ImportError:
16
+ return False
17
+
18
+
19
+ def validate_pydantic(
20
+ flow_self: Any,
21
+ model_cls: Any,
22
+ phase: str,
23
+ step_name: str,
24
+ ) -> None:
25
+ from pydantic import ValidationError
26
+
27
+ fields = model_cls.model_fields
28
+ data = {field: getattr(flow_self, field, _MISSING) for field in fields}
29
+ # Replace _MISSING with None so pydantic sees absent required fields as null
30
+ # (which will fail required-field validation with a clear message)
31
+ coerced = {k: (None if v is _MISSING else v) for k, v in data.items()}
32
+
33
+ try:
34
+ model_cls.model_validate(coerced)
35
+ except ValidationError as exc:
36
+ details = "; ".join(f"'{err['loc'][0]}': {err['msg']}" for err in exc.errors())
37
+ raise ContractViolationError(step_name, phase, details) from exc
@@ -0,0 +1,91 @@
1
+ from __future__ import annotations
2
+
3
+ import functools
4
+ from typing import Any
5
+
6
+ from .backends import is_pydantic_model, validate_plain, validate_pydantic
7
+
8
+ # A contract spec is either a dict of {field: type} or a Pydantic BaseModel class.
9
+ Spec = dict[str, Any] | type
10
+
11
+ # Metaflow copies these attributes onto decorated step functions.
12
+ _METAFLOW_ATTRS = ("is_step", "_base_func", "decorators", "name")
13
+
14
+
15
+ def contract(
16
+ outputs: Spec | None = None,
17
+ inputs: Spec | None = None,
18
+ ) -> Any:
19
+ """
20
+ Validate Metaflow step artifacts against a contract.
21
+
22
+ Args:
23
+ outputs: Primary pattern. Dict of ``{artifact_name: type}`` or a
24
+ Pydantic ``BaseModel`` subclass. Validated *after* the step
25
+ runs. Use this to catch bugs at the source.
26
+ inputs: Defensive pattern. Same format as ``outputs``. Validated
27
+ *before* the step runs. Use when consuming artifacts from
28
+ steps you don't own (e.g. third-party flows, fan-in joins).
29
+
30
+ Raises:
31
+ ContractViolationError: When an artifact is missing or has the wrong type.
32
+ TypeError: When the spec is not a dict or Pydantic BaseModel.
33
+
34
+ Example::
35
+
36
+ @step
37
+ @contract(outputs={"label": str, "confidence": float})
38
+ def classify(self):
39
+ self.label, self.confidence = model.predict(self.scores)
40
+ self.next(self.end)
41
+ """
42
+ # Eagerly validate the specs so misconfiguration is caught at import time,
43
+ # not at runtime inside a long pipeline run.
44
+ if inputs is not None:
45
+ _assert_valid_spec(inputs, "inputs")
46
+ if outputs is not None:
47
+ _assert_valid_spec(outputs, "outputs")
48
+
49
+ # Copy dicts so later mutation of the caller's dict doesn't affect the contract.
50
+ inputs_spec = dict(inputs) if isinstance(inputs, dict) else inputs
51
+ outputs_spec = dict(outputs) if isinstance(outputs, dict) else outputs
52
+
53
+ def decorator(step_func: Any) -> Any:
54
+ @functools.wraps(step_func)
55
+ def wrapper(self: Any, *args: Any, **kwargs: Any) -> Any:
56
+ step_name = step_func.__name__
57
+
58
+ if inputs_spec is not None:
59
+ _validate(self, inputs_spec, phase="input", step_name=step_name)
60
+
61
+ result = step_func(self, *args, **kwargs)
62
+
63
+ if outputs_spec is not None:
64
+ _validate(self, outputs_spec, phase="output", step_name=step_name)
65
+
66
+ return result
67
+
68
+ # Preserve Metaflow step metadata so the framework still recognises
69
+ # the method as a step after we wrap it.
70
+ for attr in _METAFLOW_ATTRS:
71
+ if hasattr(step_func, attr):
72
+ setattr(wrapper, attr, getattr(step_func, attr))
73
+
74
+ return wrapper
75
+
76
+ return decorator
77
+
78
+
79
+ def _validate(flow_self: Any, spec: Spec, phase: str, step_name: str) -> None:
80
+ if is_pydantic_model(spec):
81
+ validate_pydantic(flow_self, spec, phase=phase, step_name=step_name)
82
+ else:
83
+ validate_plain(flow_self, spec, phase=phase, step_name=step_name) # type: ignore[arg-type]
84
+
85
+
86
+ def _assert_valid_spec(spec: Any, name: str) -> None:
87
+ if not (isinstance(spec, dict) or is_pydantic_model(spec)):
88
+ raise TypeError(
89
+ f"Contract '{name}' must be a dict or Pydantic BaseModel subclass, "
90
+ f"got {type(spec).__name__!r}"
91
+ )