Skip to main content

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 self for chaining.
  • Modules are loaded with their full dotted name (e.g. myapp.services.user_service), consistent with normal Python imports — so injector.get(MyService) and from myapp.services.user_service import MyService refer to the exact same class object.
  • Calling register_all_from_folder (or register) 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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

inject_x-0.0.2.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

inject_x-0.0.2-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file inject_x-0.0.2.tar.gz.

File metadata

  • Download URL: inject_x-0.0.2.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for inject_x-0.0.2.tar.gz
Algorithm Hash digest
SHA256 a62c6ac03a9ac0aeecfb8b1267bb9017e4343f803535d6df68bd60123919a2a5
MD5 38ee67dc632c7e23b813143654955099
BLAKE2b-256 ee85332ec435f60e8aa6f65fa37cc155bef0a46d914049f24e1e13a350fe64e6

See more details on using hashes here.

File details

Details for the file inject_x-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: inject_x-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for inject_x-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f4c2ebfd2bedacbea3cc62b2b941c5b7124cab193732db473af30311ab884052
MD5 39bed35833654b50dab978442de3d5fa
BLAKE2b-256 8bec6b4aeb2edf64a5a01fd824f3498645b4dedad83283408b9fc80cc637235a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page