From a463b1109abb60fc85f8356f30c0351a4e2ed71e Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Fri, 18 Feb 2022 10:05:12 -0500 Subject: implement dataclass_transforms Implement a new means of creating a mapped dataclass where instead of applying the `@dataclass` decorator distinctly, the declarative process itself can create the dataclass. MapperProperty and MappedColumn objects themselves take the place of the dataclasses.Field object when constructing the class. The overall approach is made possible at the typing level using pep-681 dataclass transforms [1]. This new approach should be able to completely supersede the previous "dataclasses" approach of embedding metadata into Field() objects, which remains a mutually exclusive declarative setup style (mixing them introduces new issues that are not worth solving). [1] https://peps.python.org/pep-0681/#transform-descriptor-types-example Fixes: #7642 Change-Id: I6ba88a87c5df38270317b4faf085904d91c8a63c --- lib/sqlalchemy/testing/fixtures.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'lib/sqlalchemy/testing') diff --git a/lib/sqlalchemy/testing/fixtures.py b/lib/sqlalchemy/testing/fixtures.py index 53f76f3ce..d4e4d2dca 100644 --- a/lib/sqlalchemy/testing/fixtures.py +++ b/lib/sqlalchemy/testing/fixtures.py @@ -25,6 +25,7 @@ from .. import event from .. import util from ..orm import declarative_base from ..orm import DeclarativeBase +from ..orm import MappedAsDataclass from ..orm import registry from ..schema import sort_tables_and_constraints @@ -90,7 +91,14 @@ class TestBase: @config.fixture() def registry(self, metadata): - reg = registry(metadata=metadata) + reg = registry( + metadata=metadata, + type_annotation_map={ + str: sa.String().with_variant( + sa.String(50), "mysql", "mariadb" + ) + }, + ) yield reg reg.dispose() @@ -109,6 +117,21 @@ class TestBase: yield Base Base.registry.dispose() + @config.fixture + def dc_decl_base(self, metadata): + _md = metadata + + class Base(MappedAsDataclass, DeclarativeBase): + metadata = _md + type_annotation_map = { + str: sa.String().with_variant( + sa.String(50), "mysql", "mariadb" + ) + } + + yield Base + Base.registry.dispose() + @config.fixture() def future_connection(self, future_engine, connection): # integrate the future_engine and connection fixtures so -- cgit v1.2.1