()
| 49 | |
| 50 | |
| 51 | def test_from_attributes_root(): |
| 52 | class PokemonCls: |
| 53 | def __init__(self, *, en_name: str, jp_name: str): |
| 54 | self.en_name = en_name |
| 55 | self.jp_name = jp_name |
| 56 | |
| 57 | class Pokemon(BaseModel): |
| 58 | model_config = ConfigDict(from_attributes=True) |
| 59 | en_name: str |
| 60 | jp_name: str |
| 61 | |
| 62 | with pytest.warns( |
| 63 | PydanticDeprecatedSince20, match='Pydantic V1 style `@root_validator` validators are deprecated.' |
| 64 | ): |
| 65 | |
| 66 | class PokemonList(BaseModel): |
| 67 | root: list[Pokemon] |
| 68 | |
| 69 | @root_validator(pre=True) |
| 70 | @classmethod |
| 71 | def populate_root(cls, values): |
| 72 | return {'root': values} |
| 73 | |
| 74 | @model_serializer(mode='wrap') |
| 75 | def _serialize(self, handler, info): |
| 76 | data = handler(self) |
| 77 | if info.mode == 'json': |
| 78 | return data['root'] |
| 79 | else: |
| 80 | return data |
| 81 | |
| 82 | @classmethod |
| 83 | def model_modify_json_schema(cls, json_schema): |
| 84 | return json_schema['properties']['root'] |
| 85 | |
| 86 | model_config = ConfigDict(from_attributes=True) |
| 87 | |
| 88 | pika = PokemonCls(en_name='Pikachu', jp_name='ピカチュウ') |
| 89 | bulbi = PokemonCls(en_name='Bulbasaur', jp_name='フシギダネ') |
| 90 | |
| 91 | pokemons = deprecated_from_orm(PokemonList, [pika, bulbi]) |
| 92 | assert pokemons.root == [ |
| 93 | Pokemon(en_name='Pikachu', jp_name='ピカチュウ'), |
| 94 | Pokemon(en_name='Bulbasaur', jp_name='フシギダネ'), |
| 95 | ] |
| 96 | |
| 97 | with pytest.warns( |
| 98 | PydanticDeprecatedSince20, match='Pydantic V1 style `@root_validator` validators are deprecated.' |
| 99 | ): |
| 100 | |
| 101 | class PokemonDict(BaseModel): |
| 102 | root: dict[str, Pokemon] |
| 103 | model_config = ConfigDict(from_attributes=True) |
| 104 | |
| 105 | @root_validator(pre=True) |
| 106 | @classmethod |
| 107 | def populate_root(cls, values): |
| 108 | return {'root': values} |
nothing calls this directly
no test coverage detected