MCPcopy Create free account
hub / github.com/anthropics/anthropic-sdk-python / test_to_dict

Function test_to_dict

tests/test_models.py:522–551  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

520
521
522def test_to_dict() -> None:
523 class Model(BaseModel):
524 foo: Optional[str] = Field(alias="FOO", default=None)
525
526 m = Model(FOO="hello")
527 assert m.to_dict() == {"FOO": "hello"}
528 assert m.to_dict(use_api_names=False) == {"foo": "hello"}
529
530 m2 = Model()
531 assert m2.to_dict() == {}
532 assert m2.to_dict(exclude_unset=False) == {"FOO": None}
533 assert m2.to_dict(exclude_unset=False, exclude_none=True) == {}
534 assert m2.to_dict(exclude_unset=False, exclude_defaults=True) == {}
535
536 m3 = Model(FOO=None)
537 assert m3.to_dict() == {"FOO": None}
538 assert m3.to_dict(exclude_none=True) == {}
539 assert m3.to_dict(exclude_defaults=True) == {}
540
541 class Model2(BaseModel):
542 created_at: datetime
543
544 time_str = "2024-03-21T11:39:01.275859"
545 m4 = Model2.construct(created_at=time_str)
546 assert m4.to_dict(mode="python") == {"created_at": datetime.fromisoformat(time_str)}
547 assert m4.to_dict(mode="json") == {"created_at": time_str}
548
549 if PYDANTIC_V1:
550 with pytest.raises(ValueError, match="warnings is only supported in Pydantic v2"):
551 m.to_dict(warnings=False)
552
553
554def test_forwards_compat_model_dump_method() -> None:

Callers

nothing calls this directly

Calls 3

ModelClass · 0.70
to_dictMethod · 0.45
constructMethod · 0.45

Tested by

no test coverage detected