1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
import textwrap
import msgspec
from utils import temp_module
def test_process_large_recursive_union():
"""
A recursive schema processing perf test from
https://github.com/pydantic/pydantic/issues/8499
This test is mostly to ensure that processing deeply recursive schemas with
unions succeeds.
"""
def gen_code():
yield "from __future__ import annotations"
yield "from msgspec import Struct"
yield "from typing import Union"
for i in range(50):
yield textwrap.dedent(
f"""
class Node{i}(Struct, tag='node{i}'):
data: Union[Node, None]
"""
)
yield "Node = Union["
for i in range(50):
yield f" Node{i},"
yield "]"
code = "\n".join(gen_code())
with temp_module(code) as mod:
dec = msgspec.json.Decoder(mod.Node)
msg = b"""
{
"type": "node25",
"data": {
"type": "node13",
"data": null
}
}
"""
sol = mod.Node25(mod.Node13(None))
assert dec.decode(msg) == sol
|