Skip to main content

AppCtx

Spring-style dependency injection for Python

Python Version License PyPI Version

Overview

AppCtx is a lightweight dependency injection (DI) container for Python, inspired by the Spring Framework. It uses decorator-based bean registration with package scanning and auto-wiring via type annotations to manage dependencies in a clean, testable way.

Python Requirements: 3.9+ | License: MIT

Features

  • 🏷️ Pure marker decorators — @bean for functions, @component for classes — no coupling to container instances
  • 🔍 Package scanning — ApplicationContext.scan() auto-discovers annotated objects, with include/exclude filtering
  • 🔄 Auto-wiring — Dependencies resolved from type annotations, including from __future__ import annotations (PEP 563)
  • 🧩 Interface-first injection — Depend on an ABC or runtime-checkable Protocol; the container finds a unique implementation
  • 🔗 Circular dependency detection — Caught at startup, not runtime
  • ⚡ Async lifecycle — @post_construct can be async; use await ctx.refresh_async()
  • 🧭 Actionable errors — BeanResolutionError reports the missing type or ambiguous candidates per bean
  • 📦 Lightweight — Zero mandatory dependencies beyond stdlib
  • 🐍 Pythonic API — Feels natural in Python, not a Java port
  • 🏗️ Multiple contexts — Isolated containers for different scopes
  • ✨ Custom bean names — @bean(name="custom") / @component(name="custom")

Installation

pip install appctx

Quick Start

1. Define beans and components

Use @bean on functions (factories) and @component on classes:

# myapp/config.py
from appctx.decorators import bean

@bean
def db_config():
    return {"host": "localhost", "port": 5432}

@bean(name="app_config")  # Custom bean name
def application_config():
    return {"name": "my_app", "debug": False}
# myapp/services.py
from appctx.decorators import component

@component
class UserService:
    def __init__(self, db_config: dict):
        self.db_config = db_config

    def get_user(self, user_id: int):
        return f"User {user_id} from {self.db_config['host']}"

@component(name="notification_svc")
class NotificationService:
    def __init__(self, db_config: dict, app_config: dict):
        self.db_config = db_config
        self.app_config = app_config

2. Scan, refresh, and use

# main.py
from appctx import ApplicationContext

ctx = ApplicationContext()
ctx.scan("myapp").refresh()

# Entry point — the only place you should call get_bean
user_svc = ctx.get_bean(UserService)
print(user_svc.get_user(123))

Note: get_bean() is only needed at the entry point to bootstrap your application. All other dependencies are injected automatically via constructor or function parameters — just like Spring.

PEP 563: AppCtx works with from __future__ import annotations; stringized annotations are resolved when the container refreshes.

Post-construct lifecycle

Use @post_construct to run initialization logic after all beans are created:

from appctx.decorators import component, post_construct

@component
class DatabaseService:
    def __init__(self):
        self.connection = None

    @post_construct
    def init(self):
        self.connection = create_connection()  # Called after all beans are created

Async hooks are supported through the async refresh API:

@component
class AsyncDatabaseService:
    def __init__(self):
        self.connection = None

    @post_construct
    async def init(self):
        self.connection = await create_connection()

Call await ctx.refresh_async() from an async application. ctx.refresh() only runs synchronous hooks and raises a clear RuntimeError if async hooks are present.

Core Concepts

Concept Description
Bean An object managed by the container
ApplicationContext The DI container that holds and wires beans
@bean Pure marker decorator for functions (bean factories)
@component Pure marker decorator for classes (components)
scan() Discovers @bean/@component annotated objects in packages
refresh() Instantiates all beans and runs synchronous lifecycle hooks
refresh_async() Async variant that also runs async @post_construct hooks
add() Manually register a class or function bean definition

Dependency Resolution Rules

  1. Positional arguments → resolved by exact type first, then by a unique assignable type (ABC or runtime-checkable Protocol)
  2. Keyword-only arguments (*, name) → resolved by parameter name, falls back to default
  3. **kwargs → receives all remaining beans not consumed by other parameters
  4. Ambiguity → multiple candidates or a missing dependency raise BeanResolutionError with the argument and candidate details at refresh()

PEP 563 string annotations are supported. object is not used for assignability matching, so a dependency on object only matches an explicitly registered object bean.

Scanning

Package scanning

Scan an entire package recursively:

ctx = ApplicationContext()
ctx.scan("myapp").refresh()

Module scanning

Scan a single module:

ctx = ApplicationContext()
ctx.scan("myapp.services").refresh()

Auto-detection

Call scan() without arguments to auto-detect the caller's package:

# Called from within myapp/main.py
ctx = ApplicationContext()
ctx.scan().refresh()  # Auto-detects "myapp" package

Exclude patterns

Exclude packages or modules using fnmatch glob patterns:

ctx = ApplicationContext()
ctx.scan("myapp", exclude=["myapp.tests", "myapp.internal_*"]).refresh()

Include patterns

Restrict scanning to selected packages or modules:

ctx = ApplicationContext()
ctx.scan("myapp", include=["myapp.services", "myapp.config"]).refresh()

include supports the same fnmatch glob and package-prefix matching as exclude. When both are provided, a module must match include and must not match exclude.

Decorators

@bean — Function bean factories

from appctx.decorators import bean

@bean
def my_service():
    return MyService()

@bean(name="custom_service")
def another_service():
    return MyService()
  • Only accepts functions — raises TypeError on classes
  • Sets _is_bean = True and _bean_name attributes
  • The declared return type (when it is a class) is registered in addition to the concrete return type; useful for factories declared to return an ABC/Protocol. The produced object must be an instance of the declared type, otherwise refresh() raises TypeError
  • Use @component for classes

@component — Class components

from appctx.decorators import component

@component
class MyComponent:
    def __init__(self, dependency: SomeDependency):
        self.dependency = dependency

@component(name="my_comp")
class AnotherComponent:
    pass
  • Only accepts classes — raises TypeError on functions
  • Sets _is_component = True and _bean_name attributes
  • A class without a custom __init__ is constructed with no arguments; explicit **kwargs still receives the remaining beans
  • Use @bean for functions

@post_construct — Lifecycle hooks

from appctx.decorators import post_construct

class MyService:
    @post_construct
    def init(self):
        self.connection = create_connection()

Organizing Beans Across Modules

Define beans in separate modules, scan to discover them:

# myapp/__init__.py — empty or minimal

# myapp/config.py
from appctx.decorators import bean
@bean
def db_config():
    return {"host": "localhost"}

# myapp/services.py
from appctx.decorators import component
@component
class UserService:
    def __init__(self, db_config: dict):
        self.db_config = db_config

# main.py
from appctx import ApplicationContext
ctx = ApplicationContext()
ctx.scan("myapp").refresh()

You can also manually register bean definitions with add():

ctx = ApplicationContext()
ctx.add(my_factory_func).add(MyComponentClass)
ctx.refresh()

add() accepts classes and functions only. Modules are ignored; pre-built instances raise TypeError — wrap them in a @bean factory instead.

Best Practices

  1. Rely on auto-wiring, avoid get_bean — Let the container inject dependencies via parameters.
  2. Use type annotations — They are the primary mechanism for dependency resolution.
  3. Use @bean for functions, @component for classes — Semantic separation like Spring.
  4. Prefer scan() over add() — Let the container discover beans automatically.
  5. Use @post_construct for initialization — Not __init__ side-effects.
  6. Separate configuration from logic — Use dedicated config beans.
  7. Design for testability — Beans that accept dependencies via constructor are trivially mockable.

Error Handling

  • BeanResolutionError (subclass of RuntimeError) — one or more bean definitions could not be instantiated; the message lists each bean with its reason (missing type, ambiguous candidates, or an annotation that cannot be evaluated) and flags likely circular dependencies.
  • KeyError — get_bean could not find a bean.
  • RuntimeError — get_bean(Type) matched multiple beans, or refresh() found an async @post_construct hook.
  • TypeError — add() received a pre-built instance, or a @bean factory returned an object that is not an instance of its declared return type.

Documentation

License

MIT — see LICENSE.

Metadata

Release files for appctx 0.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for appctx 0.6.0
File Size Uploaded Provenance Actions
appctx-0.6.0.tar.gz 26.9 kB With provenance Details for appctx-0.6.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for appctx 0.6.0
File Interpreter ABI Platform Actions
appctx-0.6.0-py3-none-any.whl Python 3 none any Details for appctx-0.6.0-py3-none-any.whl

Total release size: 40.5 kB

Release files / appctx-0.6.0.tar.gz

Download URL appctx-0.6.0.tar.gz
Size 26.9 kB
Tags Source
SHA-256 checksum
How to use checksums
495eac22dbdf09deed29356f15501c1ec5333fe47f97226af9f9a32957aba9b0
BLAKE2b-256 checksum
How to use checksums
a8e57522f725162e575e1c07483c2a4cd635033c98a5b77d7faf0a788f4a423c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 23, 2026.

Transparency log

Release files / appctx-0.6.0-py3-none-any.whl

Download URL appctx-0.6.0-py3-none-any.whl
Size 13.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1997c632aac8099ffe2ded84e1e2d30585559b18676d1f01a777853077d6995a
BLAKE2b-256 checksum
How to use checksums
e360384923f4baa2f6a9ce5bcd5ecccd58dd20caf8bc6fa4bf167adece24c324
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 23, 2026.

Transparency log

Security for appctx 0.6.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for appctx-0.6.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 23, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page