Registry / web-framework / pinject

pinject

JSON →
library0.15.3pypypi✓ verified 84d ago

A pythonic dependency injection library for Python, inspired by Guice. Current version is 0.15.3 (last release on PyPI 0.14.1 but GitHub has 0.15.3). Development appears to be in maintenance mode with no recent releases.

pip install pinject
INSTALL
IMPORT
SIG · PINJECT
P
pinject
web-frameworkpythonv0.15.3
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

new_object_graph
from pinject import new_object_graph
import pinject new_object_graph = pinject.new_object_graph
Common but unnecessary to import the whole module
inject
from pinject import inject
from pinject import injectable
@injectable is deprecated since v0.10, use @inject
BindingSpec
from pinject import BindingSpec
from pinject import binding_spec
Correct class is BindingSpec, not binding_spec

Demonstrates basic injection: MyClass depends on DepClass; Pinject automatically injects the dependency.

import pinject class MyClass: @pinject.inject() def __init__(self, dep): self.dep = dep class DepClass: pass obj_graph = pinject.new_object_graph(binding_specs=[], modules=[DepClass]) obj = obj_graph.provide(MyClass) print(obj.dep.__class__.__name__) # DepClass
Debug
Known issues
gotchaPinject does not support Python 3.12+ due to removal of inspect.getargspec and other deprecated APIs. Pinject relies on inspect module details that are removed in Python 3.12.
fix
Use Python 3.11 or earlier, or switch to an actively maintained DI library like 'inject' or 'dependency-injector'.
affects: >=0.15.3
deprecated@injectable decorator is deprecated since v0.10. Use @inject instead.
fix
Replace @pinject.injectable with @pinject.inject() (note the parentheses).
affects: >=0.10
breakingIn v0.10, the default method names for BindingSpec were changed. Custom names can be set via 'configure_method_name' and 'dependencies_method_name' in new_object_graph.
fix
If you relied on default method names like 'configure', they still work. Only breaking if you overrode them and expected the old behavior.
affects: 0.10 to 0.15.3
Errors
Common errors & fixes
AttributeError: module 'pinject' has no attribute 'injectable'
Using deprecated @injectable decorator which was removed or aliased.
fix
Use @pinject.inject() instead of @pinject.injectable.
TypeError: 'module' object is not callable
Trying to invoke a module as a function, e.g., pinject.new_object_graph() but imported pinject incorrectly.
fix
Make sure you import correctly: from pinject import new_object_graph
TypeError: object() takes no arguments
Incorrectly using @inject without parentheses. @inject is a function that returns a decorator, so you need @inject() with parentheses.
fix
Use @pinject.inject() (with parentheses) on the __init__ method.
Upgrade
Version history
0.15.3latest on PyPI
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources