.. include:: changelog_07.rst
:start-line: 5
+.. changelog::
+ :version: 1.0.2
+
+ .. change::
+ :tags: bug, ext, declarative
+ :tickets: 3383
+
+ Fixed regression regarding the declarative ``__declare_first__``
+ and ``__declare_last__`` accessors where these would no longer be
+ called on the superclass of the declarative base.
+
.. changelog::
:version: 1.0.1
:released: April 23, 2015
from .inspection import inspect
from .engine import create_engine, engine_from_config
-__version__ = '1.0.1'
+__version__ = '1.0.2'
def __go(lcls):
return cls
-def _get_immediate_cls_attr(cls, attrname):
+def _get_immediate_cls_attr(cls, attrname, strict=False):
"""return an attribute of the class that is either present directly
on the class, e.g. not on a superclass, or is from a superclass but
this superclass is a mixin, that is, not a descendant of
for base in cls.__mro__:
_is_declarative_inherits = hasattr(base, '_decl_class_registry')
- if attrname in base.__dict__:
- value = getattr(base, attrname)
- if (base is cls or
- (base in cls.__bases__ and not _is_declarative_inherits)):
- return value
+ if attrname in base.__dict__ and (
+ base is cls or
+ ((base in cls.__bases__ if strict else True)
+ and not _is_declarative_inherits)
+ ):
+ return getattr(base, attrname)
else:
return None
@classmethod
def setup_mapping(cls, cls_, classname, dict_):
defer_map = _get_immediate_cls_attr(
- cls_, '_sa_decl_prepare_nocascade') or \
+ cls_, '_sa_decl_prepare_nocascade', strict=True) or \
hasattr(cls_, '_sa_decl_prepare')
if defer_map:
for base in cls.__mro__:
class_mapped = base is not cls and \
_declared_mapping_info(base) is not None and \
- not _get_immediate_cls_attr(base, '_sa_decl_prepare_nocascade')
+ not _get_immediate_cls_attr(
+ base, '_sa_decl_prepare_nocascade', strict=True)
if not class_mapped and base is not cls:
self._produce_column_copies(base)
continue
if _declared_mapping_info(c) is not None and \
not _get_immediate_cls_attr(
- c, '_sa_decl_prepare_nocascade'):
+ c, '_sa_decl_prepare_nocascade', strict=True):
self.inherits = c
break
else:
AssertsExecutionResults, expect_deprecated, expect_warnings
from .util import run_as_contextmanager, rowset, fail, \
- provide_metadata, adict, force_drop_names
+ provide_metadata, adict, force_drop_names, \
+ teardown_events
crashes = skip
ForeignKeyConstraint(
[tb.c.x], [tb.c.y], name=fkc)
))
+
+
+def teardown_events(event_cls):
+ @decorator
+ def decorate(fn, *arg, **kw):
+ try:
+ return fn(*arg, **kw)
+ finally:
+ event_cls._clear()
+ return decorate
+
"actual_documents.send_method AS send_method, "
"actual_documents.id AS id, 'actual' AS type "
"FROM actual_documents) AS pjoin"
- )
\ No newline at end of file
+ )
+
configure_mappers, clear_mappers, \
deferred, column_property, Session, base as orm_base
from sqlalchemy.util import classproperty
-from sqlalchemy.ext.declarative import declared_attr
+from sqlalchemy.ext.declarative import declared_attr, declarative_base
+from sqlalchemy.orm import events as orm_events
from sqlalchemy.testing import fixtures, mock
from sqlalchemy.testing.util import gc_collect
eq_(MyModel.__table__.kwargs, {'mysql_engine': 'InnoDB'})
+ @testing.teardown_events(orm_events.MapperEvents)
+ def test_declare_first_mixin(self):
+ canary = mock.Mock()
+
+ class MyMixin(object):
+ @classmethod
+ def __declare_first__(cls):
+ canary.declare_first__(cls)
+
+ @classmethod
+ def __declare_last__(cls):
+ canary.declare_last__(cls)
+
+ class MyModel(Base, MyMixin):
+ __tablename__ = 'test'
+ id = Column(Integer, primary_key=True)
+
+ configure_mappers()
+
+ eq_(
+ canary.mock_calls,
+ [
+ mock.call.declare_first__(MyModel),
+ mock.call.declare_last__(MyModel),
+ ]
+ )
+
+ @testing.teardown_events(orm_events.MapperEvents)
+ def test_declare_first_base(self):
+ canary = mock.Mock()
+
+ class MyMixin(object):
+ @classmethod
+ def __declare_first__(cls):
+ canary.declare_first__(cls)
+
+ @classmethod
+ def __declare_last__(cls):
+ canary.declare_last__(cls)
+
+ class Base(MyMixin):
+ pass
+ Base = declarative_base(cls=Base)
+
+ class MyModel(Base):
+ __tablename__ = 'test'
+ id = Column(Integer, primary_key=True)
+
+ configure_mappers()
+
+ eq_(
+ canary.mock_calls,
+ [
+ mock.call.declare_first__(MyModel),
+ mock.call.declare_last__(MyModel),
+ ]
+ )
+
+ @testing.teardown_events(orm_events.MapperEvents)
+ def test_declare_first_direct(self):
+ canary = mock.Mock()
+
+ class MyOtherModel(Base):
+ __tablename__ = 'test2'
+ id = Column(Integer, primary_key=True)
+
+ @classmethod
+ def __declare_first__(cls):
+ canary.declare_first__(cls)
+
+ @classmethod
+ def __declare_last__(cls):
+ canary.declare_last__(cls)
+
+ configure_mappers()
+
+ eq_(
+ canary.mock_calls,
+ [
+ mock.call.declare_first__(MyOtherModel),
+ mock.call.declare_last__(MyOtherModel)
+ ]
+ )
+
def test_mapper_args_declared_attr(self):
class ComputedMapperArgs: