Skip to content

Commit 9cdd52f

Browse files
committed
Add tagged union benchmarks
1 parent 3d3cb7f commit 9cdd52f

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

tests/benchmarks/test_micro_benchmarks.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,3 +1410,43 @@ def test_int_range_json(benchmark):
14101410
v.validate_python('0')
14111411

14121412
benchmark(v.validate_json, '42')
1413+
1414+
1415+
@pytest.mark.benchmark(group='tagged_union_ints')
1416+
def test_tagged_union_int_keys_python(benchmark):
1417+
inner_schema = core_schema.typed_dict_schema(
1418+
{
1419+
'x': core_schema.typed_dict_field(core_schema.int_schema()),
1420+
'y': core_schema.typed_dict_field(core_schema.int_schema()),
1421+
}
1422+
)
1423+
v = SchemaValidator(core_schema.tagged_union_schema({x: inner_schema for x in range(1000)}, discriminator='x'))
1424+
1425+
payload = {'x': 999, 'y': '1'}
1426+
assert v.validate_python(payload) == {'x': 999, 'y': 1}
1427+
with pytest.raises(
1428+
ValidationError, match="Input tag '1001' found using 'x' does not match any of the expected tags"
1429+
):
1430+
v.validate_python({'x': 1001, 'y': '1'})
1431+
1432+
benchmark(v.validate_python, payload)
1433+
1434+
1435+
@pytest.mark.benchmark(group='tagged_union_ints')
1436+
def test_tagged_union_int_keys_json(benchmark):
1437+
inner_schema = core_schema.typed_dict_schema(
1438+
{
1439+
'x': core_schema.typed_dict_field(core_schema.int_schema()),
1440+
'y': core_schema.typed_dict_field(core_schema.int_schema()),
1441+
}
1442+
)
1443+
v = SchemaValidator(core_schema.tagged_union_schema({x: inner_schema for x in range(1000)}, discriminator='x'))
1444+
1445+
payload = '{"x": 999, "y": "1"}'
1446+
assert v.validate_json(payload) == {'x': 999, 'y': 1}
1447+
with pytest.raises(
1448+
ValidationError, match="Input tag '1001' found using 'x' does not match any of the expected tags"
1449+
):
1450+
v.validate_json('{"x": 1001, "y": "1"}')
1451+
1452+
benchmark(v.validate_json, payload)

0 commit comments

Comments
 (0)