Skip to content

Several classes have __slots__, but not their bases do not #7527

Closed

Description

@ariebovenberg

Describe the bug

Quite a few classes define __slots__, but have bases where __slots__ is not defined:

ERROR: 'sqlalchemy.engine.base.Transaction' has slots but inherits from non-slot class
ERROR: 'sqlalchemy.orm.interfaces.ORMOption' has slots but inherits from non-slot class
ERROR: 'sqlalchemy.orm.strategies.ImmediateLoader' has slots but inherits from non-slot class
ERROR: 'sqlalchemy.orm.strategies.SelectInLoader' has slots but inherits from non-slot class
ERROR: 'sqlalchemy.orm.strategies.SubqueryLoader' has slots but inherits from non-slot class
ERROR: 'sqlalchemy.orm.unitofwork.Preprocess' has slots but inherits from non-slot class
ERROR: 'sqlalchemy.orm.unitofwork.ProcessAll' has slots but inherits from non-slot class
ERROR: 'sqlalchemy.sql.base.ImmutableColumnCollection' has slots but inherits from non-slot class
ERROR: 'sqlalchemy.sql.coercions.ByOfImpl' has slots but inherits from non-slot class
ERROR: 'sqlalchemy.sql.compiler._CompileLabel' has slots but inherits from non-slot class
ERROR: 'sqlalchemy.sql.roles.ExecutableOptionRole' has slots but inherits from non-slot class
ERROR: 'sqlalchemy.sql.traversals.TraversalComparatorStrategy' has slots but inherits from non-slot class
ERROR: 'sqlalchemy.util._collections.ImmutableProperties' has slots but inherits from non-slot class

"slotless" base classes causing this:

       - sqlalchemy.engine.util.TransactionalContext
       - sqlalchemy.orm.strategies.PostLoader
       - sqlalchemy.orm.unitofwork.IterateMappersMixin
       - sqlalchemy.sql.annotation.SupportsAnnotations
       - sqlalchemy.sql.annotation.SupportsWrappingAnnotations
       - sqlalchemy.sql.base.ExecutableOption
       - sqlalchemy.sql.elements.ClauseElement
       - sqlalchemy.sql.elements.ColumnElement
       - sqlalchemy.sql.roles.AllowsLambdaRole
       - sqlalchemy.sql.roles.BinaryElementRole
       - sqlalchemy.sql.roles.ByOfRole
       - sqlalchemy.sql.roles.ColumnArgumentOrKeyRole
       - sqlalchemy.sql.roles.ColumnArgumentRole
       - sqlalchemy.sql.roles.ColumnListRole
       - sqlalchemy.sql.roles.ColumnsClauseRole
       - sqlalchemy.sql.roles.DDLConstraintColumnRole
       - sqlalchemy.sql.roles.DDLExpressionRole
       - sqlalchemy.sql.roles.DMLColumnRole
       - sqlalchemy.sql.roles.ExpressionElementRole
       - sqlalchemy.sql.roles.LimitOffsetRole
       - sqlalchemy.sql.roles.OnClauseRole
       - sqlalchemy.sql.roles.OrderByRole
       - sqlalchemy.sql.roles.SQLRole
       - sqlalchemy.sql.roles.StatementOptionRole
       - sqlalchemy.sql.roles.StructuralRole
       - sqlalchemy.sql.roles.UsesInspection
       - sqlalchemy.sql.roles.WhereHavingRole
       - sqlalchemy.sql.traversals.HasCopyInternals
       - sqlalchemy.sql.traversals.MemoizedHasCacheKey
       - sqlalchemy.sql.visitors.InternalTraversal
       - sqlalchemy.sql.visitors.Traversible
       - sqlalchemy.util._collections.ImmutableContainer

To Reproduce

>>> # just an example of one of the classes with ineffective __slots__
>>> from sqlalchemy.orm.interfaces import ORMOption
>>> ORMOption().__dict__
{}

Error

There should not be a __dict__ on properly __slot__ed classes.

Versions

  • OS: MacOS
  • Python: 3.10
  • SQLAlchemy: 1.4.29
  • Database: n/a
  • DBAPI (eg: psycopg, cx_oracle, mysqlclient): n/a

Additional context

If you agree, I'd like to investigate and fix this.

Activity

  1. added
    bugSomething isn't working
    performancewhere performance can be improved. add "bug" only if it's a performance degradation
    and removed
    requires triageNew issue that requires categorization
    on Jan 3, 2022
  2. added this to the 2.0 milestone on Jan 3, 2022
  3. zzzeek commented on Jan 3, 2022

    @zzzeek
    Member

    Sure, while I'm not sure that __slots__ can be easily applied to all of those classes (you'll see why, if so), certainly the Role classes at least can have it. I would favor an automated test suite that searches for all class hierarchies testing for slots as well.

  4. ariebovenberg commented on Jan 3, 2022

    @ariebovenberg
    ContributorAuthor

    Yeah, it has definitely my experience: adding slots is not super easy if it has been forgotten for a while.

    After having fixed slots in the returns library, I decided to build a reusable checker for this: https://github.com/ariebovenberg/slotscheck. Let me know if you have any feedback.

  5. zzzeek commented on Jan 3, 2022

    @zzzeek
    Member

    Yeah, it has definitely my experience: adding slots is not super easy if it has been forgotten for a while.

    oh no, worse than that for us. we have memoizing descriptors that use __dict__. not sure if you will hit them in that list you have above but definitely a lot of classes can't be slotted in SQLAlchemy without changing those memoizing descriptors to another approach that is much more painful (MemoizedSlots). otherwise every class in the whole library would have an unconditional __slots__ stuck on it, that would be easy.

    After having fixed slots in the returns library, I decided to build a reusable checker for this: https://github.com/ariebovenberg/slotscheck. Let me know if you have any feedback.

    i dont have a lot of cycles to take a look but overall we would need a tool that traverses hierarchies and sees if a subclass has __slots__, then it walks all the way up to see that every superclass also has it. otherwise we take __slots__ off that subclass if slots can't be applied to the superclasses, and the test would skip.

  6. sqla-tester commented on Jan 18, 2022

    @sqla-tester
    Collaborator

    Mike Bayer referenced this issue:

    change AliasedInsp to use slots https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/3532

  7. sqla-tester commented on Jan 20, 2022

    @sqla-tester
    Collaborator

    Arie Bovenberg has proposed a fix for this issue in the main branch:

    Fix overlapping slots, base classes without slots https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/3548

  8. added a commit that references this issue on Feb 15, 2022
    76abc4a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingperformancewhere performance can be improved. add "bug" only if it's a performance degradation

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions