MCPcopy
hub / github.com/pydantic/pydantic / test_failed_rebuild

Function test_failed_rebuild

tests/benchmarks/test_model_schema_generation.py:234–253  ·  view source on GitHub ↗

The defined model has 8 fields with some relatively complex annotations. The last field has an undefined forward annotation, that will fail to resolve on model rebuild. The benchmark is used to measure time spent in trying to rebuild a model knowing that it will fail when encountering t

(benchmark)

Source from the content-addressed store, hash-verified

232
233@pytest.mark.benchmark(group='model_schema_generation')
234def test_failed_rebuild(benchmark):
235 """The defined model has 8 fields with some relatively complex annotations.
236
237 The last field has an undefined forward annotation, that will fail to resolve on model rebuild.
238 The benchmark is used to measure time spent in trying to rebuild a model knowing that it will
239 fail when encountering the last field.
240 """
241 fields: dict[str, Any] = {
242 f'f{i}': Annotated[Union[int, Annotated[str, Field(max_length=3)]], AfterValidator(lambda v: v)]
243 for i in range(8)
244 }
245 fields['f8'] = 'Undefined'
246
247 ModelWithUndefinedAnnotation = create_model( # pyright: ignore[reportCallIssue]
248 'ModelWithUndefinedAnnotation',
249 __base__=DeferredModel,
250 **fields,
251 )
252
253 benchmark(rebuild_model, ModelWithUndefinedAnnotation, raise_errors=False)
254
255
256@pytest.mark.benchmark(group='model_schema_generation')

Callers

nothing calls this directly

Calls 3

FieldFunction · 0.90
AfterValidatorClass · 0.90
create_modelFunction · 0.90

Tested by

no test coverage detected