Create a Minimal Great Expectations Suite Mock in Python
Build a small Python class that mimics a Great Expectations suite, storing and serializing column expectations as JSON.
Python code
30 linesimport json
class GreatExpectationsSuite:
"""A minimal mock of a Great Expectations suite."""
def __init__(self, suite_name, expectations=None):
self.suite_name = suite_name
self.expectations = expectations or []
def add_expectation(self, expectation_type, column=None, kwargs=None):
expectation = {"expectation_type": expectation_type, "column": column, "kwargs": kwargs or {}}
self.expectations.append(expectation)
return expectation
def to_dict(self):
return {
"suite_name": self.suite_name,
"expectations": self.expectations
}
if __name__ == "__main__":
suite = GreatExpectationsSuite("my_mock_suite")
suite.add_expectation("expect_column_values_to_not_be_null", column="user_id")
suite.add_expectation("expect_column_values_to_be_between", column="age",
kwargs={"min_value": 18, "max_value": 99})
suite.add_expectation("expect_column_values_to_match_regex", column="email",
kwargs={"regex": r"^[\w\.-]+@[\w\.-]+\.\w+$"})
print(json.dumps(suite.to_dict(), indent=2))
Output
{
"suite_name": "my_mock_suite",
"expectations": [
{
"expectation_type": "expect_column_values_to_not_be_null",
"column": "user_id",
"kwargs": {}
},
{
"expectation_type": "expect_column_values_to_be_between",
"column": "age",
"kwargs": {
"min_value": 18,
"max_value": 99
}
},
{
"expectation_type": "expect_column_values_to_match_regex",
"column": "email",
"kwargs": {
"regex": "^[\w\.-]+@[\w\.-]+\.\w+$"
}
}
]
}
How it works
This code defines a GreatExpectationsSuite class that keeps a list of expectations, each with an expectation type, column, and optional keyword arguments. The add_expectation method appends a dict representation to the list and returns it, allowing chained or inspected creation. to_dict serializes the suite state, which is then printed with json.dumps(indent=2) for readable output. This approach mirrors the data shape of real Great Expectations suites so downstream tooling can consume the structure without the full library dependency.
Common mistakes
- Mutating the default `expectations or []` incorrectly when reusing the same list object across instances
- Forgetting that `kwargs` is stored as a separate dict and unintentionally sharing references across expectations
- Not accounting for optional columns in expectations that apply to the whole table, not a single column
Variations
- Use a dataclass with `field(default_factory=list)` to avoid mutable default issues
- Store expectations in a `collections.OrderedDict` keyed by tuple (type, column) to prevent duplicates
Real-world use cases
- Mocking a Great Expectations suite in unit tests without installing the heavy library.
- Generating portable validation specs that can be translated to SQL or pandas checks in a pipeline.
- Creating lightweight documentation of data contracts to share with upstream data producers.
Sponsored
More from ML engineering pipelines
- Bayesian Optimization in Python: A Simplified Mock Implementation medium
- Build a Data Helper Class in Python for ML Pipelines easy
- Build a Mock Random Forest Classifier in Python easy
- Champion Challenger Deployment Mock in Python easy
- Compare Model A vs Model B Metrics in Python easy
- Detect Concept Drift in Python with a Simple Statistical Test medium
Keep learning
Related tutorials and quizzes for this topic.