MCPcopy
hub / github.com/pydantic/pydantic / _apply_annotations

Method _apply_annotations

pydantic/_internal/_generate_schema.py:2210–2256  ·  view source on GitHub ↗

Apply arguments from `Annotated` or from `FieldInfo` to a schema. This gets called by `GenerateSchema._annotated_schema` but differs from it in that it does not expect `source_type` to be an `Annotated` object, it expects it to be the first argument of that (in other words,

(
        self,
        source_type: Any,
        annotations: list[Any],
        transform_inner_schema: Callable[[CoreSchema], CoreSchema] = lambda x: x,
        check_unsupported_field_info_attributes: bool = True,
    )

Source from the content-addressed store, hash-verified

2208 return schema
2209
2210 def _apply_annotations(
2211 self,
2212 source_type: Any,
2213 annotations: list[Any],
2214 transform_inner_schema: Callable[[CoreSchema], CoreSchema] = lambda x: x,
2215 check_unsupported_field_info_attributes: bool = True,
2216 ) -> CoreSchema:
2217 """Apply arguments from `Annotated` or from `FieldInfo` to a schema.
2218
2219 This gets called by `GenerateSchema._annotated_schema` but differs from it in that it does
2220 not expect `source_type` to be an `Annotated` object, it expects it to be the first argument of that
2221 (in other words, `GenerateSchema._annotated_schema` just unpacks `Annotated`, this process it).
2222 """
2223 annotations = list(_known_annotated_metadata.expand_grouped_metadata(annotations))
2224
2225 pydantic_js_annotation_functions: list[GetJsonSchemaFunction] = []
2226
2227 def inner_handler(obj: Any) -> CoreSchema:
2228 schema = self._generate_schema_from_get_schema_method(obj, source_type)
2229
2230 if schema is None:
2231 schema = self._generate_schema_inner(obj)
2232
2233 metadata_js_function = _extract_get_pydantic_json_schema(obj)
2234 if metadata_js_function is not None:
2235 metadata_schema = resolve_original_schema(schema, self.defs)
2236 if metadata_schema is not None:
2237 self._add_js_function(metadata_schema, metadata_js_function)
2238 return transform_inner_schema(schema)
2239
2240 get_inner_schema = CallbackGetCoreSchemaHandler(inner_handler, self)
2241
2242 for annotation in annotations:
2243 if annotation is None:
2244 continue
2245 get_inner_schema = self._get_wrapped_inner_schema(
2246 get_inner_schema,
2247 annotation,
2248 pydantic_js_annotation_functions,
2249 check_unsupported_field_info_attributes=check_unsupported_field_info_attributes,
2250 )
2251
2252 schema = get_inner_schema(source_type)
2253 if pydantic_js_annotation_functions:
2254 core_metadata = schema.setdefault('metadata', {})
2255 update_core_metadata(core_metadata, pydantic_js_annotation_functions=pydantic_js_annotation_functions)
2256 return _add_custom_serialization_from_json_encoders(self._config_wrapper.json_encoders, source_type, schema)
2257
2258 def _apply_single_annotation(
2259 self,

Callers 4

_common_field_schemaMethod · 0.95
_annotated_schemaMethod · 0.95

Tested by

no test coverage detected