From 4cde5438a557af2bc9756e0bebf0661a9205d44c Mon Sep 17 00:00:00 2001 From: Konsta Vesterinen Date: Tue, 28 Jul 2015 10:56:19 +0300 Subject: [PATCH] Add class alias support * Add class alias support for get_hybrid_properties function --- CHANGES.rst | 6 ++++++ sqlalchemy_utils/__init__.py | 2 +- sqlalchemy_utils/functions/orm.py | 15 ++++++++++++++- tests/functions/test_get_hybrid_properties.py | 7 +++++++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index da0225e..cda33e4 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,6 +4,12 @@ Changelog Here you can see the full list of changes between each SQLAlchemy-Utils release. +0.30.15 (2015-07-28) +^^^^^^^^^^^^^^^^^^^^ + +- Added support for aliased classes in get_hybrid_properties utility function + + 0.30.14 (2015-07-23) ^^^^^^^^^^^^^^^^^^^^ diff --git a/sqlalchemy_utils/__init__.py b/sqlalchemy_utils/__init__.py index d126cc2..436703e 100644 --- a/sqlalchemy_utils/__init__.py +++ b/sqlalchemy_utils/__init__.py @@ -92,4 +92,4 @@ from .types import ( # noqa WeekDaysType ) -__version__ = '0.30.14' +__version__ = '0.30.15' diff --git a/sqlalchemy_utils/functions/orm.py b/sqlalchemy_utils/functions/orm.py index f067276..5621440 100644 --- a/sqlalchemy_utils/functions/orm.py +++ b/sqlalchemy_utils/functions/orm.py @@ -734,14 +734,27 @@ def get_hybrid_properties(model): get_hybrid_properties(Category).keys() # ['lowercase_name'] + This function also supports aliased classes + + :: + + + get_hybrid_properties( + sa.orm.aliased(Category) + ).keys() # ['lowercase_name'] + + .. versionchanged: 0.26.7 This function now returns a dictionary instead of generator + .. versionchanged: 0.30.15 + Added support for aliased classes + :param model: SQLAlchemy declarative model or mapper """ return dict( (key, prop) - for key, prop in sa.inspect(model).all_orm_descriptors.items() + for key, prop in get_mapper(model).all_orm_descriptors.items() if isinstance(prop, hybrid_property) ) diff --git a/tests/functions/test_get_hybrid_properties.py b/tests/functions/test_get_hybrid_properties.py index a37a664..13221aa 100644 --- a/tests/functions/test_get_hybrid_properties.py +++ b/tests/functions/test_get_hybrid_properties.py @@ -35,3 +35,10 @@ class TestGetHybridProperties(object): list(get_hybrid_properties(sa.inspect(self.Category)).keys()) == ['lowercase_name'] ) + + def test_aliased_class(self): + assert ( + list(get_hybrid_properties(sa.orm.aliased(self.Category)).keys()) + == + ['lowercase_name'] + )