MCPcopy
hub / github.com/pydantic/pydantic / from_attributes_callback

Function from_attributes_callback

pydantic/mypy.py:259–282  ·  view source on GitHub ↗

Raise an error if from_attributes is not enabled.

(ctx: MethodContext)

Source from the content-addressed store, hash-verified

257
258
259def from_attributes_callback(ctx: MethodContext) -> Type:
260 """Raise an error if from_attributes is not enabled."""
261 model_type: Instance
262 ctx_type = ctx.type
263 if isinstance(ctx_type, TypeType):
264 ctx_type = ctx_type.item
265 if isinstance(ctx_type, CallableType) and isinstance(ctx_type.ret_type, Instance):
266 model_type = ctx_type.ret_type # called on the class
267 elif isinstance(ctx_type, Instance):
268 model_type = ctx_type # called on an instance (unusual, but still valid)
269 else: # pragma: no cover
270 detail = f'ctx.type: {ctx_type} (of type {ctx_type.__class__.__name__})'
271 error_unexpected_behavior(detail, ctx.api, ctx.context)
272 return ctx.default_return_type
273 pydantic_metadata = model_type.type.metadata.get(METADATA_KEY)
274 if pydantic_metadata is None:
275 return ctx.default_return_type
276 if not model_type.type.has_base(BASEMODEL_FULLNAME):
277 # not a Pydantic v2 model
278 return ctx.default_return_type
279 from_attributes = pydantic_metadata.get('config', {}).get('from_attributes')
280 if from_attributes is not True:
281 error_from_attributes(model_type.type.name, ctx.api, ctx.context)
282 return ctx.default_return_type
283
284
285class PydanticModelField:

Callers

nothing calls this directly

Calls 3

error_from_attributesFunction · 0.85
getMethod · 0.45

Tested by

no test coverage detected