Skip to main content

A simple dependency injection library for Python

Project description

sdi

Overview

Import the injector. This is the only import needed.

from sdi 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.0.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.0-py3-none-any.whl (2.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: gladi-0.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 aa1219cdf70ad4e8a18e6aaafcbd77e28a7a1e5dc48f090277bfadf5cbbc6523
MD5 b35e84f16c580d86bc57df56650a8e3d
BLAKE2b-256 fd57d37af215b0a68172f178454f5f4a8c3100406aa799535a2b4603d5fa4a80

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gladi-0.1.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a4beade1724c20b4879d19db54bf2d2ab02784f9526b20c6f59ae143d1c7fce6
MD5 f696d05ddd4667011b39f8893bb4794e
BLAKE2b-256 d3c0b1203444ab81827da3feb4695db3320f8c1708c16665da6dc48d5cc7aae3

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