---
title: di | CodeWeaver Docs
description: API reference for codeweaver.core.di
url: "https://docs.knitli.com/api/core/di"
type: static
generatedAt: "2026-04-17T17:21:07.978Z"
---

# di
       [Open in ChatGPT](https://chatgpt.com/?q=Read%20https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fapi%2Fcore%2Fdi%2F.%20I%20want%20to%20ask%20questions%20about%20it.)[Open in Claude](https://claude.ai/new?q=Read%20https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fapi%2Fcore%2Fdi%2F.%20I%20want%20to%20ask%20questions%20about%20it.)[View in Markdown](/codeweaver/api/core/di.md)       [Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fapi%2Fcore%2Fdi%2F)[Share on X](https://x.com/intent/tweet?url=https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fapi%2Fcore%2Fdi%2F&text=di)[Share on Threads](https://threads.net/intent/post?url=https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fapi%2Fcore%2Fdi%2F&text=di)[Share on Bluesky](https://bsky.app/intent/compose?text=di%20https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fapi%2Fcore%2Fdi%2F)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fapi%2Fcore%2Fdi%2F)[Share on Reddit](https://reddit.com/submit?url=https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fapi%2Fcore%2Fdi%2F&title=di)[Share on Hacker News](https://news.ycombinator.com/submitlink?u=https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fapi%2Fcore%2Fdi%2F&t=di)[Share on Email](mailto:?subject=di&body=https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fapi%2Fcore%2Fdi%2F)[Share on WhatsApp](https://wa.me/?text=di%20https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fapi%2Fcore%2Fdi%2F)[Share on Telegram](https://t.me/share/url?url=https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fapi%2Fcore%2Fdi%2F&text=di)
# `codeweaver.core.di`
[Section titled “codeweaver.core.di”](#codeweavercoredi)
Dependency Injection for CodeWeaver.

Provides a FastAPI-inspired declarative injection system.

## Registering Providers
[Section titled “Registering Providers”](#registering-providers)
To register a provider function or class with the DI container, use the `@dependency_provider` decorator:


**

```
from codeweaver.core.di import dependency_provider

@dependency_provider(ServiceProvider)def service_factory() -&gt; ServiceProvider:    return ServiceProvider()
```


**

```
from codeweaver.core.di import dependency_provider

@dependency_provider(scope="singleton")class ServiceProvider:    def __init__(self):        self.value = 42
```

The `@dependency_provider` decorator automatically registers your factory with the global DI container. No additional setup needed - just decorate and inject!

## Creating a Type Alias for Dependency Injection
[Section titled “Creating a Type Alias for Dependency Injection”](#creating-a-type-alias-for-dependency-injection)
Create a type alias for cleaner, more maintainable code:


**

```
from typing import TYPE_CHECKING, Annotated
from codeweaver.core.di import INJECTED
if TYPE_CHECKING:    from someplace import ServiceProvider
# Simple type alias (relies on auto-resolution)type ServiceDep = ServiceProvider
# Or with explicit Depends marker for custom behaviorfrom codeweaver.core.di import depends
type ServiceDepWithScope = Annotated[ServiceProvider, depends(scope="request")]
```

## Using Your Dependency in Functions
[Section titled “Using Your Dependency in Functions”](#using-your-dependency-in-functions)
Use the `INJECTED` sentinel to mark parameters for dependency injection:


**

```
from typing import TYPE_CHECKINGfrom codeweaver.core.di import INJECTED
if TYPE_CHECKING:    from someplace import ServiceProvider

async def my_function(service: ServiceProvider = INJECTED) -&gt; None:    # service will be injected automatically by the DI container    # The type annotation tells the container what to inject    ...
```

The container automatically:

 1. Discovers all `@dependency_provider` registrations
 1. Resolves dependencies recursively
 1. Manages singleton/request/function scopes
 1. Handles async factories and generators