From 54a713959fc59bb1c6f298f4071612450a9c7b45 Mon Sep 17 00:00:00 2001 From: Konsta Vesterinen Date: Wed, 10 Dec 2014 09:44:39 +0200 Subject: [PATCH] Make getdotattr always return a flat list --- sqlalchemy_utils/functions/orm.py | 8 +------- tests/functions/test_getdotattr.py | 5 +++-- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/sqlalchemy_utils/functions/orm.py b/sqlalchemy_utils/functions/orm.py index 05a70e3..86ba278 100644 --- a/sqlalchemy_utils/functions/orm.py +++ b/sqlalchemy_utils/functions/orm.py @@ -644,13 +644,7 @@ def getdotattr(obj_or_class, dot_path): for path in dot_path.split('.'): getter = attrgetter(path) if isinstance(last, list): - tmp = [] - for el in last: - if isinstance(el, list): - tmp.extend(map(getter, el)) - else: - tmp.append(getter(el)) - last = tmp + last = sum((getter(el) for el in last), []) elif isinstance(last, InstrumentedAttribute): last = getter(last.property.mapper.class_) elif last is None: diff --git a/tests/functions/test_getdotattr.py b/tests/functions/test_getdotattr.py index 36f082f..6dae397 100644 --- a/tests/functions/test_getdotattr.py +++ b/tests/functions/test_getdotattr.py @@ -67,11 +67,12 @@ class TestGetDotAttr(TestCase): subsection = self.SubSection(section=section) subsubsection = self.SubSubSection(subsection=subsection) + assert getdotattr(document, 'sections') == [section] assert getdotattr(document, 'sections.subsections') == [ - [subsection] + subsection ] assert getdotattr(document, 'sections.subsections.subsubsections') == [ - [subsubsection] + subsubsection ] def test_class_paths(self):