diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2022-02-18 10:05:12 -0500 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2022-05-20 14:19:02 -0400 |
| commit | a463b1109abb60fc85f8356f30c0351a4e2ed71e (patch) | |
| tree | de8f96b7bce319fc0f19f56b302202ea3e4e91db /lib/sqlalchemy/testing | |
| parent | 9e7bed9df601ead02fd96bf2fc787b23b536d2d6 (diff) | |
| download | sqlalchemy-a463b1109abb60fc85f8356f30c0351a4e2ed71e.tar.gz | |
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
Diffstat (limited to 'lib/sqlalchemy/testing')
| -rw-r--r-- | lib/sqlalchemy/testing/fixtures.py | 25 |
1 files changed, 24 insertions, 1 deletions
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 |
