summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy
diff options
context:
space:
mode:
authorjonathan vanasco <jonathan@2xlp.com>2020-04-20 13:56:49 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2020-08-25 11:02:43 -0400
commitaff9d06e00019ee171ea98fbe9ae1687c53a3e25 (patch)
treed6c4edfc264fad7aac7c791b5f0773212097c9f2 /lib/sqlalchemy
parenta1a081398e3b0fb9ce2703ae862778b55b735bc9 (diff)
downloadsqlalchemy-aff9d06e00019ee171ea98fbe9ae1687c53a3e25.tar.gz
More descriptive error for non-mapped string prop name
Fixed issue where using a loader option against a string attribute name that is not actually a mapped attribute, such as a plain Python descriptor, would raise an uninformative AttributeError; a descriptive error is now raised. Fixes: #4589 Closes: #4594 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/4594 Pull-request-sha: 2b7ed5240f49be90f9390e3d041c9cb957083465 Change-Id: I66b9937991eb7cdbe074a92f490af1c80d16449e
Diffstat (limited to 'lib/sqlalchemy')
-rw-r--r--lib/sqlalchemy/orm/strategy_options.py18
1 files changed, 17 insertions, 1 deletions
diff --git a/lib/sqlalchemy/orm/strategy_options.py b/lib/sqlalchemy/orm/strategy_options.py
index b3913ec5b..6c6f0307d 100644
--- a/lib/sqlalchemy/orm/strategy_options.py
+++ b/lib/sqlalchemy/orm/strategy_options.py
@@ -15,6 +15,7 @@ from .base import _is_aliased_class
from .base import _is_mapped_class
from .base import InspectionAttr
from .interfaces import LoaderOption
+from .interfaces import MapperProperty
from .interfaces import PropComparator
from .path_registry import _DEFAULT_TOKEN
from .path_registry import _WILDCARD_TOKEN
@@ -170,6 +171,7 @@ class Load(Generative, LoaderOption):
if isinstance(attr, util.string_types):
default_token = attr.endswith(_DEFAULT_TOKEN)
+ attr_str_name = attr
if attr.endswith(_WILDCARD_TOKEN) or default_token:
if default_token:
self.propagate_to_loaders = False
@@ -207,7 +209,21 @@ class Load(Generative, LoaderOption):
else:
return None
else:
- attr = found_property = attr.property
+ try:
+ attr = found_property = attr.property
+ except AttributeError as ae:
+ if not isinstance(attr, MapperProperty):
+ util.raise_(
+ sa_exc.ArgumentError(
+ 'Expected attribute "%s" on %s to be a '
+ "mapped attribute; "
+ "instead got %s object."
+ % (attr_str_name, ent, type(attr))
+ ),
+ replace_context=ae,
+ )
+ else:
+ raise
path = path[attr]
elif _is_mapped_class(attr):