Repository navigation
Closed
Labels
bugSomething isn't workingSomething isn't workingperformancewhere performance can be improved. add "bug" only if it's a performance degradationwhere performance can be improved. add "bug" only if it's a performance degradation
Milestone
Description
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.
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't workingperformancewhere performance can be improved. add "bug" only if it's a performance degradationwhere performance can be improved. add "bug" only if it's a performance degradation
Activity
zzzeek commentedon Jan 3, 2022
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.ariebovenberg commentedon Jan 3, 2022
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
returnslibrary, I decided to build a reusable checker for this: https://github.com/ariebovenberg/slotscheck. Let me know if you have any feedback.zzzeek commentedon Jan 3, 2022
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.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.sqla-tester commentedon Jan 18, 2022
Mike Bayer referenced this issue:
change AliasedInsp to use slots https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/3532
sqla-tester commentedon Jan 20, 2022
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
Add new infrastructure to support greater use of __slots__
Fix overlapping slots, base classes without slots