diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2021-04-18 20:30:14 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2021-04-18 20:31:27 -0400 |
| commit | fa817cc0ea21a4b28b7a076eab0b42010279fbc9 (patch) | |
| tree | dffbe841faa45b1631370d43ec13645cf64e55f0 /lib/sqlalchemy | |
| parent | 8eb4ea7f609052333514919d38fd98780d57f243 (diff) | |
| download | sqlalchemy-fa817cc0ea21a4b28b7a076eab0b42010279fbc9.tar.gz | |
Bypass declared_attr w/ a custom wrapper for lambda criteria
Fixed bug in new :func:`_orm.with_loader_criteria` feature where using a
mixin class with :func:`_orm.declared_attr` on an attribute that were
accessed inside the custom lambda would emit a warning regarding using an
unmapped declared attr, when the lambda callable were first initialized.
This warning is now prevented using special instrumentation for this
lambda initialization step.
Fixes: #6320
Change-Id: I18ce0c662131f2e683b84caa38c01b2182eb210b
Diffstat (limited to 'lib/sqlalchemy')
| -rw-r--r-- | lib/sqlalchemy/orm/util.py | 34 |
1 files changed, 31 insertions, 3 deletions
diff --git a/lib/sqlalchemy/orm/util.py b/lib/sqlalchemy/orm/util.py index a30f085aa..65fdcf9fc 100644 --- a/lib/sqlalchemy/orm/util.py +++ b/lib/sqlalchemy/orm/util.py @@ -874,6 +874,32 @@ class AliasedInsp( return "aliased(%s)" % (self._target.__name__,) +class _WrapUserEntity(object): + """A wrapper used within the loader_criteria lambda caller so that + we can bypass declared_attr descriptors on unmapped mixins, which + normally emit a warning for such use. + + might also be useful for other per-lambda instrumentations should + the need arise. + + """ + + def __init__(self, subject): + self.subject = subject + + @util.preload_module("sqlalchemy.orm.decl_api") + def __getattribute__(self, name): + decl_api = util.preloaded.orm.decl_api + + subject = object.__getattribute__(self, "subject") + if name in subject.__dict__ and isinstance( + subject.__dict__[name], decl_api.declared_attr + ): + return subject.__dict__[name].fget(subject) + else: + return getattr(subject, name) + + class LoaderCriteriaOption(CriteriaOption): """Add additional WHERE criteria to the load for all occurrences of a particular entity. @@ -1033,9 +1059,11 @@ class LoaderCriteriaOption(CriteriaOption): where_criteria, roles.WhereHavingRole, lambda_args=( - self.root_entity - if self.root_entity is not None - else self.entity.entity, + _WrapUserEntity( + self.root_entity + if self.root_entity is not None + else self.entity.entity, + ), ), opts=lambdas.LambdaOptions( track_closure_variables=track_closure_variables |
