From 541d3c0e4b454cf798ed366e73652fca0d358d63 Mon Sep 17 00:00:00 2001 From: Roman Mogilatov Date: Wed, 22 Jul 2015 10:53:16 +0300 Subject: [PATCH] Refactoring of utils checks --- objects/injections.py | 8 ++++---- objects/providers.py | 2 +- objects/utils.py | 10 +++++----- tests/test_utils.py | 20 ++++++++++++++++++++ 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/objects/injections.py b/objects/injections.py index c574071b..3bb70df7 100644 --- a/objects/injections.py +++ b/objects/injections.py @@ -7,7 +7,7 @@ class Injection(object): """Base injection class.""" - __IS_OBJECTS_INJECTION__ = True + __IS_INJECTION__ = True __slots__ = ('name', 'injectable') def __init__(self, name, injectable): @@ -27,18 +27,18 @@ class KwArg(Injection): """Keyword argument injection.""" - __IS_OBJECTS_KWARG_INJECTION__ = True + __IS_KWARG_INJECTION__ = True class Attribute(Injection): """Attribute injection.""" - __IS_OBJECTS_ATTRIBUTE_INJECTION__ = True + __IS_ATTRIBUTE_INJECTION__ = True class Method(Injection): """Method injection.""" - __IS_OBJECTS_METHOD_INJECTION__ = True + __IS_METHOD_INJECTION__ = True diff --git a/objects/providers.py b/objects/providers.py index 16fca49e..99a33350 100644 --- a/objects/providers.py +++ b/objects/providers.py @@ -14,7 +14,7 @@ class Provider(object): """Base provider class.""" - __IS_OBJECTS_PROVIDER__ = True + __IS_PROVIDER__ = True __slots__ = ('overridden',) def __init__(self): diff --git a/objects/utils.py b/objects/utils.py index 89319021..392fad96 100644 --- a/objects/utils.py +++ b/objects/utils.py @@ -8,7 +8,7 @@ from .errors import Error def is_provider(instance): """Check if instance is provider instance.""" return (not isinstance(instance, class_types) and - hasattr(instance, '__IS_OBJECTS_PROVIDER__')) + getattr(instance, '__IS_PROVIDER__', False) is True) def ensure_is_provider(instance): @@ -22,7 +22,7 @@ def ensure_is_provider(instance): def is_injection(instance): """Check if instance is injection instance.""" return (not isinstance(instance, class_types) and - hasattr(instance, '__IS_OBJECTS_INJECTION__')) + getattr(instance, '__IS_INJECTION__', False) is True) def ensure_is_injection(instance): @@ -36,16 +36,16 @@ def ensure_is_injection(instance): def is_kwarg_injection(instance): """Check if instance is keyword argument injection instance.""" return (not isinstance(instance, class_types) and - hasattr(instance, '__IS_OBJECTS_KWARG_INJECTION__')) + getattr(instance, '__IS_KWARG_INJECTION__', False) is True) def is_attribute_injection(instance): """Check if instance is attribute injection instance.""" return (not isinstance(instance, class_types) and - hasattr(instance, '__IS_OBJECTS_ATTRIBUTE_INJECTION__')) + getattr(instance, '__IS_ATTRIBUTE_INJECTION__', False) is True) def is_method_injection(instance): """Check if instance is method injection instance.""" return (not isinstance(instance, class_types) and - hasattr(instance, '__IS_OBJECTS_METHOD_INJECTION__')) + getattr(instance, '__IS_METHOD_INJECTION__', False) is True) diff --git a/tests/test_utils.py b/tests/test_utils.py index f4b038ce..3de7e5e7 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -40,6 +40,26 @@ class IsProviderTests(unittest.TestCase): """Test with object.""" self.assertFalse(is_provider(object())) + def test_with_subclass_instance(self): + """Test with subclass of provider instance.""" + class SomeProvider(Provider): + + """Some provider for test.""" + + self.assertTrue(is_provider(SomeProvider())) + + def test_with_class_with_getattr(self): + """Test with class that has __getattr__() method implementation.""" + class SomeClass(object): + + """Some test class with __getattr__() method implementation.""" + + def __getattr__(self, _): + """Test implementation that just returns False.""" + return False + + self.assertFalse(is_provider(SomeClass())) + class EnsureIsProviderTests(unittest.TestCase):