diff --git a/oslo_utils/reflection.py b/oslo_utils/reflection.py index 02f0c30a..486e75c9 100644 --- a/oslo_utils/reflection.py +++ b/oslo_utils/reflection.py @@ -30,7 +30,7 @@ except AttributeError: _BUILTIN_MODULES = ('builtins', '__builtin__', '__builtins__', 'exceptions') -def _get_members(obj, exclude_hidden): +def get_members(obj, exclude_hidden=True): """Yields the members of an object, filtering by hidden/not hidden.""" for (name, value) in inspect.getmembers(obj): if name.startswith("_") and exclude_hidden: @@ -40,7 +40,8 @@ def _get_members(obj, exclude_hidden): def get_member_names(obj, exclude_hidden=True): """Get all the member names for a object.""" - return [name for (name, _obj) in _get_members(obj, exclude_hidden)] + return [name for (name, _obj) in + get_members(obj, exclude_hidden=exclude_hidden)] def get_class_name(obj, fully_qualified=True): diff --git a/oslo_utils/tests/test_reflection.py b/oslo_utils/tests/test_reflection.py index d66f438c..fce0f70b 100644 --- a/oslo_utils/tests/test_reflection.py +++ b/oslo_utils/tests/test_reflection.py @@ -50,6 +50,14 @@ def function_with_kwargs(a, b, **kwargs): pass +class TestObject(object): + def _hello(self): + pass + + def hi(self): + pass + + class Class(object): def method(self, c, d): @@ -74,6 +82,29 @@ class ClassWithInit(object): pass +class MemberGetTest(test_base.BaseTestCase): + def test_get_members_exclude_hidden(self): + obj = TestObject() + members = list(reflection.get_members(obj, exclude_hidden=True)) + self.assertEqual(1, len(members)) + + def test_get_members_no_exclude_hidden(self): + obj = TestObject() + members = list(reflection.get_members(obj, exclude_hidden=False)) + self.assertGreater(len(members), 1) + + def test_get_members_names_exclude_hidden(self): + obj = TestObject() + members = list(reflection.get_member_names(obj, exclude_hidden=True)) + self.assertEqual(["hi"], members) + + def test_get_members_names_no_exclude_hidden(self): + obj = TestObject() + members = list(reflection.get_member_names(obj, exclude_hidden=False)) + members = [member for member in members if not member.startswith("__")] + self.assertEqual(["_hello", "hi"], sorted(members)) + + class CallbackEqualityTest(test_base.BaseTestCase): def test_different_simple_callbacks(self):