Skip to content

fix: Incorrect to_schema generation for dataclass using ResultConverter #35

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions sqlspec/mixins.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import dataclasses
import datetime
from abc import abstractmethod
from collections.abc import Sequence
Expand Down Expand Up @@ -257,10 +258,10 @@ def to_schema(
if not isinstance(data, Sequence):
return cast("ModelT", data)
return cast("Sequence[ModelT]", data)
if is_dataclass(schema_type):
if dataclasses.is_dataclass(schema_type):
if not isinstance(data, Sequence):
# data is assumed to be dict[str, Any] as per the method's overloads
return cast("ModelDTOT", schema_type(**data)) # type: ignore[operator]
return cast("ModelDTOT", schema_type(data)) # type: ignore[operator]
# data is assumed to be Sequence[dict[str, Any]]
return cast("Sequence[ModelDTOT]", [schema_type(**item) for item in data]) # type: ignore[operator]
if is_msgspec_struct(schema_type):
Expand Down
72 changes: 72 additions & 0 deletions tests/unit/test_mixins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
"""Tests for mixins utilities."""

from __future__ import annotations

from dataclasses import dataclass, field
from typing import Any, ClassVar

import pytest
from msgspec import Struct
from pydantic import BaseModel

from sqlspec.mixins import (
ResultConverter
)
from sqlspec.typing import (
Empty,
is_dataclass_instance,
)


@dataclass
class SampleDataclass:
"""Sample dataclass for testing."""

name: str
value: int | None = None
empty_field: Any = Empty
meta: ClassVar[str] = "test"


class SamplePydanticModel(BaseModel):
"""Sample Pydantic model for testing."""

name: str
value: int | None = None


class SampleMsgspecModel(Struct):
"""Sample Msgspec model for testing."""

name: str
value: int | None = None


@pytest.fixture(scope="session")
def sample_dataclass() -> SampleDataclass:
"""Create a sample dataclass instance."""
return SampleDataclass(name="test", value=42)


@pytest.fixture(scope="session")
def sample_pydantic() -> SamplePydanticModel:
"""Create a sample Pydantic model instance."""
return SamplePydanticModel(name="test", value=42)


@pytest.fixture(scope="session")
def sample_msgspec() -> SampleMsgspecModel:
"""Create a sample Msgspec model instance."""
return SampleMsgspecModel(name="test", value=42)


@pytest.fixture(scope="session")
def sample_dict() -> dict[str, Any]:
"""Create a sample dictionary."""
return {"name": "test", "value": 42}


def test_is_result_converter_dataclass(sample_dataclass: SampleDataclass) -> None:
"""Test dataclass type checking."""
assert is_dataclass_instance(ResultConverter.to_schema(data=SampleDataclass(name="test", value=42), schema_type=SampleDataclass))

Loading