From: Federico Caselli Date: Wed, 1 Feb 2023 21:25:03 +0000 (+0100) Subject: Guard against wrong dataclass mapping X-Git-Tag: rel_2_0_2~11^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a09972dcab2d154044cfd2d60bf72e4a3f7568f1;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Guard against wrong dataclass mapping Ensure that the decorator style @registry.mapped_as_dataclass and MappedAsDataclass are not mixed. Fixes: #9211 Change-Id: I5cd94cae862122e4f627d0d051495b3789cf6de5 --- diff --git a/doc/build/changelog/unreleased_20/9211.rst b/doc/build/changelog/unreleased_20/9211.rst new file mode 100644 index 0000000000..f15a64b053 --- /dev/null +++ b/doc/build/changelog/unreleased_20/9211.rst @@ -0,0 +1,10 @@ +.. change:: + :tags: bug, orm + :tickets: 9211 + + An explicit error is raised if a mapping attempts to mix the use of + :class:`_orm.MappedAsDataclass` with + :meth:`_orm.registry.mapped_as_dataclass` within the same class hierarchy, + as this produces issues with the dataclass function being applied at the + wrong time to the mapped class, leading to errors during the mapping + process. diff --git a/lib/sqlalchemy/orm/decl_base.py b/lib/sqlalchemy/orm/decl_base.py index a858f12cb9..8ce9d59d0b 100644 --- a/lib/sqlalchemy/orm/decl_base.py +++ b/lib/sqlalchemy/orm/decl_base.py @@ -1036,6 +1036,17 @@ class _ClassScanMapperConfig(_MapperConfig): if not dataclass_setup_arguments: return + # can't use is_dataclass since it uses hasattr + if "__dataclass_fields__" in self.cls.__dict__: + raise exc.InvalidRequestError( + f"Class {self.cls} is already a dataclass; ensure that " + "base classes / decorator styles of establishing dataclasses " + "are not being mixed. " + "This can happen if a class that inherits from " + "'MappedAsDataclass', even indirectly, is been mapped with " + "'@registry.mapped_as_dataclass'" + ) + manager = instrumentation.manager_of_class(self.cls) assert manager is not None diff --git a/test/orm/declarative/test_dc_transforms.py b/test/orm/declarative/test_dc_transforms.py index 63450f4a17..302587b9ac 100644 --- a/test/orm/declarative/test_dc_transforms.py +++ b/test/orm/declarative/test_dc_transforms.py @@ -629,6 +629,23 @@ class DCTransformsTest(AssertsCompiledSQL, fixtures.TestBase): b1 = Bar(mixin_value=5) eq_(b1.bar_value, 78) + def test_mixing_MappedAsDataclass_with_decorator_raises(self, registry): + """test #9211""" + + class Mixin(MappedAsDataclass): + id: Mapped[int] = mapped_column(primary_key=True, init=False) + + with expect_raises_message( + exc.InvalidRequestError, + "Class .*Foo.* is already a dataclass; ensure that " + "base classes / decorator styles of establishing dataclasses " + "are not being mixed. ", + ): + + @registry.mapped_as_dataclass + class Foo(Mixin): + bar_value: Mapped[float] = mapped_column(default=78) + class RelationshipDefaultFactoryTest(fixtures.TestBase): def test_list(self, dc_decl_base: Type[MappedAsDataclass]):