# pysparky_data_quality.expectations

*module*

Explicit dataset and top-level schema expectations for PySpark.

[View source](https://github.com/PySparky/pysparky-data-quality/blob/46facb9e156e26dfd76fa534c81f75749c7879d8/src/pysparky_data_quality/expectations.py#L1-L1)

## pysparky_data_quality.expectations.Cardinality

*attribute* · *module attribute*

```python
Cardinality = Literal['many_to_one', 'one_to_many', 'one_to_one']
```

[View source](https://github.com/PySparky/pysparky-data-quality/blob/46facb9e156e26dfd76fa534c81f75749c7879d8/src/pysparky_data_quality/expectations.py#L16-L16)

## pysparky_data_quality.expectations.NullPolicy

*attribute* · *module attribute*

```python
NullPolicy = Literal['fail', 'equal', 'distinct']
```

[View source](https://github.com/PySparky/pysparky-data-quality/blob/46facb9e156e26dfd76fa534c81f75749c7879d8/src/pysparky_data_quality/expectations.py#L15-L15)

## pysparky_data_quality.expectations.ExpectationDefinitionError

*class*

```python
class ExpectationDefinitionError(ValueError)
```

Bases: `ValueError`

An expectation or its DataFrame inputs are invalid or ambiguous.

[View source](https://github.com/PySparky/pysparky-data-quality/blob/46facb9e156e26dfd76fa534c81f75749c7879d8/src/pysparky_data_quality/expectations.py#L20-L21)

## pysparky_data_quality.expectations.ExpectationEvaluationError

*class*

```python
class ExpectationEvaluationError(RuntimeError)
```

Bases: `RuntimeError`

PySpark failed while evaluating a dataset or schema expectation.

[View source](https://github.com/PySparky/pysparky-data-quality/blob/46facb9e156e26dfd76fa534c81f75749c7879d8/src/pysparky_data_quality/expectations.py#L24-L25)

## pysparky_data_quality.expectations.ExpectationOutcome

*class* · *dataclass*

```python
class ExpectationOutcome
```

Immutable result from one explicit expectation evaluation.

Metrics are stored as an ordered tuple of key/value pairs so the outcome
remains immutable and straightforward to serialize in a later report.

[View source](https://github.com/PySparky/pysparky-data-quality/blob/46facb9e156e26dfd76fa534c81f75749c7879d8/src/pysparky_data_quality/expectations.py#L28-L38)

### pysparky_data_quality.expectations.ExpectationOutcome.metrics

*attribute* · *class attribute* · *instance attribute*

```python
metrics: tuple[tuple[str, MetricValue], ...] = ()
```

[View source](https://github.com/PySparky/pysparky-data-quality/blob/46facb9e156e26dfd76fa534c81f75749c7879d8/src/pysparky_data_quality/expectations.py#L37-L37)

### pysparky_data_quality.expectations.ExpectationOutcome.passed

*attribute* · *instance attribute*

```python
passed: bool
```

[View source](https://github.com/PySparky/pysparky-data-quality/blob/46facb9e156e26dfd76fa534c81f75749c7879d8/src/pysparky_data_quality/expectations.py#L36-L36)

### pysparky_data_quality.expectations.ExpectationOutcome.violations

*attribute* · *class attribute* · *instance attribute*

```python
violations: tuple[str, ...] = ()
```

[View source](https://github.com/PySparky/pysparky-data-quality/blob/46facb9e156e26dfd76fa534c81f75749c7879d8/src/pysparky_data_quality/expectations.py#L38-L38)

## pysparky_data_quality.expectations.cardinality

*function*

```python
def cardinality(dataframe: DataFrame, source: str | Iterable[str], target: str | Iterable[str], *, relationship: Cardinality = 'many_to_one', null_policy: NullPolicy = 'equal') -> ExpectationOutcome
```

Check key-to-key cardinality within one DataFrame.

``many_to_one`` requires each source key to map to at most one target key;
``one_to_many`` reverses that constraint; ``one_to_one`` requires both.
Exact duplicate source/target pairs do not violate a relationship. Calling
this function is an explicit Spark action boundary.

[View source](https://github.com/PySparky/pysparky-data-quality/blob/46facb9e156e26dfd76fa534c81f75749c7879d8/src/pysparky_data_quality/expectations.py#L142-L252)

## pysparky_data_quality.expectations.row_count

*function*

```python
def row_count(dataframe: DataFrame, *, min_rows: int | None = None, max_rows: int | None = None) -> ExpectationOutcome
```

Check inclusive row-count bounds with one aggregate action.

[View source](https://github.com/PySparky/pysparky-data-quality/blob/46facb9e156e26dfd76fa534c81f75749c7879d8/src/pysparky_data_quality/expectations.py#L110-L139)

## pysparky_data_quality.expectations.schema_matches

*function*

```python
def schema_matches(dataframe: DataFrame, *, required_columns: str | Iterable[str] = (), forbidden_columns: str | Iterable[str] = (), data_types: Mapping[str, str | DataType] | None = None, nullable: Mapping[str, bool] | None = None) -> ExpectationOutcome
```

Compare top-level schema metadata without scanning DataFrame rows.

String types are compared to the canonical result of PySpark's
``DataType.typeName()``. Nested type matching is not supported; expected
type strings for arrays, maps, structs, and parameterized nested types are
rejected.

[View source](https://github.com/PySparky/pysparky-data-quality/blob/46facb9e156e26dfd76fa534c81f75749c7879d8/src/pysparky_data_quality/expectations.py#L255-L415)

## pysparky_data_quality.expectations.unique

*function*

```python
def unique(dataframe: DataFrame, columns: str | Iterable[str], *, null_policy: NullPolicy = 'equal') -> ExpectationOutcome
```

Check uniqueness of one or more key columns with one aggregate action.

``fail`` rejects any row with a NULL key and also checks duplicate groups;
``equal`` treats NULL values as equal when identifying duplicate groups;
``distinct`` excludes rows with any NULL key from duplicate detection.
Calling this function is an explicit Spark action boundary.

[View source](https://github.com/PySparky/pysparky-data-quality/blob/46facb9e156e26dfd76fa534c81f75749c7879d8/src/pysparky_data_quality/expectations.py#L41-L107)
