<h3 dir="auto">Discussed in <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="4835061" data-permission-text="Title is private" data-url="https://github.com/sqlalchemy/sqlalchemy/discussions/9264" data-hovercard-type="discussion" data-hovercard-url="/sqlalchemy/sqlalchemy/discussions/9264/hovercard" href="https://github.com/sqlalchemy/sqlalchemy/discussions/9264">#9264</a></h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import pydantic
from sqlalchemy.orm import (
    DeclarativeBase,
    Mapped,
    MappedAsDataclass,
    mapped_column,
    registry,
)


class MappedAsPydanticDataclass(MappedAsDataclass):
    registry = registry(dataclass_callable=pydantic.dataclasses.dataclass)


class Base(MappedAsPydanticDataclass, DeclarativeBase):
    &quot;&quot;&quot;subclasses will be converted to dataclasses&quot;&quot;&quot;

    # we need to write __annotations__ on mixins the way we do on mapped
    # classes
    name: Mapped[str]


class User(Base):
    __tablename__ = &quot;user&quot;

    id: Mapped[int] = mapped_column(primary_key=True)
"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">pydantic</span>
<span class="pl-k">from</span> <span class="pl-s1">sqlalchemy</span>.<span class="pl-s1">orm</span> <span class="pl-k">import</span> (
    <span class="pl-v">DeclarativeBase</span>,
    <span class="pl-v">Mapped</span>,
    <span class="pl-v">MappedAsDataclass</span>,
    <span class="pl-s1">mapped_column</span>,
    <span class="pl-s1">registry</span>,
)


<span class="pl-k">class</span> <span class="pl-v">MappedAsPydanticDataclass</span>(<span class="pl-v">MappedAsDataclass</span>):
    <span class="pl-s1">registry</span> <span class="pl-c1">=</span> <span class="pl-en">registry</span>(<span class="pl-s1">dataclass_callable</span><span class="pl-c1">=</span><span class="pl-s1">pydantic</span>.<span class="pl-s1">dataclasses</span>.<span class="pl-s1">dataclass</span>)


<span class="pl-k">class</span> <span class="pl-v">Base</span>(<span class="pl-v">MappedAsPydanticDataclass</span>, <span class="pl-v">DeclarativeBase</span>):
    <span class="pl-s">"""subclasses will be converted to dataclasses"""</span>

    <span class="pl-c"># we need to write __annotations__ on mixins the way we do on mapped</span>
    <span class="pl-c"># classes</span>
    <span class="pl-s1">name</span>: <span class="pl-v">Mapped</span>[<span class="pl-s1">str</span>]


<span class="pl-k">class</span> <span class="pl-v">User</span>(<span class="pl-v">Base</span>):
    <span class="pl-s1">__tablename__</span> <span class="pl-c1">=</span> <span class="pl-s">"user"</span>

    <span class="pl-s1">id</span>: <span class="pl-v">Mapped</span>[<span class="pl-s1">int</span>] <span class="pl-c1">=</span> <span class="pl-en">mapped_column</span>(<span class="pl-s1">primary_key</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)</pre></div>
<h3 dir="auto">Discussed in <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="4835061" data-permission-text="Title is private" data-url="https://github.com/sqlalchemy/sqlalchemy/discussions/9264" data-hovercard-type="discussion" data-hovercard-url="/sqlalchemy/sqlalchemy/discussions/9264/hovercard" href="https://github.com/sqlalchemy/sqlalchemy/discussions/9264">#9264</a></h3>
<p dir="auto">it seems that there are 3rd party libraries that extend dataclasses with additional functionality, like <a href="https://docs.pydantic.dev/usage/dataclasses/" rel="nofollow">pydantic</a></p>
<p dir="auto">The registry could have an argument that allows customization of the dataclasses library</p>
<p dir="auto"><del><a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/zzzeek/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/zzzeek">@zzzeek</a> do you think we should accept a module or a single <code class="notranslate">dataclass</code> classable here?</del></p>
<p dir="auto">let's pass a single callable, since the pydantic dataclasses module has a different interface than the builtin one</p>