Skip to content

Commit

Permalink
Add benchmark decorators (#9123)
Browse files Browse the repository at this point in the history
  • Loading branch information
sydney-runkle committed Mar 27, 2024
1 parent 0634dd4 commit c95fe75
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
3 changes: 3 additions & 0 deletions tests/benchmarks/test_discriminated_unions.py
Expand Up @@ -2,6 +2,7 @@

from typing import Literal, Union

import pytest
from typing_extensions import Annotated

from pydantic import BaseModel, Field, TypeAdapter
Expand All @@ -24,6 +25,7 @@ class LeafState(BaseModel):
AnyState = Annotated[Union[NestedState, LoopState, LeafState], Field(..., discriminator='state_type')]


@pytest.mark.benchmark
def test_schema_build() -> None:
adapter = TypeAdapter(AnyState)
assert adapter.core_schema['schema']['type'] == 'tagged-union'
Expand All @@ -39,6 +41,7 @@ def build_nested_state(n):
return {'state_type': 'loop', 'substate': {'state_type': 'nested', 'substate': build_nested_state(n - 1)}}


@pytest.mark.benchmark
def test_efficiency_with_highly_nested_examples() -> None:
# can go much higher, but we keep it reasonably low here for a proof of concept
for i in range(1, 12):
Expand Down
2 changes: 2 additions & 0 deletions tests/benchmarks/test_schema_build.py
@@ -1,5 +1,6 @@
from typing import Literal, Union

import pytest
from typing_extensions import Annotated

from pydantic import BaseModel, Discriminator
Expand All @@ -25,6 +26,7 @@ class NestedModel(BaseModel):
animal: Annotated[Union[Cat, Dog], Discriminator('type')]


@pytest.mark.benchmark
def test_construct_schema():
@dataclass(frozen=True, kw_only=True)
class Root:
Expand Down

0 comments on commit c95fe75

Please sign in to comment.