From 53f692a78c4f888a03322f98fa1ef7175bd8a38d Mon Sep 17 00:00:00 2001 From: Roman Mogilatov Date: Fri, 23 Oct 2015 18:07:52 +0300 Subject: [PATCH] Fix bug with injecting entities with __getattr__ implementations --- dependency_injector/__init__.py | 2 +- dependency_injector/utils.py | 12 ++++++++++-- docs/main/changelog.rst | 4 ++++ examples/advanced_usage/inject_flask.py | 4 +++- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/dependency_injector/__init__.py b/dependency_injector/__init__.py index e0d5cead..fbf262bc 100644 --- a/dependency_injector/__init__.py +++ b/dependency_injector/__init__.py @@ -39,7 +39,7 @@ from .utils import ensure_is_catalog_bundle from .errors import Error -VERSION = '0.10.1' +VERSION = '0.10.2' __all__ = ( diff --git a/dependency_injector/utils.py b/dependency_injector/utils.py index 903afc8b..ecf77164 100644 --- a/dependency_injector/utils.py +++ b/dependency_injector/utils.py @@ -13,7 +13,8 @@ GLOBAL_LOCK = threading.RLock() def is_provider(instance): """Check if instance is provider instance.""" return (not isinstance(instance, six.class_types) and - getattr(instance, '__IS_PROVIDER__', False) is True) + hasattr(instance, '__IS_PROVIDER__') and + getattr(instance, '__IS_PROVIDER__') is True) def ensure_is_provider(instance): @@ -30,7 +31,8 @@ def ensure_is_provider(instance): def is_injection(instance): """Check if instance is injection instance.""" return (not isinstance(instance, six.class_types) and - getattr(instance, '__IS_INJECTION__', False) is True) + hasattr(instance, '__IS_INJECTION__') and + getattr(instance, '__IS_INJECTION__') is True) def ensure_is_injection(instance): @@ -44,36 +46,42 @@ def ensure_is_injection(instance): def is_arg_injection(instance): """Check if instance is positional argument injection instance.""" return (not isinstance(instance, six.class_types) and + hasattr(instance, '__IS_ARG_INJECTION__') and getattr(instance, '__IS_ARG_INJECTION__', False) is True) def is_kwarg_injection(instance): """Check if instance is keyword argument injection instance.""" return (not isinstance(instance, six.class_types) and + hasattr(instance, '__IS_KWARG_INJECTION__') and getattr(instance, '__IS_KWARG_INJECTION__', False) is True) def is_attribute_injection(instance): """Check if instance is attribute injection instance.""" return (not isinstance(instance, six.class_types) and + hasattr(instance, '__IS_ATTRIBUTE_INJECTION__') and getattr(instance, '__IS_ATTRIBUTE_INJECTION__', False) is True) def is_method_injection(instance): """Check if instance is method injection instance.""" return (not isinstance(instance, six.class_types) and + hasattr(instance, '__IS_METHOD_INJECTION__') and getattr(instance, '__IS_METHOD_INJECTION__', False) is True) def is_catalog(instance): """Check if instance is catalog instance.""" return (isinstance(instance, six.class_types) and + hasattr(instance, '__IS_CATALOG__') and getattr(instance, '__IS_CATALOG__', False) is True) def is_catalog_bundle(instance): """Check if instance is catalog bundle instance.""" return (not isinstance(instance, six.class_types) and + hasattr(instance, '__IS_CATALOG_BUNDLE__') and getattr(instance, '__IS_CATALOG_BUNDLE__', False) is True) diff --git a/docs/main/changelog.rst b/docs/main/changelog.rst index b02dd808..fca85915 100644 --- a/docs/main/changelog.rst +++ b/docs/main/changelog.rst @@ -7,6 +7,10 @@ that were made in every particular version. From version 0.7.6 *Dependency Injector* framework strictly follows `Semantic versioning`_ +0.10.2 +------ +- Fix bug with injecting entities that implement ``__getattr__``. + 0.10.1 ------ - Update some examples. diff --git a/examples/advanced_usage/inject_flask.py b/examples/advanced_usage/inject_flask.py index ecb6edc9..585edc8d 100644 --- a/examples/advanced_usage/inject_flask.py +++ b/examples/advanced_usage/inject_flask.py @@ -16,8 +16,10 @@ app = flask.Flask(__name__) @app.route('/') @di.inject(database) -def hello(database): +@di.inject(flask.request) +def hello(request, database): """Example Flask view.""" + print request one = database.execute('SELECT 1').fetchone()[0] return 'Query returned {0}, db connection {1}'.format(one, database)