Socket
Book a DemoInstallSign in
Socket

injected

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

injected

Simple, type-safe dependency injection

0.2.2
pipPyPI
Maintainers
1

injected

CI Build Status Test coverage report
PyPI Package Python versions

Simple, type-safe dependency injection in idiomatic Python, inspired by FastAPI.

Injecting dependencies

from injected import depends, resolver


def get_a() -> int:
    return 13


def get_b() -> int:
    return 17


@resolver
def get_sum(
    a: int = depends(get_a),
    b: int = depends(get_b),
) -> int:
    return a + b


assert get_sum() == 30

Seeding the context of a resolver

It's sometimes useful to be able to provide an already resolved value, making it available throughout the dependency graph. The canonical example of this is how FastAPI makes things like requests and headers available to all dependencies.

To use this pattern, you specify a sentinel function, get_global_value in the example below, and then map it to a resolved value in a context passed to seed_context().

from injected import depends, resolver, seed_context


def get_global_value() -> int: ...


@resolver
def calculate_value(a: int = depends(get_global_value)) -> int:
    return a + 13


seeded = seed_context(calculate_value, {get_global_value: 31})

assert seeded() == 44

Async dependencies and context managers

Dependencies can be any combination of async and non-async functions and context managers. Async dependencies are resolved concurrently, and are scheduled at the optimal time, as soon as their own dependencies are resolved.

Context managers are torn down upon exiting the entry-point function.

import asyncio
from collections.abc import AsyncIterator
from contextlib import asynccontextmanager
from injected import depends, resolver


async def get_a() -> int:
    return 13


def get_b() -> int:
    return 17


@asynccontextmanager
async def get_c() -> AsyncIterator[int]:
    yield 23


@resolver
async def get_sum_async(
    a: int = depends(get_a),
    b: int = depends(get_b),
    c: int = depends(get_c),
) -> int:
    return a + b + c


@resolver
def get_sum_sync(
    a: int = depends(get_a),
    b: int = depends(get_b),
    c: int = depends(get_c),
) -> int:
    return a + b + c


assert asyncio.run(get_sum_async()) == 53
assert get_sum_sync() == 53

FAQs

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.