:ref:`declarative_config_toplevel`
+.. _orm_explicit_declarative_base:
+
+Creating an Explicit Base Non-Dynamically (for use with mypy, similar)
+----------------------------------------------------------------------
+
+Tools like mypy are not necessarily compatible with the dynamically
+generated ``Base`` delivered by SQLAlchemy functions like :func:`_orm.declarative_base`.
+To build a declarative base in a non-dynamic fashion, the
+:class:`_orm.DeclarativeMeta` class may be used directly as follows::
+
+ from sqlalchemy.orm import registry
+ from sqlalchemy.orm.decl_api import DeclarativeMeta
+
+ mapper_registry = registry()
+
+ class Base(metaclass=DeclarativeMeta):
+ __abstract__ = True
+ registry = mapper_registry
+ metadata = mapper_registry.metadata
+
+The above ``Base`` is equivalent to one created using the
+:meth:`_orm.registry.generate_base` method and will be fully understood by
+type analysis tools without the use of plugins.
+
.. _orm_declarative_decorator:
from .context import QueryContext # noqa
from .decl_api import as_declarative # noqa
from .decl_api import declarative_base # noqa
+from .decl_api import DeclarativeMeta # noqa
from .decl_api import declared_attr # noqa
from .decl_api import has_inherited_table # noqa
from .decl_api import registry # noqa
__tablename__ = "my_table"
id = Column(Integer, primary_key=True)
+ The above dynamically generated class is equivalent to the
+ non-dynamic example below::
+
+ from sqlalchemy.orm import registry
+ from sqlalchemy.orm.decl_api import DeclarativeMeta
+
+ mapper_registry = registry()
+
+ class Base(metaclass=DeclarativeMeta):
+ __abstract__ = True
+ registry = mapper_registry
+ metadata = mapper_registry.metadata
+
The :meth:`_orm.registry.generate_base` method provides the
implementation for the :func:`_orm.declarative_base` function, which
creates the :class:`_orm.registry` and base class all at once.
-
See the section :ref:`orm_declarative_mapping` for background and
examples.
):
__dialect__ = "default"
+ base_style = "dynamic"
+
def setup_test(self):
global Base
- Base = declarative_base(testing.db)
+
+ if self.base_style == "dynamic":
+ Base = declarative_base(testing.db)
+ elif self.base_style == "explicit":
+ mapper_registry = registry(_bind=testing.db)
+
+ class Base(with_metaclass(DeclarativeMeta)):
+ __abstract__ = True
+ registry = mapper_registry
+ metadata = mapper_registry.metadata
def teardown_test(self):
close_all_sessions()
Base.metadata.drop_all(testing.db)
+@testing.combinations(
+ ("dynamic",), ("explicit",), argnames="base_style", id_="s"
+)
class DeclarativeTest(DeclarativeTestBase):
def test_basic(self):
class User(Base, fixtures.ComparableEntity):
eq_(UserType._set_random_keyword_used_here, True)
+# TODO: this should be using @combinations
def _produce_test(inline, stringbased):
class ExplicitJoinTest(fixtures.MappedTest):
@classmethod