From c2877777afc0835de1b1ba2007e9ee7b85964135 Mon Sep 17 00:00:00 2001 From: Roman Mogylatov Date: Sun, 27 Mar 2022 14:18:46 -0400 Subject: [PATCH] Refactor + add tests to #569 --- src/dependency_injector/wiring.py | 15 ++++++++------- tests/unit/samples/wiring/imports.py | 3 ++- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/dependency_injector/wiring.py b/src/dependency_injector/wiring.py index 927796bc..42882533 100644 --- a/src/dependency_injector/wiring.py +++ b/src/dependency_injector/wiring.py @@ -321,13 +321,7 @@ class InspectFilter: def _is_starlette_request_cls(self, instance: object) -> bool: return starlette \ and isinstance(instance, type) \ - and self._safe_is_subclass(instance, starlette.requests.Request) - - def _safe_is_subclass(self, instance: type, cls: type) -> bool: - try: - return issubclass(instance, cls) - except TypeError: - return False + and _safe_is_subclass(instance, starlette.requests.Request) def _is_builtin(self, instance: object) -> bool: return inspect.isbuiltin(instance) @@ -685,6 +679,13 @@ def _is_declarative_container(instance: Any) -> bool: and getattr(instance, "declarative_parent", None) is None) +def _safe_is_subclass(instance: Any, cls: Type) -> bool: + try: + return issubclass(instance, cls) + except TypeError: + return False + + class Modifier: def modify( diff --git a/tests/unit/samples/wiring/imports.py b/tests/unit/samples/wiring/imports.py index ecab7ce8..7416bb91 100644 --- a/tests/unit/samples/wiring/imports.py +++ b/tests/unit/samples/wiring/imports.py @@ -5,7 +5,8 @@ import sys if "pypy" not in sys.version.lower(): import numpy # noqa from numpy import * # noqa - from numpy.typing import * # noqa + if sys.version_info >= (3, 7): + from numpy.typing import * # noqa import scipy # noqa from scipy import * # noqa