Closed
Description
due to #9171
I would expect this to work
from __future__ import annotations
from sqlalchemy.orm import DeclarativeBase
from sqlalchemy.orm import Mapped
from sqlalchemy.orm import mapped_column
class Mixin:
def __init__(self, **kw):
print("mixin!")
class Base(Mixin, DeclarativeBase):
pass
class A(Base):
__tablename__ = "a"
id: Mapped[int] = mapped_column(primary_key=True)
data: Mapped[str]
A()
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
zzzeek commentedon Feb 6, 2023
Not sure on this one. I wanted to use it in the recipe I just posted at #9246. However it doesn't "work" anyway because I can't call super() on the mixin and get the DeclarativeBase initializer anyway (which seems weird? )
above,
super().__init__()
is going back down toobject.__init__
again. I thoughtDeclarativeBase
has__init__()
on it also? but it really can't, because__init__()
is part of the base.If we want mixins on the base to do init we can of course use the
init
mapper event.not sure how I want to do this pattern
[-]regression: mixin on base no longe has init called [/-][+]how to put mixins on the declarative base that have their own `__init__` ? [/+]zzzeek commentedon Feb 6, 2023
we can improve the behavior like this:
that way at least the super
__init__
is called. The super__init__
itself can't callsuper()
again but at least things are less mysterioussqla-tester commentedon Feb 6, 2023
Mike Bayer has proposed a fix for this issue in the main branch:
check for superclasses of user defined init https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/4429
check for superclasses of user defined init