MCPcopy
hub / github.com/pydantic/pydantic / test_json_encoder_str

Function test_json_encoder_str

tests/test_forward_ref.py:586–617  ·  view source on GitHub ↗
(create_module)

Source from the content-addressed store, hash-verified

584
585
586def test_json_encoder_str(create_module):
587 module = create_module(
588 # language=Python
589 """
590from pydantic import BaseModel, ConfigDict, field_serializer
591
592
593class User(BaseModel):
594 x: str
595
596
597FooUser = User
598
599
600class User(BaseModel):
601 y: str
602
603
604class Model(BaseModel):
605 foo_user: FooUser
606 user: User
607
608 @field_serializer('user')
609 def serialize_user(self, v):
610 return f'User({v.y})'
611
612"""
613 )
614
615 m = module.Model(foo_user={'x': 'user1'}, user={'y': 'user2'})
616 # TODO: How can we replicate this custom-encoder functionality without affecting the serialization of `User`?
617 assert m.model_dump_json() == '{"foo_user":{"x":"user1"},"user":"User(user2)"}'
618
619
620def test_pep585_self_referencing_generics(create_module):

Callers

nothing calls this directly

Calls 2

create_moduleFunction · 0.85
model_dump_jsonMethod · 0.80

Tested by

no test coverage detected