|
| 1 | +"""Drift-prevention tests for Partial* TypedDict variants. |
| 2 | +
|
| 3 | +Each *Partial TypedDict in the package must declare the same fields |
| 4 | +(with the same annotations) and the same extra_items setting as its |
| 5 | +full counterpart. The only intentional difference is total=False |
| 6 | +(i.e. every field becomes NotRequired). This test enforces that |
| 7 | +invariant so adding a field to the full type without mirroring it |
| 8 | +on the partial fails CI. |
| 9 | +""" |
| 10 | + |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +from typing import Any |
| 14 | + |
| 15 | +import pytest |
| 16 | + |
| 17 | +from zarr_metadata.v2.array import ArrayMetadataV2, ArrayMetadataV2Partial |
| 18 | +from zarr_metadata.v2.group import GroupMetadataV2, GroupMetadataV2Partial |
| 19 | +from zarr_metadata.v3.array import ArrayMetadataV3, ArrayMetadataV3Partial |
| 20 | +from zarr_metadata.v3.group import GroupMetadataV3, GroupMetadataV3Partial |
| 21 | + |
| 22 | +# (full, partial) pairs to check. Add new pairs here as more are introduced. |
| 23 | +PAIRS: list[tuple[type, type]] = [ |
| 24 | + (ArrayMetadataV3, ArrayMetadataV3Partial), |
| 25 | + (GroupMetadataV3, GroupMetadataV3Partial), |
| 26 | + (ArrayMetadataV2, ArrayMetadataV2Partial), |
| 27 | + (GroupMetadataV2, GroupMetadataV2Partial), |
| 28 | +] |
| 29 | + |
| 30 | + |
| 31 | +@pytest.mark.parametrize(("full", "partial"), PAIRS, ids=lambda p: p.__name__) |
| 32 | +def test_partial_matches_full(full: Any, partial: Any) -> None: |
| 33 | + """Partial TypedDict has identical fields and extra_items, only total differs.""" |
| 34 | + assert full.__annotations__ == partial.__annotations__, ( |
| 35 | + f"{partial.__name__} fields drifted from {full.__name__}: " |
| 36 | + f"full={set(full.__annotations__)}, partial={set(partial.__annotations__)}" |
| 37 | + ) |
| 38 | + assert getattr(full, "__extra_items__", None) == getattr(partial, "__extra_items__", None), ( |
| 39 | + f"{partial.__name__} extra_items differs from {full.__name__}" |
| 40 | + ) |
| 41 | + assert full.__total__ is True, f"{full.__name__} must be declared with total=True (default)" |
| 42 | + assert partial.__total__ is False, f"{partial.__name__} must be declared with total=False" |
0 commit comments