Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a deeply recursive schema/union test #638

Merged
merged 1 commit into from
Jan 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions tests/test_performance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
Loading