Skip to main content

A simple dependency injection library for Python

Project description

gladi

Overview

Import the injector. This is the only import needed.

from gladi import inject

The library provides 4 decorators:

  • @inject.singleton: creates a singleton from a callable that instantiates the class in its return type
  • @inject.scoped: same as singleton, but the callable gets called once per request
  • @inject.transient: same as singleton, but it gets called on every request
  • @inject.resolve: signals to the library that dependencies in parameters need to be resolved
    • using @inject.singleton, @inject.scoped or @inject.transient implies this

Simple Usage

Assuming we have a class to inject,

class Database: ...

create a function (or callable) with no user-provided arguments that returns a Database instance.

def get_database() -> Database:
	return Database(
		host=...,
		port=...,
	)

Mark the callable with a decorator. In this case, a singleton would be appropriate.

@inject.singleton
def get_database() -> Database: ...

Database class is now an injectable (as a singleton).

async retrieve_user(
	user_id: int,
	database = inject(Database),
): ...

Lastly, signal that the retrieve_user function needs its dependencies to be resolved with the @inject.resolve decorator.

@inject.resolve
async retrieve_user(
	user_id: int,
	database = inject(Database),
): ...

await retrieve_user(33)

Project details


Download files

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

Source Distribution

gladi-0.1.1.tar.gz (2.0 kB view details)

Uploaded Source

Built Distribution

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

gladi-0.1.1-py3-none-any.whl (2.6 kB view details)

Uploaded Python 3

File details

Details for the file gladi-0.1.1.tar.gz.

File metadata

  • Download URL: gladi-0.1.1.tar.gz
  • Upload date:
  • Size: 2.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.19

File hashes

Hashes for gladi-0.1.1.tar.gz
Algorithm Hash digest
SHA256 0e29f894dfb8a3dff2334529b073b07ce4a822080f2cf813afe6cc60176cfadf
MD5 586a178d7d03101141ebefd6aedec633
BLAKE2b-256 8232294aa8b2040713d323e361d402fac2c7efd96eda5b84a5474248330dff76

See more details on using hashes here.

File details

Details for the file gladi-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: gladi-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 2.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.19

File hashes

Hashes for gladi-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b7733221084247a3087677ffd24648822539582147248f40c4825a8a0dc37b98
MD5 e6ae999cfab160a6757caa0067a8e7dc
BLAKE2b-256 6b795da5d463270fb6063cf671ffb9659a85cf0b1c12db3365624649a34048d5

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