()
| 2136 | |
| 2137 | |
| 2138 | def test_post_init(): |
| 2139 | calls = [] |
| 2140 | |
| 2141 | class InnerModel(BaseModel): |
| 2142 | a: int |
| 2143 | b: int |
| 2144 | |
| 2145 | def model_post_init(self, context, /) -> None: |
| 2146 | super().model_post_init(context) # this is included just to show it doesn't error |
| 2147 | assert self.model_dump() == {'a': 3, 'b': 4} |
| 2148 | calls.append('inner_model_post_init') |
| 2149 | |
| 2150 | class Model(BaseModel): |
| 2151 | c: int |
| 2152 | d: int |
| 2153 | sub: InnerModel |
| 2154 | |
| 2155 | def model_post_init(self, context, /) -> None: |
| 2156 | assert self.model_dump() == {'c': 1, 'd': 2, 'sub': {'a': 3, 'b': 4}} |
| 2157 | calls.append('model_post_init') |
| 2158 | |
| 2159 | m = Model(c=1, d='2', sub={'a': 3, 'b': '4'}) |
| 2160 | assert calls == ['inner_model_post_init', 'model_post_init'] |
| 2161 | assert m.model_dump() == {'c': 1, 'd': 2, 'sub': {'a': 3, 'b': 4}} |
| 2162 | |
| 2163 | class SubModel(Model): |
| 2164 | def model_post_init(self, context, /) -> None: |
| 2165 | assert self.model_dump() == {'c': 1, 'd': 2, 'sub': {'a': 3, 'b': 4}} |
| 2166 | super().model_post_init(context) |
| 2167 | calls.append('submodel_post_init') |
| 2168 | |
| 2169 | calls.clear() |
| 2170 | m = SubModel(c=1, d='2', sub={'a': 3, 'b': '4'}) |
| 2171 | assert calls == ['inner_model_post_init', 'model_post_init', 'submodel_post_init'] |
| 2172 | assert m.model_dump() == {'c': 1, 'd': 2, 'sub': {'a': 3, 'b': 4}} |
| 2173 | |
| 2174 | |
| 2175 | def test_post_init_function_attrs_preserved() -> None: |
nothing calls this directly
no test coverage detected