Skip to content

Commit 9eef7c7

Browse files
committed
Add benchmark for field validator functions
1 parent 0d12efd commit 9eef7c7

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

tests/benchmarks/test_micro_benchmarks.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,3 +1450,23 @@ def test_tagged_union_int_keys_json(benchmark):
14501450
v.validate_json('{"x": 1001, "y": "1"}')
14511451

14521452
benchmark(v.validate_json, payload)
1453+
1454+
1455+
@pytest.mark.benchmark(group='field_function_validator')
1456+
def test_field_function_validator(benchmark) -> None:
1457+
def f(v: int, info: core_schema.FieldValidationInfo) -> int:
1458+
assert info.field_name == 'x'
1459+
return v + 1
1460+
1461+
schema: core_schema.CoreSchema = core_schema.int_schema()
1462+
1463+
for _ in range(100):
1464+
schema = core_schema.field_after_validator_function(f, schema)
1465+
1466+
schema = core_schema.typed_dict_schema({'x': core_schema.typed_dict_field(schema)})
1467+
1468+
v = SchemaValidator(schema)
1469+
payload = {'x': 0}
1470+
assert v.validate_python(payload) == {'x': 100}
1471+
1472+
benchmark(v.validate_python, payload)

0 commit comments

Comments
 (0)