|
1 | 1 | """Containers module.""" |
2 | 2 |
|
3 | | -import asyncio |
4 | | -import contextlib |
5 | | -import copy as copy_module |
6 | | -import json |
7 | | -import importlib |
8 | | -import inspect |
| 3 | +from asyncio import gather |
| 4 | +from contextlib import suppress |
| 5 | +from copy import deepcopy as copy_deepcopy |
| 6 | +from importlib import import_module |
| 7 | +from inspect import getmodule, stack as inspect_stack |
| 8 | +from json import load as json_load |
9 | 9 |
|
10 | 10 | try: |
11 | 11 | import yaml |
12 | 12 | except ImportError: |
13 | 13 | yaml = None |
14 | 14 |
|
15 | | -from . import providers, errors |
| 15 | +from . import errors, providers |
| 16 | + |
16 | 17 | from .providers cimport __is_future_or_coroutine |
17 | | -from .wiring import wire, unwire |
| 18 | + |
| 19 | +from .wiring import unwire, wire |
18 | 20 |
|
19 | 21 |
|
20 | 22 | class WiringConfiguration: |
@@ -113,7 +115,7 @@ class DynamicContainer(Container): |
113 | 115 |
|
114 | 116 | copied.provider_type = providers.Provider |
115 | 117 | copied.overridden = providers.deepcopy(self.overridden, memo) |
116 | | - copied.wiring_config = copy_module.deepcopy(self.wiring_config, memo) |
| 118 | + copied.wiring_config = copy_deepcopy(self.wiring_config, memo) |
117 | 119 | copied.declarative_parent = self.declarative_parent |
118 | 120 |
|
119 | 121 | for name, provider in providers.deepcopy(self.providers, memo).items(): |
@@ -301,10 +303,10 @@ class DynamicContainer(Container): |
301 | 303 | from_package = self.wiring_config.from_package |
302 | 304 | elif self.declarative_parent is not None \ |
303 | 305 | and (self.wiring_config.modules or self.wiring_config.packages): |
304 | | - with contextlib.suppress(Exception): |
| 306 | + with suppress(Exception): |
305 | 307 | from_package = _resolve_package_name_from_cls(self.declarative_parent) |
306 | 308 | else: |
307 | | - with contextlib.suppress(Exception): |
| 309 | + with suppress(Exception): |
308 | 310 | from_package = _resolve_calling_package_name() |
309 | 311 |
|
310 | 312 | modules = _resolve_string_imports(modules, from_package) |
@@ -354,7 +356,7 @@ class DynamicContainer(Container): |
354 | 356 | futures.append(resource) |
355 | 357 |
|
356 | 358 | if futures: |
357 | | - return asyncio.gather(*futures) |
| 359 | + return gather(*futures) |
358 | 360 |
|
359 | 361 | def shutdown_resources(self, resource_type=providers.BaseResource): |
360 | 362 | """Shutdown all container resources.""" |
@@ -382,7 +384,7 @@ class DynamicContainer(Container): |
382 | 384 | result = resource.shutdown() |
383 | 385 | if __is_future_or_coroutine(result): |
384 | 386 | futures.append(result) |
385 | | - await asyncio.gather(*futures) |
| 387 | + await gather(*futures) |
386 | 388 |
|
387 | 389 | def _sync_ordered_shutdown(resources): |
388 | 390 | while any(resource.initialized for resource in resources): |
@@ -469,7 +471,7 @@ class DynamicContainer(Container): |
469 | 471 | def from_json_schema(self, filepath): |
470 | 472 | """Build container providers from JSON schema.""" |
471 | 473 | with open(filepath) as file: |
472 | | - schema = json.load(file) |
| 474 | + schema = json_load(file) |
473 | 475 | self.from_schema(schema) |
474 | 476 |
|
475 | 477 | def resolve_provider_name(self, provider): |
@@ -735,7 +737,7 @@ class DeclarativeContainer(Container, metaclass=DeclarativeContainerMetaClass): |
735 | 737 | """ |
736 | 738 | container = cls.instance_type() |
737 | 739 | container.provider_type = cls.provider_type |
738 | | - container.wiring_config = copy_module.deepcopy(cls.wiring_config) |
| 740 | + container.wiring_config = copy_deepcopy(cls.wiring_config) |
739 | 741 | container.declarative_parent = cls |
740 | 742 |
|
741 | 743 | copied_providers = providers.deepcopy({ **cls.providers, **{"@@self@@": cls.__self__}}) |
@@ -924,18 +926,18 @@ cpdef bint _any_relative_string_imports_in(object modules): |
924 | 926 |
|
925 | 927 | cpdef list _resolve_string_imports(object modules, object from_package): |
926 | 928 | return [ |
927 | | - importlib.import_module(module, from_package) if isinstance(module, str) else module |
| 929 | + import_module(module, from_package) if isinstance(module, str) else module |
928 | 930 | for module in modules |
929 | 931 | ] |
930 | 932 |
|
931 | 933 |
|
932 | 934 | cpdef object _resolve_calling_package_name(): |
933 | | - stack = inspect.stack() |
| 935 | + stack = inspect_stack() |
934 | 936 | pre_last_frame = stack[0] |
935 | | - module = inspect.getmodule(pre_last_frame[0]) |
| 937 | + module = getmodule(pre_last_frame[0]) |
936 | 938 | return module.__package__ |
937 | 939 |
|
938 | 940 |
|
939 | 941 | cpdef object _resolve_package_name_from_cls(cls): |
940 | | - module = importlib.import_module(cls.__module__) |
| 942 | + module = import_module(cls.__module__) |
941 | 943 | return module.__package__ |
0 commit comments