]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commit
MappedAsDataclass applies @dataclasses.dataclass unconditionally
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 30 Jan 2023 18:28:42 +0000 (13:28 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 30 Jan 2023 20:40:20 +0000 (15:40 -0500)
commit4d3a50c2f1af894b081c5f64c529da89f35f1839
tree19e5e5f294c7dd627806c6aeb50d814402dcd4a0
parentbcb5c850f2a5279bf7a97af6fbf99cc63dffd62f
MappedAsDataclass applies @dataclasses.dataclass unconditionally

When using the :class:`.MappedAsDataclass` superclass, all classes within
the hierarchy that are subclasses of this class will now be run through the
``@dataclasses.dataclass`` function whether or not they are actually
mapped, so that non-ORM fields declared on non-mapped classes within the
hierarchy will be used when mapped subclasses are turned into dataclasses.
This behavior applies both to intermediary classes mapped with
``__abstract__ = True`` as well as to the user-defined declarative base
itself, assuming :class:`.MappedAsDataclass` is present as a superclass for
these classes.

This allows non-mapped attributes such as ``InitVar`` declarations on
superclasses to be used, without the need to run the
``@dataclasses.dataclass`` decorator explicitly on each non-mapped class.
The new behavior is considered as correct as this is what the :pep:`681`
implementation expects when using a superclass to indicate dataclass
behavior.

Fixes: #9179
Change-Id: Ia01fa9806a27f7c1121bf7eaddf2847cf6dc5313
doc/build/changelog/unreleased_20/9179.rst [new file with mode: 0644]
lib/sqlalchemy/orm/decl_api.py
lib/sqlalchemy/orm/decl_base.py
test/orm/declarative/test_dc_transforms.py