MCPcopy
hub / github.com/pydantic/pydantic / apply_known_metadata

Function apply_known_metadata

pydantic/_internal/_known_annotated_metadata.py:169–339  ·  view source on GitHub ↗

Apply `annotation` to `schema` if it is an annotation we know about (Gt, Le, etc.). Otherwise return `None`. This does not handle all known annotations. If / when it does, it can always return a CoreSchema and return the unmodified schema if the annotation should be ignored. Assume

(annotation: Any, schema: CoreSchema)

Source from the content-addressed store, hash-verified

167
168
169def apply_known_metadata(annotation: Any, schema: CoreSchema) -> CoreSchema | None: # noqa: C901
170 """Apply `annotation` to `schema` if it is an annotation we know about (Gt, Le, etc.).
171 Otherwise return `None`.
172
173 This does not handle all known annotations. If / when it does, it can always
174 return a CoreSchema and return the unmodified schema if the annotation should be ignored.
175
176 Assumes that GroupedMetadata has already been expanded via `expand_grouped_metadata`.
177
178 Args:
179 annotation: The annotation.
180 schema: The schema.
181
182 Returns:
183 An updated schema with annotation if it is an annotation we know about, `None` otherwise.
184
185 Raises:
186 RuntimeError: If a constraint can't be applied to a specific schema type.
187 ValueError: If an unknown constraint is encountered.
188 """
189 import annotated_types as at
190
191 from ._validators import NUMERIC_VALIDATOR_LOOKUP, forbid_inf_nan_check
192
193 schema = schema.copy()
194 schema_update, other_metadata = collect_known_metadata([annotation])
195 schema_type = schema['type']
196
197 chain_schema_constraints: set[str] = {
198 'pattern',
199 'strip_whitespace',
200 'to_lower',
201 'to_upper',
202 'coerce_numbers_to_str',
203 'ascii_only',
204 }
205 chain_schema_steps: list[CoreSchema] = []
206
207 for constraint, value in schema_update.items():
208 if constraint not in CONSTRAINTS_TO_ALLOWED_SCHEMAS:
209 raise ValueError(f'Unknown constraint {constraint}')
210 allowed_schemas = CONSTRAINTS_TO_ALLOWED_SCHEMAS[constraint]
211
212 # if it becomes necessary to handle more than one constraint
213 # in this recursive case with function-after or function-wrap, we should refactor
214 # this is a bit challenging because we sometimes want to apply constraints to the inner schema,
215 # whereas other times we want to wrap the existing schema with a new one that enforces a new constraint.
216 if schema_type in {'function-before', 'function-wrap', 'function-after'} and constraint == 'strict':
217 schema['schema'] = apply_known_metadata(annotation, schema['schema']) # type: ignore # schema is function schema
218 return schema
219
220 # if we're allowed to apply constraint directly to the schema, like le to int, do that
221 if schema_type in allowed_schemas:
222 if constraint == 'union_mode' and schema_type == 'union':
223 schema['mode'] = value # type: ignore # schema is UnionSchema
224 else:
225 schema[constraint] = value
226 continue

Callers

nothing calls this directly

Calls 8

collect_known_metadataFunction · 0.85
as_jsonable_valueFunction · 0.85
str_schemaMethod · 0.80
chain_schemaMethod · 0.80
copyMethod · 0.45
itemsMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected