|
34 | 34 | ConfigDict, |
35 | 35 | InstanceOf, |
36 | 36 | PlainSerializer, |
| 37 | + PydanticSchemaGenerationError, |
| 38 | + PydanticUserError, |
37 | 39 | TypeAdapter, |
38 | 40 | ValidationError, |
39 | 41 | model_validator, |
@@ -142,59 +144,40 @@ def test_type_adapter_standalone() -> None: |
142 | 144 | # --- the road not taken: native dataclass introspection ---------------------- |
143 | 145 |
|
144 | 146 |
|
145 | | -def test_native_dataclass_introspection_is_possible_but_diverges() -> None: |
146 | | - """Pydantic CAN introspect the model dataclass after a namespace rebuild, |
147 | | - but that path validates the model shape, not the document: it rejects the |
148 | | - document form, coerces booleans into dimensions, and skips the library's |
149 | | - cross-field checks. This test documents why the delegation pattern above |
150 | | - is the recommended integration.""" |
| 147 | +def test_native_dataclass_introspection_is_not_supported() -> None: |
| 148 | + """Pydantic cannot field-introspect the model dataclasses: the UNSET |
| 149 | + sentinel (PEP 661, typing_extensions.Sentinel) in the optional-field |
| 150 | + annotations has no pydantic schema (as of pydantic 2.13), so even the |
| 151 | + rebuild-with-namespace recipe fails. Introspection was already the wrong |
| 152 | + tool before the sentinel existed — it validated the model shape rather |
| 153 | + than the document, and its lax coercion re-opened validator holes (e.g. |
| 154 | + shape=[True, -5] coerced to (1, -5)) — so the delegation patterns above |
| 155 | + are the only supported integrations. If this test ever fails because |
| 156 | + pydantic learned to handle sentinels, revisit whether the introspection |
| 157 | + path needs its divergences documented again.""" |
151 | 158 | from zarr_metadata._common import JSONValue |
152 | | - from zarr_metadata.model import UNSET, UnsetType |
| 159 | + from zarr_metadata.model import UNSET |
153 | 160 | from zarr_metadata.v3._common import MetadataV3 |
154 | 161 | from zarr_metadata.v3.array import ArrayMetadataV3, ExtensionFieldV3 |
155 | 162 |
|
156 | | - adapter = TypeAdapter(ArrayMetadataModelV3) |
157 | | - adapter.rebuild( |
158 | | - force=True, |
159 | | - _types_namespace={ |
160 | | - "JSONValue": JSONValue, |
161 | | - "ExtensionFieldV3": ExtensionFieldV3, |
162 | | - "MetadataV3": MetadataV3, |
163 | | - "ArrayMetadataV3": ArrayMetadataV3, |
164 | | - "NamedConfigModelV3": NamedConfigModelV3, |
165 | | - "MetadataFieldModelV3": NamedConfigModelV3, |
166 | | - "UnsetType": UnsetType, |
167 | | - }, |
168 | | - ) |
169 | | - model_shaped = { |
170 | | - "shape": [10], |
171 | | - "fill_value": 0, |
172 | | - "data_type": {"name": "uint8", "configuration": {}}, |
173 | | - "chunk_grid": {"name": "regular", "configuration": {"chunk_shape": [5]}}, |
174 | | - "codecs": [{"name": "bytes", "configuration": {}}], |
175 | | - "chunk_key_encoding": {"name": "default", "configuration": {}}, |
176 | | - "dimension_names": UNSET, |
177 | | - "attributes": {}, |
178 | | - "storage_transformers": [], |
179 | | - "extra_fields": {}, |
180 | | - } |
181 | | - # model-shaped data validates, nested named configs and all |
182 | | - model = adapter.validate_python(model_shaped) |
183 | | - assert isinstance(model.data_type, NamedConfigModelV3) |
184 | | - |
185 | | - # divergence 1: the DOCUMENT form is rejected — no from_json normalization |
186 | | - with pytest.raises(ValidationError): |
187 | | - adapter.validate_python(model_shaped | {"data_type": "uint8"}) |
188 | | - |
189 | | - # divergence 2: lax coercion re-opens holes the library validators close |
190 | | - coerced = adapter.validate_python(model_shaped | {"shape": [True, -5]}) |
191 | | - assert coerced.shape == (1, -5) # from_json would reject both entries |
192 | | - |
193 | | - # __post_init__ invariants DO still run under pydantic construction |
194 | | - with pytest.raises(ValidationError, match="Extra fields"): |
195 | | - adapter.validate_python( |
196 | | - model_shaped | {"extra_fields": {"shape": {"must_understand": False}}} |
| 163 | + def build_and_use() -> None: |
| 164 | + adapter = TypeAdapter(ArrayMetadataModelV3) |
| 165 | + adapter.rebuild( |
| 166 | + force=True, |
| 167 | + _types_namespace={ |
| 168 | + "JSONValue": JSONValue, |
| 169 | + "ExtensionFieldV3": ExtensionFieldV3, |
| 170 | + "MetadataV3": MetadataV3, |
| 171 | + "ArrayMetadataV3": ArrayMetadataV3, |
| 172 | + "NamedConfigModelV3": NamedConfigModelV3, |
| 173 | + "MetadataFieldModelV3": NamedConfigModelV3, |
| 174 | + "UNSET": UNSET, |
| 175 | + }, |
197 | 176 | ) |
| 177 | + adapter.validate_python({}) |
| 178 | + |
| 179 | + with pytest.raises((AttributeError, PydanticSchemaGenerationError, PydanticUserError)): |
| 180 | + build_and_use() |
198 | 181 |
|
199 | 182 |
|
200 | 183 | # --- a first-class pydantic model, engine-backed (the pydantic-zarr pattern) -- |
|
0 commit comments