Skip to content

dataclasses re-attaches "Field" to object after mapping #8880

Closed
@zzzeek

Description

@zzzeek
Member
from __future__ import annotations

import dataclasses

from sqlalchemy.orm import DeclarativeBase
from sqlalchemy.orm import Mapped
from sqlalchemy.orm import mapped_column
from sqlalchemy.orm import MappedAsDataclass


class Base(DeclarativeBase):
    pass


@dataclasses.dataclass
class A:
    some_other_field: int
    some_field: int = dataclasses.field(default=5)


class B(MappedAsDataclass, Base):
    __tablename__ = "b"
    __allow_unmapped__ = True

    id: Mapped[int] = mapped_column(primary_key=True, init=False)
    some_other_field: int
    some_field: int = dataclasses.field(default=5)


for cls in A, B:
    assert "some_other_field" not in cls.__dict__
    assert cls.__dict__["some_field"] == 5

Activity

added this to the 2.0beta4 milestone on Nov 25, 2022
sqla-tester

sqla-tester commented on Nov 25, 2022

@sqla-tester
Collaborator

Mike Bayer has proposed a fix for this issue in the main branch:

WIP: annotated / DC forms for association proxy https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/4240

added a commit that references this issue on Jun 9, 2025
3e3e3ab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingdataclassesorm - annotated declarativeissues with the new annotations-based declarative ORM approach

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @zzzeek@sqla-tester

        Issue actions

          dataclasses re-attaches "Field" to object after mapping · Issue #8880 · sqlalchemy/sqlalchemy