instrument_class event is being called at the wrong time in 1.0

Issue #3388 resolved
Mike Bayer repo owner created an issue

this works in 0.9, fails in 1.0. Not really sure how this can be happening since nothing should have changed in this regard:

from sqlalchemy import event
from sqlalchemy import MetaData, Table, Column, Integer
from sqlalchemy.orm import mapper


def my_init(self):
    print("hi")


@event.listens_for(mapper, "instrument_class")
def instrument_class(mp, class_):
    print("INSTRUMENT CLASS!")
    class_.__init__ = my_init

m = MetaData()
foo = Table('foo', m, Column('id', Integer, primary_key=True))


class Foo(object):
    pass

mapper(Foo, foo)

f1 = Foo() assert f1._sa_instance_state

Comments (2)

  1. Mike Bayer reporter
    • Fixed a regression regarding the :meth:.MapperEvents.instrument_class event where its invocation was moved to be after the class manager's instrumentation of the class, which is the opposite of what the documentation for the event explicitly states. The rationale for the switch was due to Declarative taking the step of setting up the full "instrumentation manager" for a class before it was mapped for the purpose of the new @declared_attr features described in :ref:feature_3150, but the change was also made against the classical use of :func:.mapper for consistency. However, SQLSoup relies upon the instrumentation event happening before any instrumentation under classical mapping. The behavior is reverted in the case of classical and declarative mapping, the latter implemented by using a simple memoization without using class manager. fixes #3388

    → <<cset 6c0f30db81d1>>

  2. Log in to comment