Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Dead-simple dependency injection framework for Python.

License

NotificationsYou must be signed in to change notification settings

100nm/python-injection

Repository files navigation

CIPyPI - VersionPyPI - DownloadsRuff

Installation

⚠️Requires Python 3.12 or higher

pip install python-injection

Features

  • Automatic dependency resolution based on type hints.
  • Support for multiple dependency lifetimes:transient,singleton,constant, andscoped.
  • Works seamlessly in bothasync andsync environments.
  • Separation of dependency sets using modules.
  • Runtime switching between different sets of dependencies.
  • Centralized setup logic using entrypoints.
  • Built-in type annotation for easy integration withFastAPI.
  • Lazy dependency resolution for optimized performance.

Motivations

  1. Easy to use
  2. No impact on class and function definitions
  3. No tedious configuration

Quick start

Simply apply the decorators and the package takes care of the rest.

frominjectionimportinjectable,inject,singleton@singletonclassPrinter:def__init__(self):self.history= []defprint(self,message:str):self.history.append(message)print(message)@injectableclassService:def__init__(self,printer:Printer):self.printer=printerdefhello(self):self.printer.print("Hello world!")@injectdefmain(service:Service):service.hello()if__name__=="__main__":main()

Resources

⚠️ The package isn't threadsafe by default, for better performance in single-threaded applications and those usingasyncio.

Non-threadsafe functions are those that resolve dependencies or define scopes. They all come with an optionalparameterthreadsafe.

You can setPYTHON_INJECTION_THREADSAFE=1 in environment variables to make the package fully threadsafe. Theenvironment variable is resolved at thePython module level, so be careful if the variable is defined dynamically.


[8]ページ先頭

©2009-2025 Movatter.jp