Skip to main content

Simple yet powerful dependency injection framework!

Project description

Snake DI

test coverage pypi python

Source Code: https://github.com/l1onsun/snake_di

Features:

  • Lightweight - no external dependencies (only typing_extensions)
  • Based on type annotations - no configuration required, less boilerplate
  • Overridable - simple and flexible override system
  • Pytest integration - writing unit tests was never so easy!
  • yeild powered - just yeild your component and then write closing steps
  • async support - able to work with async resources

Install

pip install snake-di

ToDo

  • Documentation
  • Pytest fixtures support
  • Selective builds - allow build only necessary components
  • More helpful exception messages
  • Scopes - reuse factories for different app configurations

Quick example

file_manager.py

@dataclass
class FileManager:
    opened_file: typing.TextIO
    
    def do_some_file_work(self): 
      ...
    
@dataclass
class Settings:
    file_name: str

provider.py

from snake_di import Provider

provider = Provider()

@provider.include_factory
def provide_settings() -> Settings:
    return Settings(file_name="some.txt")

@provider.include_factory
def provide_file(settings: Settings) -> TextIO:
    with open(settings.file_name) as file:
        yield file

@provider.include_factory
def provide_file_manager(opened_file: TextIO) -> FileManager:
    return FileManager(opened_file)

main.py

def main():
    with provider.build() as container:
        assert container.keys() == {Settings, TextIO, FileManager}
        file_manager = container[FileManager]
        file_manager.do_some_file_work()

        assert container[TextIO].closed is False
    assert container[TextIO].closed is True

Override example - Multiple providers:

@Provider.from_factory
def change_settings() -> Settings:
    return Settings(file_name="other.txt")

@Provider.from_factory
def mock_file() -> TextIO:
    return unittest.mock.Mock()
    
def main():
    assert type(mock_file) is type(change_settings) is Provider
    assert type(provider | mock_file | change_settings) is Provider
    
    with (provider | mock_file | change_settings).build() as container:
        assert container[Settings].file_name = "other.txt"
        assert type(container[FileManager].opened_file) is unittest.mock.Mock

Integration with pytest example:

from snake_di.pytest import pytest_provide

@pytest.fixture
def any_usual_fixture():
    return ...

@pytest_provide(provider | mock_file)
def test_with_mocked_file(
    file_manager: FileManager, 
    mocked_file: typing.TextIO, 
    any_usual_fixture
):
    assert type(mocked_file) is unittest.mock.Mock
    assert file_manager.opened_file == mocked_file

Container.partial_solve() example

def handle_data(file_manager: FileManager, settings: Settings, data: str):
  ...

def main():
    with provider.build() as container:
        handle_data_solved = container.partial_solve(handle_data)
        handle_data_solved(data="data")

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

snake-di-0.0.3rc0.tar.gz (15.4 kB view details)

Uploaded Source

Built Distribution

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

snake_di-0.0.3rc0-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file snake-di-0.0.3rc0.tar.gz.

File metadata

  • Download URL: snake-di-0.0.3rc0.tar.gz
  • Upload date:
  • Size: 15.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.28.2

File hashes

Hashes for snake-di-0.0.3rc0.tar.gz
Algorithm Hash digest
SHA256 030227c7c7cb73dd65dd5254ba67cce424eb37d14e4b0841e9a1ecd7327e17b3
MD5 0c60bea27fed30158e115a34eb04f493
BLAKE2b-256 0cc672433576d82ef936fa993cd61b679beff3e49e1495d3f2a4c9261ab199c8

See more details on using hashes here.

File details

Details for the file snake_di-0.0.3rc0-py3-none-any.whl.

File metadata

  • Download URL: snake_di-0.0.3rc0-py3-none-any.whl
  • Upload date:
  • Size: 10.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.28.2

File hashes

Hashes for snake_di-0.0.3rc0-py3-none-any.whl
Algorithm Hash digest
SHA256 f6284d7400d861f926a48597142e345e749efb51d46c50a7d3479dff52a80e2d
MD5 0a0daeb45a15b4f61b79b8ed59355185
BLAKE2b-256 939764486e4600a58e33302bad4fa279499c7ac72c8c116f084cb16994ad33d7

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