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-1.0.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

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

gladi-1.0.0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for gladi-1.0.0.tar.gz
Algorithm Hash digest
SHA256 b59ea298c61a04b3094910d6c117d3c9ac27ca947257c673877e77ca23949321
MD5 b21acbe464f964618d273be2e62d7721
BLAKE2b-256 718d75a5ff9744cce9618d7a1a76897a2208875d47961f8b0ae6bf09f7c42311

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for gladi-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 719e47a87178250d40cfaf857748c8225620f490e6d2eb6c654c68457823a1e2
MD5 b850d79bde091d5a2a05e0c872cf3da7
BLAKE2b-256 fbc2c48e97efb45e4c93e38365e2e9ad9c51c309a59bd7d0393cd4f538c2836b

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