|
1 | 1 | import re |
2 | 2 | import warnings |
3 | 3 | from collections.abc import Sequence |
4 | | -from copy import copy, deepcopy |
| 4 | +from copy import copy |
5 | 5 | from dataclasses import dataclass, is_dataclass |
6 | 6 | from enum import Enum |
7 | 7 | from functools import lru_cache |
@@ -169,11 +169,11 @@ def validate( |
169 | 169 | values: dict[str, Any] = {}, # noqa: B006 |
170 | 170 | *, |
171 | 171 | loc: tuple[Union[int, str], ...] = (), |
172 | | - ) -> tuple[Any, Union[list[dict[str, Any]], None]]: |
| 172 | + ) -> tuple[Any, list[dict[str, Any]]]: |
173 | 173 | try: |
174 | 174 | return ( |
175 | 175 | self._type_adapter.validate_python(value, from_attributes=True), |
176 | | - None, |
| 176 | + [], |
177 | 177 | ) |
178 | 178 | except ValidationError as exc: |
179 | 179 | return None, _regenerate_error_with_loc( |
@@ -305,94 +305,12 @@ def get_definitions( |
305 | 305 | if "description" in item_def: |
306 | 306 | item_description = cast(str, item_def["description"]).split("\f")[0] |
307 | 307 | item_def["description"] = item_description |
308 | | - new_mapping, new_definitions = _remap_definitions_and_field_mappings( |
309 | | - model_name_map=model_name_map, |
310 | | - definitions=definitions, # type: ignore[arg-type] |
311 | | - field_mapping=field_mapping, |
312 | | - ) |
313 | | - return new_mapping, new_definitions |
314 | | - |
315 | | - |
316 | | -def _replace_refs( |
317 | | - *, |
318 | | - schema: dict[str, Any], |
319 | | - old_name_to_new_name_map: dict[str, str], |
320 | | -) -> dict[str, Any]: |
321 | | - new_schema = deepcopy(schema) |
322 | | - for key, value in new_schema.items(): |
323 | | - if key == "$ref": |
324 | | - value = schema["$ref"] |
325 | | - if isinstance(value, str): |
326 | | - ref_name = schema["$ref"].split("/")[-1] |
327 | | - if ref_name in old_name_to_new_name_map: |
328 | | - new_name = old_name_to_new_name_map[ref_name] |
329 | | - new_schema["$ref"] = REF_TEMPLATE.format(model=new_name) |
330 | | - continue |
331 | | - if isinstance(value, dict): |
332 | | - new_schema[key] = _replace_refs( |
333 | | - schema=value, |
334 | | - old_name_to_new_name_map=old_name_to_new_name_map, |
335 | | - ) |
336 | | - elif isinstance(value, list): |
337 | | - new_value = [] |
338 | | - for item in value: |
339 | | - if isinstance(item, dict): |
340 | | - new_item = _replace_refs( |
341 | | - schema=item, |
342 | | - old_name_to_new_name_map=old_name_to_new_name_map, |
343 | | - ) |
344 | | - new_value.append(new_item) |
345 | | - |
346 | | - else: |
347 | | - new_value.append(item) |
348 | | - new_schema[key] = new_value |
349 | | - return new_schema |
350 | | - |
351 | | - |
352 | | -def _remap_definitions_and_field_mappings( |
353 | | - *, |
354 | | - model_name_map: ModelNameMap, |
355 | | - definitions: dict[str, Any], |
356 | | - field_mapping: dict[ |
357 | | - tuple[ModelField, Literal["validation", "serialization"]], JsonSchemaValue |
358 | | - ], |
359 | | -) -> tuple[ |
360 | | - dict[tuple[ModelField, Literal["validation", "serialization"]], JsonSchemaValue], |
361 | | - dict[str, Any], |
362 | | -]: |
363 | | - old_name_to_new_name_map = {} |
364 | | - for field_key, schema in field_mapping.items(): |
365 | | - model = field_key[0].type_ |
366 | | - if model not in model_name_map or "$ref" not in schema: |
367 | | - continue |
368 | | - new_name = model_name_map[model] |
369 | | - old_name = schema["$ref"].split("/")[-1] |
370 | | - if old_name in {f"{new_name}-Input", f"{new_name}-Output"}: |
371 | | - continue |
372 | | - old_name_to_new_name_map[old_name] = new_name |
373 | | - |
374 | | - new_field_mapping: dict[ |
375 | | - tuple[ModelField, Literal["validation", "serialization"]], JsonSchemaValue |
376 | | - ] = {} |
377 | | - for field_key, schema in field_mapping.items(): |
378 | | - new_schema = _replace_refs( |
379 | | - schema=schema, |
380 | | - old_name_to_new_name_map=old_name_to_new_name_map, |
381 | | - ) |
382 | | - new_field_mapping[field_key] = new_schema |
383 | | - |
384 | | - new_definitions = {} |
385 | | - for key, value in definitions.items(): |
386 | | - if key in old_name_to_new_name_map: |
387 | | - new_key = old_name_to_new_name_map[key] |
388 | | - else: |
389 | | - new_key = key |
390 | | - new_value = _replace_refs( |
391 | | - schema=value, |
392 | | - old_name_to_new_name_map=old_name_to_new_name_map, |
393 | | - ) |
394 | | - new_definitions[new_key] = new_value |
395 | | - return new_field_mapping, new_definitions |
| 308 | + # definitions: dict[DefsRef, dict[str, Any]] |
| 309 | + # but mypy complains about general str in other places that are not declared as |
| 310 | + # DefsRef, although DefsRef is just str: |
| 311 | + # DefsRef = NewType('DefsRef', str) |
| 312 | + # So, a cast to simplify the types here |
| 313 | + return field_mapping, cast(dict[str, dict[str, Any]], definitions) |
396 | 314 |
|
397 | 315 |
|
398 | 316 | def is_scalar_field(field: ModelField) -> bool: |
@@ -441,7 +359,7 @@ def serialize_sequence_value(*, field: ModelField, value: Any) -> Sequence[Any]: |
441 | 359 | return shared.sequence_annotation_to_type[origin_type](value) # type: ignore[no-any-return,index] |
442 | 360 |
|
443 | 361 |
|
444 | | -def get_missing_field_error(loc: tuple[str, ...]) -> dict[str, Any]: |
| 362 | +def get_missing_field_error(loc: tuple[Union[int, str], ...]) -> dict[str, Any]: |
445 | 363 | error = ValidationError.from_exception_data( |
446 | 364 | "Field required", [{"type": "missing", "loc": loc, "input": {}}] |
447 | 365 | ).errors(include_url=False)[0] |
@@ -499,11 +417,6 @@ def get_model_name_map(unique_models: TypeModelSet) -> dict[TypeModelOrEnum, str |
499 | 417 | return {v: k for k, v in name_model_map.items()} |
500 | 418 |
|
501 | 419 |
|
502 | | -def get_compat_model_name_map(fields: list[ModelField]) -> ModelNameMap: |
503 | | - flat_models = get_flat_models_from_fields(fields, known_models=set()) |
504 | | - return get_model_name_map(flat_models) |
505 | | - |
506 | | - |
507 | 420 | def get_flat_models_from_model( |
508 | 421 | model: type["BaseModel"], known_models: Union[TypeModelSet, None] = None |
509 | 422 | ) -> TypeModelSet: |
|
0 commit comments