anydi 0.25.1__py3-none-any.whl → 0.25.2__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.
anydi/_utils.py CHANGED
@@ -8,7 +8,7 @@ import inspect
8
8
  import sys
9
9
  from typing import Any, AsyncIterator, Callable, ForwardRef, Iterator, TypeVar, cast
10
10
 
11
- from typing_extensions import Annotated, ParamSpec, get_origin
11
+ from typing_extensions import ParamSpec, get_args, get_origin
12
12
 
13
13
  try:
14
14
  import anyio # noqa
@@ -33,27 +33,28 @@ P = ParamSpec("P")
33
33
 
34
34
  def get_full_qualname(obj: Any) -> str:
35
35
  """Get the fully qualified name of an object."""
36
- origin = get_origin(obj)
37
- if origin is Annotated:
38
- metadata = ", ".join(
39
- [
40
- f'"{arg}"' if isinstance(arg, str) else str(arg)
41
- for arg in obj.__metadata__
42
- ]
43
- )
44
- return f"Annotated[{get_full_qualname(obj.__args__[0])}, {metadata}]]"
45
-
46
36
  qualname = getattr(obj, "__qualname__", None)
47
- module_name = getattr(obj, "__module__", None)
37
+ module = getattr(obj, "__module__", None)
38
+
48
39
  if qualname is None:
49
40
  qualname = type(obj).__qualname__
50
41
 
51
- if module_name is None:
52
- module_name = type(obj).__module__
42
+ if module is None:
43
+ module = type(obj).__module__
53
44
 
54
- if module_name == builtins.__name__:
45
+ if module == builtins.__name__:
55
46
  return qualname
56
- return f"{module_name}.{qualname}"
47
+
48
+ origin = get_origin(obj)
49
+
50
+ if origin:
51
+ args = ", ".join(
52
+ get_full_qualname(arg) if not isinstance(arg, str) else f'"{arg}"'
53
+ for arg in get_args(obj)
54
+ )
55
+ return f"{get_full_qualname(origin)}[{args}]"
56
+
57
+ return f"{module}.{qualname}"
57
58
 
58
59
 
59
60
  def is_builtin_type(tp: type[Any]) -> bool:
@@ -61,17 +62,19 @@ def is_builtin_type(tp: type[Any]) -> bool:
61
62
  return tp.__module__ == builtins.__name__
62
63
 
63
64
 
64
- def make_forwardref(annotation: str, globalns: dict[str, Any]) -> Any:
65
- """Create a forward reference from a string annotation."""
66
- forward_ref = ForwardRef(annotation)
67
- return evaluate_forwardref(forward_ref, globalns, globalns)
68
-
69
-
70
- def get_typed_annotation(annotation: Any, globalns: dict[str, Any]) -> Any:
65
+ def get_typed_annotation(
66
+ annotation: Any,
67
+ globalns: dict[str, Any],
68
+ module: Any = None,
69
+ is_class: bool = False,
70
+ ) -> Any:
71
71
  """Get the typed annotation of a parameter."""
72
72
  if isinstance(annotation, str):
73
- annotation = ForwardRef(annotation)
74
- annotation = evaluate_forwardref(annotation, globalns, globalns)
73
+ if sys.version_info < (3, 9):
74
+ annotation = ForwardRef(annotation)
75
+ else:
76
+ annotation = ForwardRef(annotation, module=module, is_class=is_class)
77
+ annotation = evaluate_forwardref(annotation, globalns, {})
75
78
  return annotation
76
79
 
77
80
 
@@ -82,15 +85,24 @@ def get_typed_return_annotation(obj: Callable[..., Any]) -> Any:
82
85
  if annotation is inspect.Signature.empty:
83
86
  return None
84
87
  globalns = getattr(obj, "__globals__", {})
85
- return get_typed_annotation(annotation, globalns)
88
+ module = getattr(obj, "__module__", None)
89
+ is_class = inspect.isclass(obj)
90
+ return get_typed_annotation(annotation, globalns, module=module, is_class=is_class)
86
91
 
87
92
 
88
93
  def get_typed_parameters(obj: Callable[..., Any]) -> list[inspect.Parameter]:
89
94
  """Get the typed parameters of a callable object."""
90
95
  globalns = getattr(obj, "__globals__", {})
96
+ module = getattr(obj, "__module__", None)
97
+ is_class = inspect.isclass(obj)
91
98
  return [
92
99
  parameter.replace(
93
- annotation=get_typed_annotation(parameter.annotation, globalns)
100
+ annotation=get_typed_annotation(
101
+ parameter.annotation,
102
+ globalns,
103
+ module=module,
104
+ is_class=is_class,
105
+ )
94
106
  )
95
107
  for name, parameter in inspect.signature(obj).parameters.items()
96
108
  ]
@@ -6,6 +6,7 @@ from typing import Any, Callable, Iterator, cast
6
6
  import pytest
7
7
 
8
8
  from anydi import Container
9
+ from anydi._utils import get_typed_parameters
9
10
 
10
11
 
11
12
  def pytest_configure(config: pytest.Config) -> None:
@@ -63,14 +64,14 @@ def _anydi_injected_parameter_iterator(
63
64
  _anydi_unresolved: list[str],
64
65
  ) -> Callable[[], Iterator[tuple[str, Any]]]:
65
66
  def _iterator() -> Iterator[tuple[str, inspect.Parameter]]:
66
- for name, parameter in inspect.signature(request.function).parameters.items():
67
+ for parameter in get_typed_parameters(request.function):
67
68
  if (
68
69
  ((interface := parameter.annotation) is parameter.empty)
69
70
  or interface in _anydi_unresolved
70
- or name in request.node.funcargs
71
+ or parameter.name in request.node.funcargs
71
72
  ):
72
73
  continue
73
- yield name, interface
74
+ yield parameter.name, interface
74
75
 
75
76
  return _iterator
76
77
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: anydi
3
- Version: 0.25.1
3
+ Version: 0.25.2
4
4
  Summary: Dependency Injection library
5
5
  Home-page: https://github.com/antonrh/anydi
6
6
  License: MIT
@@ -5,7 +5,7 @@ anydi/_logger.py,sha256=UpubJUnW83kffFxkhUlObm2DmZX1Pjqoz9YFKS-JOPg,52
5
5
  anydi/_module.py,sha256=E1TfLud_Af-MPB83PxIzHVA1jlDW2FGaRP_il1a6y3Y,3675
6
6
  anydi/_scanner.py,sha256=cyEk-K2Q8ssZStq8GrxMeEcCuAZMw-RXrjlgWEevKCs,6667
7
7
  anydi/_types.py,sha256=vQTrFjsYhlMxfo1nOFem05x2QUJMQkVh4ZaC7W0XZJY,3434
8
- anydi/_utils.py,sha256=XHVNkd-__SKlWlyeGE2e1Yi-DBr4DPWzZOIVbTrQyMI,3692
8
+ anydi/_utils.py,sha256=zP4UvO1aVQJTB8pFNUWAcncvSiuhcg4xNdRU7CoLrqw,3871
9
9
  anydi/ext/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
10
10
  anydi/ext/django/__init__.py,sha256=QI1IABCVgSDTUoh7M9WMECKXwB3xvh04HfQ9TOWw1Mk,223
11
11
  anydi/ext/django/_container.py,sha256=cxVoYQG16WP0S_Yv4TnLwuaaT7NVEOhLWO-YdALJUb4,418
@@ -17,12 +17,12 @@ anydi/ext/django/ninja/__init__.py,sha256=kW3grUgWp_nkWSG_-39ADHMrZLGNcj9TsJ9OW8
17
17
  anydi/ext/django/ninja/_operation.py,sha256=wSWa7D73XTVlOibmOciv2l6JHPe1ERZcXrqI8W-oO2w,2696
18
18
  anydi/ext/django/ninja/_signature.py,sha256=2cSzKxBIxXLqtwNuH6GSlmjVJFftoGmleWfyk_NVEWw,2207
19
19
  anydi/ext/fastapi.py,sha256=kVUKVKtqCx1Nfnm1oh2BMyB0G7qQKPw6OGfxFlqUqtc,5305
20
- anydi/ext/pytest_plugin.py,sha256=vtjQCwQ0_saG8qhYAYn2wQzXVrXfwXOEhJlTjGqtXA8,3999
20
+ anydi/ext/pytest_plugin.py,sha256=nDNqjblVQufLha6P8J8hZw4Q0EuwC71bOI_bdaGm-OQ,4043
21
21
  anydi/ext/starlette/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
22
22
  anydi/ext/starlette/middleware.py,sha256=Ni0BQaPjs_Ha6zcLZYYJ3-XkslTCnL9aCSa06rnRDMI,1139
23
23
  anydi/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
24
- anydi-0.25.1.dist-info/LICENSE,sha256=V6rU8a8fv6o2jQ-7ODHs0XfDFimot8Q6Km6CylRIDTo,1069
25
- anydi-0.25.1.dist-info/METADATA,sha256=fB_AJZKJ6uqYM9Sd4O5ULmFJWuyJq1i1Vi1zV5AyBJE,5160
26
- anydi-0.25.1.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
27
- anydi-0.25.1.dist-info/entry_points.txt,sha256=GmQblwzxFg42zva1HyBYJJ7TvrTIcSAGBHmyi3bvsi4,42
28
- anydi-0.25.1.dist-info/RECORD,,
24
+ anydi-0.25.2.dist-info/LICENSE,sha256=V6rU8a8fv6o2jQ-7ODHs0XfDFimot8Q6Km6CylRIDTo,1069
25
+ anydi-0.25.2.dist-info/METADATA,sha256=5i5o5jrVz-bp48inB3Cz6LpDjElsKfSvqjMyVn_LFJY,5160
26
+ anydi-0.25.2.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
27
+ anydi-0.25.2.dist-info/entry_points.txt,sha256=GmQblwzxFg42zva1HyBYJJ7TvrTIcSAGBHmyi3bvsi4,42
28
+ anydi-0.25.2.dist-info/RECORD,,
File without changes