• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ets-labs / python-dependency-injector / 13524441259

23 Feb 2025 05:22PM UTC coverage: 93.883%. Remained the same
13524441259

push

github

ZipFile
Bump version to v4.46.0

3361 of 3580 relevant lines covered (93.88%)

0.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.34
/src/dependency_injector/_cwiring.pyx
1
"""Wiring optimizations module."""
1✔
2

3
import asyncio
1✔
4
import collections.abc
1✔
5
import inspect
1✔
6
import types
1✔
7

8
from .wiring import _Marker
1✔
9

10
from .providers cimport Provider, Resource
11

12

13
def _sync_inject(object fn, tuple args, dict kwargs, dict injections, dict closings, /):
1✔
14
    cdef object result
15
    cdef dict to_inject
16
    cdef object arg_key
17
    cdef Provider provider
18

19
    to_inject = kwargs.copy()
1✔
20
    for arg_key, provider in injections.items():
1✔
21
        if arg_key not in kwargs or isinstance(kwargs[arg_key], _Marker):
1✔
22
            to_inject[arg_key] = provider()
1✔
23

24
    result = fn(*args, **to_inject)
1✔
25

26
    if closings:
1✔
27
        for arg_key, provider in closings.items():
1✔
28
            if arg_key in kwargs and not isinstance(kwargs[arg_key], _Marker):
1✔
29
                continue
1✔
30
            if not isinstance(provider, Resource):
1✔
31
                continue
1✔
32
            provider.shutdown()
1✔
33

34
    return result
1✔
35

36

37
async def _async_inject(object fn, tuple args, dict kwargs, dict injections, dict closings, /):
1✔
38
    cdef object result
39
    cdef dict to_inject
40
    cdef list to_inject_await = []
1✔
41
    cdef list to_close_await = []
1✔
42
    cdef object arg_key
43
    cdef Provider provider
44

45
    to_inject = kwargs.copy()
1✔
46
    for arg_key, provider in injections.items():
1✔
47
        if arg_key not in kwargs or isinstance(kwargs[arg_key], _Marker):
1✔
48
            provide = provider()
1✔
49
            if provider.is_async_mode_enabled():
1✔
50
                to_inject_await.append((arg_key, provide))
1✔
51
            elif _isawaitable(provide):
1✔
52
                to_inject_await.append((arg_key, provide))
×
53
            else:
54
                to_inject[arg_key] = provide
1✔
55

56
    if to_inject_await:
1✔
57
        async_to_inject = await asyncio.gather(*(provide for _, provide in to_inject_await))
1✔
58
        for provide, (injection, _) in zip(async_to_inject, to_inject_await):
1✔
59
            to_inject[injection] = provide
1✔
60

61
    result = await fn(*args, **to_inject)
1✔
62

63
    if closings:
1✔
64
        for arg_key, provider in closings.items():
1✔
65
            if arg_key in kwargs and isinstance(kwargs[arg_key], _Marker):
1✔
66
                continue
1✔
67
            if not isinstance(provider, Resource):
1✔
68
                continue
×
69
            shutdown = provider.shutdown()
1✔
70
            if _isawaitable(shutdown):
1✔
71
                to_close_await.append(shutdown)
1✔
72

73
        await asyncio.gather(*to_close_await)
1✔
74

75
    return result
1✔
76

77

78
cdef bint _isawaitable(object instance):
1✔
79
    """Return true if object can be passed to an ``await`` expression."""
80
    return (isinstance(instance, types.CoroutineType) or
1✔
81
            isinstance(instance, types.GeneratorType) and
1✔
82
            bool(instance.gi_code.co_flags & inspect.CO_ITERABLE_COROUTINE) or
×
83
            isinstance(instance, collections.abc.Awaitable))
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc