]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commit
unmapped_dataclass
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 14 Jul 2025 17:50:09 +0000 (13:50 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 18 Sep 2025 19:17:18 +0000 (15:17 -0400)
commit02c9d3ab43e163cfa3ffa4f9658ccad323ca686d
treeb79426b2c14f6a9a4a0c3d482b328330bb573089
parent8bb8c1a6d6439380f682a57b92e3d1b0092250eb
unmapped_dataclass

Reworked the handling of classes which extend from MappedAsDataclass but
are not themselves mapped, i.e. the declarative base as well as any
mixins or abstract classes.  These classes as before are turned into
real dataclasses, however a scan now takes place across the mapped
elements such as mapped_column(), relationship(), etc. so that we may
also take into account dataclasses.field-specific parameters like
init=False, repr, etc.  The main use case for this is so that mixin
dataclasses may make use of "default" in fields while not being rejected
by the dataclasses constructor.  The generated classes are more
functional as dataclasses in a standalone fashion as well, even though
this is not their intended use.  As a standalone dataclass, the one
feature that does not work is a field that has
a default with init=False, because we still need to have a
mapped_column() or similar present at the class level for the class
to work as a superclass.

The change also addes the :func:`_orm.unmapped_dataclass` decorator
function, which may be used
to create unmapped superclasses in a mapped hierarchy that is using the
:func:`_orm.mapped_dataclass` decorator to create mapped dataclasses.
Previously there was no way to use unmapped dataclass mixins with
the decorator approach.

Finally, the warning added in 2.0 for :ticket:`9350` is turned into
an error as mentioned for 2.1, since we're deep into dataclass hierarchy
changes here.

Fixes: #12854
Change-Id: I11cd8c628d49e9ff1bdbda8a09f4112b40d84be7
18 files changed:
doc/build/changelog/unreleased_21/12854.rst [new file with mode: 0644]
doc/build/errors.rst
doc/build/orm/dataclasses.rst
doc/build/orm/mapping_api.rst
lib/sqlalchemy/ext/automap.py
lib/sqlalchemy/ext/declarative/extensions.py
lib/sqlalchemy/orm/__init__.py
lib/sqlalchemy/orm/decl_api.py
lib/sqlalchemy/orm/decl_base.py
lib/sqlalchemy/orm/descriptor_props.py
lib/sqlalchemy/orm/interfaces.py
lib/sqlalchemy/orm/properties.py
lib/sqlalchemy/orm/relationships.py
test/ext/declarative/test_reflection.py
test/orm/declarative/test_basic.py
test/orm/declarative/test_dc_transforms.py
test/orm/declarative/test_dc_transforms_future_anno_sync.py
test/typing/plain_files/orm/dataclass_transforms_decorator_w_mixins.py [new file with mode: 0644]