Wiring import fixes numpy scipy (#422)

* Add signature guards

* Fix flake8 errors and update changelog

* Fix slow numpy/scipy installs on pypy3
This commit is contained in:
Roman Mogylatov 2021-03-08 16:32:34 -05:00 committed by GitHub
parent b3bcf60ced
commit bbbed8972a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 54 additions and 2 deletions

View File

@ -7,6 +7,12 @@ that were made in every particular version.
From version 0.7.6 *Dependency Injector* framework strictly From version 0.7.6 *Dependency Injector* framework strictly
follows `Semantic versioning`_ follows `Semantic versioning`_
Development version
-------------------
- Fix wiring to not crash on missing signatures.
See issue: `#420 <https://github.com/ets-labs/python-dependency-injector/issues/420>`_.
Thanks to `@Balthus1989 <https://github.com/Balthus1989>`_ for reporting the issue.
4.29.1 4.29.1
------ ------
- Fix recursive copying issue in ``Delegate`` provider. - Fix recursive copying issue in ``Delegate`` provider.

View File

@ -10,5 +10,7 @@ pyyaml
httpx httpx
fastapi fastapi
pydantic pydantic
numpy
scipy
-r requirements-ext.txt -r requirements-ext.txt

View File

@ -298,6 +298,8 @@ class InspectFilter:
return True return True
elif self._is_starlette_request_cls(instance): elif self._is_starlette_request_cls(instance):
return True return True
elif self._is_builtin(instance):
return True
else: else:
return False return False
@ -309,6 +311,9 @@ class InspectFilter:
and isinstance(instance, type) \ and isinstance(instance, type) \
and issubclass(instance, starlette.requests.Request) and issubclass(instance, starlette.requests.Request)
def _is_builtin(self, instance: object) -> bool:
return inspect.isbuiltin(instance)
def wire( # noqa: C901 def wire( # noqa: C901
container: Container, container: Container,
@ -476,7 +481,7 @@ def _unpatch_attribute(patched: PatchedAttribute) -> None:
setattr(patched.member, patched.name, patched.marker) setattr(patched.member, patched.name, patched.marker)
def _fetch_reference_injections( def _fetch_reference_injections( # noqa: C901
fn: Callable[..., Any], fn: Callable[..., Any],
) -> Tuple[Dict[str, Any], Dict[str, Any]]: ) -> Tuple[Dict[str, Any], Dict[str, Any]]:
# Hotfix, see: # Hotfix, see:
@ -488,7 +493,15 @@ def _fetch_reference_injections(
)): )):
fn = fn.__init__ fn = fn.__init__
try:
signature = inspect.signature(fn) signature = inspect.signature(fn)
except ValueError as exception:
if 'no signature found' in str(exception):
return {}, {}
elif 'not supported by signature' in str(exception):
return {}, {}
else:
raise exception
injections = {} injections = {}
closing = {} closing = {}
@ -874,9 +887,13 @@ class AutoLoader:
super().exec_module(module) super().exec_module(module)
loader.wire_module(module) loader.wire_module(module)
class ExtensionFileLoader(importlib.machinery.ExtensionFileLoader):
...
loader_details = [ loader_details = [
(SourcelessFileLoader, importlib.machinery.BYTECODE_SUFFIXES), (SourcelessFileLoader, importlib.machinery.BYTECODE_SUFFIXES),
(SourceFileLoader, importlib.machinery.SOURCE_SUFFIXES), (SourceFileLoader, importlib.machinery.SOURCE_SUFFIXES),
(ExtensionFileLoader, importlib.machinery.EXTENSION_SUFFIXES),
] ]
self._path_hook = importlib.machinery.FileFinder.path_hook(*loader_details) self._path_hook = importlib.machinery.FileFinder.path_hook(*loader_details)

View File

@ -1,6 +1,7 @@
"""Test module for wiring.""" """Test module for wiring."""
from decimal import Decimal from decimal import Decimal
import sys
from typing import Callable from typing import Callable
from dependency_injector import providers from dependency_injector import providers
@ -128,3 +129,16 @@ def test_class_decorator(service: Service = Provide[Container.service]):
def test_container(container: Container = Provide[Container]): def test_container(container: Container = Provide[Container]):
return container.service() return container.service()
# Import tests
if 'pypy' not in sys.version.lower():
import numpy # noqa
from numpy import * # noqa
import scipy # noqa
from scipy import * # noqa
import builtins # noqa
from builtins import * # noqa

13
tox.ini
View File

@ -8,6 +8,8 @@ deps=
typing_extensions typing_extensions
httpx httpx
fastapi fastapi
numpy
scipy
extras= extras=
yaml yaml
pydantic pydantic
@ -62,6 +64,17 @@ extras=
commands= commands=
python -m unittest discover -s tests/unit -p test_*_py2_py3.py python -m unittest discover -s tests/unit -p test_*_py2_py3.py
[testenv:pypy3]
deps=
httpx
fastapi
extras=
yaml
flask
commands=
python -m unittest discover -s tests/unit -p test_*_py2_py3.py
[testenv:pylint] [testenv:pylint]
deps= deps=
pylint pylint