dinkleberg 0.3.0__py3-none-any.whl → 0.3.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,7 @@
1
1
  import asyncio
2
2
  import inspect
3
3
  import logging
4
+ from functools import wraps
4
5
  from inspect import Signature
5
6
  from types import MappingProxyType
6
7
  from typing import AsyncGenerator, Callable, overload, get_type_hints, Mapping, get_origin
@@ -135,9 +136,15 @@ class DependencyConfigurator(DependencyScope):
135
136
 
136
137
  # TODO circular dependency detection
137
138
  # TODO singleton race condition prevention (async.Lock)
138
- async def resolve[T](self, t: type[T], **kwargs) -> T:
139
+ async def resolve[T](self, t: type[T] | Callable, **kwargs) -> T:
139
140
  self._raise_if_closed()
140
141
 
142
+ if not inspect.isclass(t):
143
+ if not inspect.isfunction(t):
144
+ raise ValueError(f'Cannot resolve type {t}. Only classes and functions are supported.')
145
+
146
+ return self._wrap_func(t)
147
+
141
148
  if t == DependencyScope:
142
149
  return self
143
150
 
@@ -249,6 +256,28 @@ class DependencyConfigurator(DependencyScope):
249
256
 
250
257
  return actual_kwargs
251
258
 
259
+ def _wrap_func(self, func: Callable):
260
+ signature = inspect.signature(func)
261
+
262
+ dep_params = MappingProxyType({
263
+ param_name: param
264
+ for param_name, param in signature.parameters.items()
265
+ if isinstance(param.default, Dependency)
266
+ })
267
+
268
+ if not dep_params:
269
+ return func
270
+
271
+ if not asyncio.iscoroutinefunction(func):
272
+ raise NotImplementedError('Synchronous functions with Dependency() defaults are not supported.')
273
+
274
+ @wraps(func)
275
+ async def wrapped_func(*args, **kwargs):
276
+ new_kwargs = await self._resolve_kwargs(signature, func.__name__, args, kwargs, dep_params)
277
+ return await func(*args, **new_kwargs)
278
+
279
+ return wrapped_func
280
+
252
281
  # TODO handle __slots__
253
282
  def _wrap_instance(self, instance):
254
283
  if getattr(instance, '__dinkleberg__', False):
@@ -256,25 +285,11 @@ class DependencyConfigurator(DependencyScope):
256
285
 
257
286
  methods = get_public_methods(instance)
258
287
  for name, value in methods:
259
- signature = inspect.signature(value)
260
-
261
- dep_params = MappingProxyType({
262
- param_name: param
263
- for param_name, param in signature.parameters.items()
264
- if isinstance(param.default, Dependency)
265
- })
266
- if not dep_params:
267
- continue
268
-
269
288
  instance_method = getattr(instance, name)
270
- if asyncio.iscoroutinefunction(instance_method):
271
- async def wrapped_method(*args, __m=instance_method, __s=signature, __n=name, __d=dep_params, **kwargs):
272
- new_kwargs = await self._resolve_kwargs(__s, __n, args, kwargs, __d)
273
- return await __m(*args, **new_kwargs)
274
289
 
275
- setattr(instance, name, wrapped_method)
276
- else:
277
- raise NotImplementedError('Synchronous methods with Dependency() defaults are not supported.')
290
+ wrapped_method = self._wrap_func(instance_method)
291
+
292
+ setattr(instance, name, wrapped_method)
278
293
 
279
294
  try:
280
295
  setattr(instance, '__dinkleberg__', True)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dinkleberg
3
- Version: 0.3.0
3
+ Version: 0.3.1
4
4
  Summary: Your friendly neighbour when it comes to dependency management.
5
5
  Project-URL: Homepage, https://github.com/DavidVollmers/dinkleberg
6
6
  Project-URL: Documentation, https://github.com/DavidVollmers/dinkleberg/blob/main/libs/dinkleberg/README.md
@@ -1,9 +1,9 @@
1
1
  dinkleberg/__init__.py,sha256=6gowHoL3xTWd2iyU09upgjXKUv_WWp6Tw1iqw2uz7Vc,192
2
- dinkleberg/dependency_configurator.py,sha256=vsXTHFUkhW5_bR95qFYOzaio-UwupIkS9qjPL2e8s4o,14356
2
+ dinkleberg/dependency_configurator.py,sha256=gSuizzr28qwO9Q5Z1IYytxaIRB5dAAKiPKe3GozKUuk,14660
3
3
  dinkleberg/descriptor.py,sha256=ZtVwJihvCLugakHx201iX7Jm4A50Z0SzPu9zeHfD320,349
4
4
  dinkleberg/typing.py,sha256=qAdSQomntQfI4bSWc9X32DXGftuyj56WP7nG3Pia7ZE,942
5
5
  dinkleberg/fastapi/__init__.py,sha256=boj1ywpWhuuOjHLwdUha2EPBG2f1zbc374N35k1CJxk,352
6
- dinkleberg-0.3.0.dist-info/METADATA,sha256=sGBRFKAmbtQS1vc79a5tyotUivlciNlhZAbwYqSC1FQ,1555
7
- dinkleberg-0.3.0.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
8
- dinkleberg-0.3.0.dist-info/top_level.txt,sha256=6TjbaJ_eyRzoxzz2r1Eu0hgYfxBBM2bOV3u_TJ8yjjw,11
9
- dinkleberg-0.3.0.dist-info/RECORD,,
6
+ dinkleberg-0.3.1.dist-info/METADATA,sha256=zK12ZPnFptOjz9QiF8i13yqHnG2UtWz1qSMzUkzE8rQ,1555
7
+ dinkleberg-0.3.1.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
8
+ dinkleberg-0.3.1.dist-info/top_level.txt,sha256=6TjbaJ_eyRzoxzz2r1Eu0hgYfxBBM2bOV3u_TJ8yjjw,11
9
+ dinkleberg-0.3.1.dist-info/RECORD,,