Raise an error if from_attributes is not enabled.
(ctx: MethodContext)
| 257 | |
| 258 | |
| 259 | def 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 | |
| 285 | class PydanticModelField: |
nothing calls this directly
no test coverage detected