Injectinator 0.2.0__tar.gz → 0.4.0__tar.gz

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,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: Injectinator
3
- Version: 0.2.0
3
+ Version: 0.4.0
4
4
  Summary: Very simple dependency injection
5
5
  Author-email: Chris Read <centurix@gmail.com>
6
6
  License: MIT
@@ -124,10 +124,10 @@ Notes on this script
124
124
  - It relies on dunder methods on `func` itself, so these _could_ change in the future. They've all been clumped up together for this reason to make it obvious when it fails because the Python foundation changed their usage of dunders
125
125
  - There is no parameter support on the injected class, it could be adjusted to support them
126
126
  - All the config for the injection is in the function specification rather than the decorator
127
- - There's no types. It should have types I guess. Probably added to the two function specifications. Could be lazy and just make it all `Any` but then you'd have to drag in `typing` as an import for this gist
128
127
  - I have not timed this. It could be wildly inefficient. There's an iteration enumerating over the function parameter list so its O(n)
129
128
  - This script does work with arbitrary length argument specifications like adding `*args` to the end
130
- - Although it is DI in 11 lines, I've no interest in code golf.
129
+ - Although it is DI in 11 lines, I've no interest in code golf
130
+ - There is a typed version of this in typed.py, just in case your type checked is getting a little nervous around it.
131
131
 
132
132
  Operation
133
133
  -
@@ -103,10 +103,10 @@ Notes on this script
103
103
  - It relies on dunder methods on `func` itself, so these _could_ change in the future. They've all been clumped up together for this reason to make it obvious when it fails because the Python foundation changed their usage of dunders
104
104
  - There is no parameter support on the injected class, it could be adjusted to support them
105
105
  - All the config for the injection is in the function specification rather than the decorator
106
- - There's no types. It should have types I guess. Probably added to the two function specifications. Could be lazy and just make it all `Any` but then you'd have to drag in `typing` as an import for this gist
107
106
  - I have not timed this. It could be wildly inefficient. There's an iteration enumerating over the function parameter list so its O(n)
108
107
  - This script does work with arbitrary length argument specifications like adding `*args` to the end
109
- - Although it is DI in 11 lines, I've no interest in code golf.
108
+ - Although it is DI in 11 lines, I've no interest in code golf
109
+ - There is a typed version of this in typed.py, just in case your type checked is getting a little nervous around it.
110
110
 
111
111
  Operation
112
112
  -
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "Injectinator"
3
- version = "0.2.0"
3
+ version = "0.4.0"
4
4
  description = "Very simple dependency injection"
5
5
  authors = [
6
6
  {name = "Chris Read",email = "centurix@gmail.com"},
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: Injectinator
3
- Version: 0.2.0
3
+ Version: 0.4.0
4
4
  Summary: Very simple dependency injection
5
5
  Author-email: Chris Read <centurix@gmail.com>
6
6
  License: MIT
@@ -124,10 +124,10 @@ Notes on this script
124
124
  - It relies on dunder methods on `func` itself, so these _could_ change in the future. They've all been clumped up together for this reason to make it obvious when it fails because the Python foundation changed their usage of dunders
125
125
  - There is no parameter support on the injected class, it could be adjusted to support them
126
126
  - All the config for the injection is in the function specification rather than the decorator
127
- - There's no types. It should have types I guess. Probably added to the two function specifications. Could be lazy and just make it all `Any` but then you'd have to drag in `typing` as an import for this gist
128
127
  - I have not timed this. It could be wildly inefficient. There's an iteration enumerating over the function parameter list so its O(n)
129
128
  - This script does work with arbitrary length argument specifications like adding `*args` to the end
130
- - Although it is DI in 11 lines, I've no interest in code golf.
129
+ - Although it is DI in 11 lines, I've no interest in code golf
130
+ - There is a typed version of this in typed.py, just in case your type checked is getting a little nervous around it.
131
131
 
132
132
  Operation
133
133
  -
@@ -5,4 +5,6 @@ src/Injectinator.egg-info/SOURCES.txt
5
5
  src/Injectinator.egg-info/dependency_links.txt
6
6
  src/Injectinator.egg-info/top_level.txt
7
7
  src/injectinator/__init__.py
8
+ src/injectinator/py.typed
9
+ src/injectinator/typed.py
8
10
  tests/test_inject.py
File without changes
@@ -0,0 +1,19 @@
1
+ import functools # This is only needed for debugging, you can remove
2
+ from typing import Callable, ParamSpec, TypeVar
3
+
4
+ P = ParamSpec("P")
5
+ R = TypeVar("R")
6
+
7
+ def injectinator(func: Callable[P, R]) -> Callable[P, R]:
8
+ @functools.wraps(func) # This is only needed for debugging, you can remove
9
+ def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
10
+ replacements = dict(zip(
11
+ func.__code__.co_varnames[:func.__code__.co_argcount],
12
+ list(([None] * func.__code__.co_argcount) + list(func.__defaults__ or []))[-func.__code__.co_argcount:]
13
+ ))
14
+ for position, default in enumerate(replacements.keys()):
15
+ if position >= len(args):
16
+ if default not in kwargs and isinstance(replacements[default], type):
17
+ kwargs[default] = replacements[default]()
18
+ return func(*args, **kwargs)
19
+ return wrapper
File without changes