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

Support for SQLAlchemy dataclass #517

Closed
barsa-net opened this issue Apr 25, 2024 · 0 comments · Fixed by #518
Closed

Support for SQLAlchemy dataclass #517

barsa-net opened this issue Apr 25, 2024 · 0 comments · Fixed by #518
Labels
enhancement New feature or request

Comments

@barsa-net
Copy link
Contributor

barsa-net commented Apr 25, 2024

SQLAlchemy 2.0 introduced PEP 681 support to its integration with dataclasses, but unfortunately this is implemented through a generic type Mapped[T] that at the moment is not correctly understood.

from typing import Optional

from serde import serde, from_dict
from sqlalchemy import String, JSON
from sqlalchemy.orm import DeclarativeBase, MappedAsDataclass
from sqlalchemy.orm import Mapped
from sqlalchemy.orm import mapped_column


class Base(MappedAsDataclass, DeclarativeBase):
    pass


@serde
class User(Base):
    __tablename__ = "user"

    id: Mapped[int] = mapped_column(primary_key=True)
    name: Mapped[str]
    fullname: Mapped[str] = mapped_column(String(30))
    nickname: Mapped[Optional[str]]
    meta: Mapped[Optional[JSON]] = mapped_column(type_=JSON)


user = from_dict(User, {"id": 1, "name": "john", "fullname": "John Doe"})

Leads to an error:

Traceback (most recent call last):
  File "[...]/example.py", line 25, in <module>
    user = from_dict(User, {"id": 1, "name": "john", "fullname": "John Doe"})
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "[...]/lib/python3.12/site-packages/serde/de.py", line 529, in from_dict
    return from_obj(cls, o, named=True, reuse_instances=reuse_instances)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "[...]/lib/python3.12/site-packages/serde/de.py", line 497, in from_obj
    raise SerdeError(e) from None
serde.compat.SerdeError: Unsupported type: int
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants