Skip to content

declarative meta doesnt work with __init_subclass__ #7900

Closed
@zzzeek

Description

@zzzeek
Member

i can fix this but I have to make declarative meta look at cls.__dict__, and that's confusing me. What's "dict_" then ? shrugs. lets just run through CI

        class Base:
            def __init_subclass__(cls) -> None:
                cls.id = Column(Integer, primary_key=True)

        Base = registry.generate_base(cls=Base)

        class Foo(Base):
            __tablename__ = 'foo'
            name = Column(String)

        self.assert_compile(select(Foo), "SELECT foo.name, foo.id FROM foo")

1.4 tentative

Activity

added
bugSomething isn't working
declarativehas to do with the declarative API, scanning classes and mixins for attributes to be mapped
on Apr 4, 2022
added this to the 1.4.x milestone on Apr 4, 2022
sqla-tester

sqla-tester commented on Apr 4, 2022

@sqla-tester
Collaborator

Mike Bayer has proposed a fix for this issue in the main branch:

read from cls.dict so init_subclass works https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/3769

sqla-tester

sqla-tester commented on Apr 4, 2022

@sqla-tester
Collaborator

Mike Bayer has proposed a fix for this issue in the rel_1_4 branch:

read from cls.dict so init_subclass works https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/3770

added a commit that references this issue on Apr 13, 2022
289894f
subhashb

subhashb commented on May 8, 2022

@subhashb

@zzzeek Does this change alter our discussion in #6791?

As per that discussion:

since your metaclass is attempting to produce a new namespace for a new class, it should be populating the dictionary, and only optionally the actual cls.dict

I had changed my code to populate the dictionary passed into __init__ instead of cls.__dict__. Now, 1.4.36 breaks my code, while 1.4.35 works.

What do you suggest?

zzzeek

zzzeek commented on May 8, 2022

@zzzeek
MemberAuthor

@zzzeek Does this change alter our discussion in #6791?

yes.

As per that discussion:

since your metaclass is attempting to produce a new namespace for a new class, it should be populating the dictionary, and only optionally the actual cls.dict

I had changed my code to populate the dictionary passed into __init__ instead of cls.__dict__. Now, 1.4.36 breaks my code, while 1.4.35 works.

What do you suggest?

populate both class.__dict__ and dict_.

zzzeek

zzzeek commented on May 8, 2022

@zzzeek
MemberAuthor

or, block SQLAlchemy versions from 1.4.10 -> 1.4.35 and revert your code to what you had previously in #6791

subhashb

subhashb commented on May 10, 2022

@subhashb

I will go the route of populating both dict_ and class.__dict__ as I need to accommodate both pre and post 1.4.36 changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingdeclarativehas to do with the declarative API, scanning classes and mixins for attributes to be mappedorm

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @zzzeek@subhashb@sqla-tester

        Issue actions

          declarative meta doesnt work with `__init_subclass__` · Issue #7900 · sqlalchemy/sqlalchemy