Skip to main content

Build pypi versions codecov license documentation

Implementation of dependency injection for Python 3

Features:

  • types resolution by signature types annotations (type hints)
  • types resolution by class annotations (type hints)
  • types resolution by names and aliases (convention over configuration)
  • unintrusive: builds objects graph without the need to change the source code of classes
  • minimum overhead to obtain services, once the objects graph is built
  • support for singletons, transient, and scoped services

This library is freely inspired by .NET Standard Microsoft.Extensions.DependencyInjection implementation (ref. MSDN, Dependency injection in ASP.NET Core, Using dependency injection in a .Net Core console application). The ContainerProtocol for v2 is inspired by punq.

Documentation

Rodi is documented here: https://www.neoteroi.dev/rodi/.

Installation

pip install rodi

Efficient

rodi works by inspecting code once at runtime, to generate functions that return instances of desired types - as long as the object graph is not altered. Inspections are done either on constructors (__init__) or class annotations. Validation steps, for example to detect circular dependencies or missing services, are done when building these functions, so additional validation is not needed when activating services.

Flexible

rodi offers two code APIs:

  • one is kept as generic as possible, using a ContainerProtocol for scenarios in which it is desirable being able to replace rodi with alternative implementations of dependency injection for Python. The protocol only expects a class being able to register and resolve types, and to tell if a type is configured in it (__contains__). Even if other implementations of DI don´t implement these three methods, it should be easy to use composition to wrap other libraries with a compatible class.
  • one is a more concrete implementation, for scenarios where it's not desirable to consider alternative implementations of dependency injection.

For this reason, the examples report two ways to achieve certain things.

Examples

For examples, refer to the examples folder.

Recommended practices

All services should be configured once, when an application starts, and the object graph should not be altered during normal program execution. Example: if you build a web application, configure the object graph when bootstrapping the application, avoid altering the Container configuration while handling web requests.

Aim at keeping the Container and service graphs abstracted from the front-end layer of your application, and avoid mixing runtime values with container configuration. Example: if you build a web application, avoid if possible relying on the HTTP Request object being a service registered in your container.

Service life style:

  • singleton - instantiated only once per service provider
  • transient - services are instantiated every time they are required
  • scoped - instantiated only once per root service resolution call (e.g. once per web request)

Usage in BlackSheep

rodi is used in the BlackSheep web framework to implement dependency injection for request handlers.

Release files for rodi 2.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rodi 2.1.0
File Size Uploaded
rodi-2.1.0.tar.gz 16.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for rodi 2.1.0
File Interpreter ABI Platform
rodi-2.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 31.5 kB

Release files / rodi-2.1.0.tar.gz

Download URL rodi-2.1.0.tar.gz
Size 16.8 kB
Tags Source
SHA-256 checksum
How to use checksums
7e1548361dcff5eb5e5228c752ed9a3ac1535cbdc1ba0561deeb63b686f069c5
BLAKE2b-256 checksum
How to use checksums
19e672d8fbdaf8e70b5e717a889f7d35f4983173cdcd3a5c1a9e427d064bb4c2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.12

Release files / rodi-2.1.0-py3-none-any.whl

Download URL rodi-2.1.0-py3-none-any.whl
Size 14.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cfca0d9b9f6693850d2ec01c5680aed5f5e356aafe4494671dc8caddd569d654
BLAKE2b-256 checksum
How to use checksums
96349565181b12a94139376b49645f2439d57f14d01082e12d28f8d88f1a2b27
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.12

Release history Release notifications | RSS feed

This release

2.1.0 This release

2 release files

2.0.8

2 release files

2.0.7

2 release files

2.0.6

2 release files

2.0.5

2 release files

2.0.4

2 release files

2.0.3

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.9

2 release files

1.0.8

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page