MCPcopy
hub / github.com/pydantic/pydantic / generate_inner

Method generate_inner

pydantic/json_schema.py:450–588  ·  view source on GitHub ↗

Generates a JSON schema for a given core schema. Args: schema: The given core schema. Returns: The generated JSON schema. TODO: the nested function definitions here seem like bad practice, I'd like to unpack these in a future PR. It'd be gre

(self, schema: CoreSchemaOrField)

Source from the content-addressed store, hash-verified

448 return self.sort(json_schema)
449
450 def generate_inner(self, schema: CoreSchemaOrField) -> JsonSchemaValue: # noqa: C901
451 """Generates a JSON schema for a given core schema.
452
453 Args:
454 schema: The given core schema.
455
456 Returns:
457 The generated JSON schema.
458
459 TODO: the nested function definitions here seem like bad practice, I'd like to unpack these
460 in a future PR. It'd be great if we could shorten the call stack a bit for JSON schema generation,
461 and I think there's potential for that here.
462 """
463 # If a schema with the same CoreRef has been handled, just return a reference to it
464 # Note that this assumes that it will _never_ be the case that the same CoreRef is used
465 # on types that should have different JSON schemas
466 if 'ref' in schema:
467 core_ref = CoreRef(schema['ref']) # type: ignore[typeddict-item]
468 core_mode_ref = (core_ref, self.mode)
469 if core_mode_ref in self.core_to_defs_refs and self.core_to_defs_refs[core_mode_ref] in self.definitions:
470 return {'$ref': self.core_to_json_refs[core_mode_ref]}
471
472 def populate_defs(core_schema: CoreSchema, json_schema: JsonSchemaValue) -> JsonSchemaValue:
473 if 'ref' in core_schema:
474 core_ref = CoreRef(core_schema['ref']) # type: ignore[typeddict-item]
475 defs_ref, ref_json_schema = self.get_cache_defs_ref_schema(core_ref)
476 json_ref = JsonRef(ref_json_schema['$ref'])
477 # Replace the schema if it's not a reference to itself
478 # What we want to avoid is having the def be just a ref to itself
479 # which is what would happen if we blindly assigned any
480 if json_schema.get('$ref', None) != json_ref:
481 self.definitions[defs_ref] = json_schema
482 self._core_defs_invalid_for_json_schema.pop(defs_ref, None)
483 json_schema = ref_json_schema
484 return json_schema
485
486 def handler_func(schema_or_field: CoreSchemaOrField) -> JsonSchemaValue:
487 """Generate a JSON schema based on the input schema.
488
489 Args:
490 schema_or_field: The core schema to generate a JSON schema from.
491
492 Returns:
493 The generated JSON schema.
494
495 Raises:
496 TypeError: If an unexpected schema type is encountered.
497 """
498 # Generate the core-schema-type-specific bits of the schema generation:
499 json_schema: JsonSchemaValue | None = None
500 if self.mode == 'serialization' and 'serialization' in schema_or_field:
501 # In this case, we skip the JSON Schema generation of the schema
502 # and use the `'serialization'` schema instead (canonical example:
503 # `Annotated[int, PlainSerializer(str)]`).
504 ser_schema = schema_or_field['serialization'] # type: ignore
505 json_schema = self.ser_schema(ser_schema)
506
507 # It might be that the 'serialization'` is skipped depending on `when_used`.

Callers 15

generate_definitionsMethod · 0.95
generateMethod · 0.95
list_schemaMethod · 0.95
tuple_schemaMethod · 0.95
_common_set_schemaMethod · 0.95
generator_schemaMethod · 0.95
dict_schemaMethod · 0.95
function_after_schemaMethod · 0.95
function_plain_schemaMethod · 0.95
function_wrap_schemaMethod · 0.95
default_schemaMethod · 0.95

Calls 1

getMethod · 0.45

Tested by

no test coverage detected