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
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
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):
},
)
+ class OrderWProp(cls.classes.Order):
+ @property
+ def some_attr(self):
+ return "hi"
+
+ mapper(OrderWProp, None, inherits=cls.classes.Order)
+
class PathTest(object):
def _make_path(self, path):
"relationship",
)
+ def test_gen_path_attr_str_not_mapped(self):
+ OrderWProp = self.classes.OrderWProp
+
+ sess = Session()
+ q = sess.query(OrderWProp).options(defer("some_attr"))
+
+ assert_raises_message(
+ sa.exc.ArgumentError,
+ r"Expected attribute \"some_attr\" on mapped class "
+ "OrderWProp->orders to be a mapped attribute; instead "
+ "got .*property.* object.",
+ q._compile_state,
+ )
+
def test_gen_path_attr_entity_invalid_noraiseerr(self):
User = self.classes.User
Order = self.classes.Order
'column property "Keyword.keywords"',
)
- def test_wrong_type_in_option(self):
+ def test_wrong_type_in_option_cls(self):
Item = self.classes.Item
Keyword = self.classes.Keyword
self._assert_eager_with_entity_exception(
r"mapper option expects string key or list of attributes",
)
+ def test_wrong_type_in_option_descriptor(self):
+ OrderWProp = self.classes.OrderWProp
+
+ self._assert_eager_with_entity_exception(
+ [OrderWProp],
+ (joinedload(OrderWProp.some_attr),),
+ r"mapper option expects string key or list of attributes",
+ )
+
def test_non_contiguous_all_option(self):
User = self.classes.User
self._assert_eager_with_entity_exception(
),
)
+ class OrderWProp(cls.classes.Order):
+ @property
+ def some_attr(self):
+ return "hi"
+
+ mapper(OrderWProp, None, inherits=cls.classes.Order)
+
def _assert_option(self, entity_list, option):
Item = self.classes.Item