MCPcopy
hub / github.com/pydantic/pydantic / sort

Method sort

pydantic/json_schema.py:590–602  ·  view source on GitHub ↗

Override this method to customize the sorting of the JSON schema (e.g., don't sort at all, sort all keys unconditionally, etc.) By default, alphabetically sort the keys in the JSON schema, skipping the 'properties' and 'default' keys to preserve field definition order. This sort is

(self, value: JsonSchemaValue, parent_key: str | None = None)

Source from the content-addressed store, hash-verified

588 return json_schema
589
590 def sort(self, value: JsonSchemaValue, parent_key: str | None = None) -> JsonSchemaValue:
591 """Override this method to customize the sorting of the JSON schema (e.g., don't sort at all, sort all keys unconditionally, etc.)
592
593 By default, alphabetically sort the keys in the JSON schema, skipping the 'properties' and 'default' keys to preserve field definition order.
594 This sort is recursive, so it will sort all nested dictionaries as well.
595 """
596 sorted_dict: dict[str, JsonSchemaValue] = {}
597 keys = value.keys()
598 if parent_key not in ('properties', 'default'):
599 keys = sorted(keys)
600 for key in keys:
601 sorted_dict[key] = self._sort_recursive(value[key], parent_key=key)
602 return sorted_dict
603
604 def _sort_recursive(self, value: Any, parent_key: str | None = None) -> Any:
605 """Recursively sort a JSON schema value."""

Callers 4

generate_definitionsMethod · 0.95
generateMethod · 0.95
using_update.pyFile · 0.80

Calls 2

_sort_recursiveMethod · 0.95
keysMethod · 0.80

Tested by 1