inject-x
Dependency injection for Python with automatic class discovery.
Built on top of injector, inject-x adds auto-loading from a folder and retrieval of all instances by base type — removing the need to manually register every class.
Installation
pip install inject-x
Requirements: Python >= 3.11.5, injector >= 0.22.0
Quick start
from injectx import Injector, Presentation, Service, Repository, Config
# Load and register all eligible classes from a folder (recursive)
inj = Injector().register_all_from_folder("myapp/")
# Retrieve a specific instance
my_service = inj.get(MyService)
# Retrieve all registered instances of a base type
presentations = inj.get_all_by_type(Presentation)
Base classes
Annotate your classes by inheriting from one of the four base types:
| Class | Intended use |
|---|---|
Presentation |
Controllers, API handlers, CLI entry points |
Service |
Business logic |
Repository |
Data access (DB, external APIs) |
Config |
Configuration objects |
Only classes that inherit from one of these will be picked up by register_all_from_folder. Classes that don't inherit from any of them (like plain helpers) are ignored.
API
Injector(folder_path_to_register="")
Creates a new injector. Optionally accepts a folder path to auto-register on construction.
inj = Injector("myapp/")
# equivalent to:
inj = Injector().register_all_from_folder("myapp/")
register_all_from_folder(folder: str) -> Injector
Recursively walks folder, imports every .py file (skipping __init__.py), and registers all classes that inherit from Presentation, Service, Repository, or Config as singletons.
- Returns
selffor chaining. - Modules are loaded with their full dotted name (e.g.
myapp.services.user_service), consistent with normal Python imports — soinjector.get(MyService)andfrom myapp.services.user_service import MyServicerefer to the exact same class object. - Calling
register_all_from_folder(orregister) twice with the same class is a no-op.
inj = (
Injector()
.register_all_from_folder("myapp/services/")
.register_all_from_folder("myapp/repositories/")
)
register(cls) -> None
Manually registers a single class as a singleton. Useful when auto-discovery is not needed.
inj = Injector()
inj.register(MyService)
get(cls) -> instance
Returns the singleton instance for the given class.
svc = inj.get(MyService)
get_all_by_type(base_class) -> list
Returns the singleton instances of all registered classes that are subclasses of base_class, traversing the full inheritance hierarchy recursively.
from injectx import Injector, Presentation
class Handler(Presentation): ...
class SpecialHandler(Handler): ...
inj = Injector()
inj.register(Handler)
inj.register(SpecialHandler)
inj.get_all_by_type(Presentation) # [<Handler>, <SpecialHandler>]
Example project layout
myapp/
├── presentation/
│ └── user_controller.py # class UserController(Presentation): ...
├── services/
│ └── user_service.py # class UserService(Service): ...
└── repositories/
└── user_repo.py # class UserRepo(Repository): ...
from injectx import Injector, Service
inj = Injector().register_all_from_folder("myapp/")
# All services, regardless of nesting depth
services = inj.get_all_by_type(Service)
Running tests
pip install pytest
pytest tests/
Build & publish
bumpver update --patch # or --minor / --major
python -m build
twine upload dist/*
License
MIT — Colageo Mirko mirko.colageo@gmail.com
Metadata
Release files for inject-x 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| inject_x-0.0.2.tar.gz | 8.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| inject_x-0.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 15.1 kB
Release files / inject_x-0.0.2.tar.gz
| Download URL | inject_x-0.0.2.tar.gz |
|---|---|
| Size | 8.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a62c6ac03a9ac0aeecfb8b1267bb9017e4343f803535d6df68bd60123919a2a5
|
|
BLAKE2b-256 checksum How to use checksums |
ee85332ec435f60e8aa6f65fa37cc155bef0a46d914049f24e1e13a350fe64e6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.11
|
Release files / inject_x-0.0.2-py3-none-any.whl
| Download URL | inject_x-0.0.2-py3-none-any.whl |
|---|---|
| Size | 6.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f4c2ebfd2bedacbea3cc62b2b941c5b7124cab193732db473af30311ab884052
|
|
BLAKE2b-256 checksum How to use checksums |
8bec6b4aeb2edf64a5a01fd824f3498645b4dedad83283408b9fc80cc637235a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.11
|