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

Function test_to_dict

tests/test_models.py:541–570  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

539
540
541def test_to_dict() -> None:
542 class Model(BaseModel):
543 foo: Optional[str] = Field(alias="FOO", default=None)
544
545 m = Model(FOO="hello")
546 assert m.to_dict() == {"FOO": "hello"}
547 assert m.to_dict(use_api_names=False) == {"foo": "hello"}
548
549 m2 = Model()
550 assert m2.to_dict() == {}
551 assert m2.to_dict(exclude_unset=False) == {"FOO": None}
552 assert m2.to_dict(exclude_unset=False, exclude_none=True) == {}
553 assert m2.to_dict(exclude_unset=False, exclude_defaults=True) == {}
554
555 m3 = Model(FOO=None)
556 assert m3.to_dict() == {"FOO": None}
557 assert m3.to_dict(exclude_none=True) == {}
558 assert m3.to_dict(exclude_defaults=True) == {}
559
560 class Model2(BaseModel):
561 created_at: datetime
562
563 time_str = "2024-03-21T11:39:01.275859"
564 m4 = Model2.construct(created_at=time_str)
565 assert m4.to_dict(mode="python") == {"created_at": datetime.fromisoformat(time_str)}
566 assert m4.to_dict(mode="json") == {"created_at": time_str}
567
568 if PYDANTIC_V1:
569 with pytest.raises(ValueError, match="warnings is only supported in Pydantic v2"):
570 m.to_dict(warnings=False)
571
572
573def 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