Skip to content

v3.1.0b1

Pre-release
Pre-release

Choose a tag to compare

@ZechCodes ZechCodes released this 03 Jun 00:46
· 83 commits to main since this release

Bevy 3.1.0-beta.1 Release 🚀

We're excited to announce the release of Bevy 3.1.0-beta.1, featuring a major overhaul of the dependency injection system with modern Python 3.12+ features and significant new functionality!

🌟 Major Features

Factory-Based Caching System

  • Intelligent Caching: Factory functions now serve as cache keys, ensuring same factory = same instance
  • Configurable Behavior: Control caching with cache_factory_result parameter in Options class
  • Performance Optimization: Eliminates redundant factory calls while maintaining flexibility for testing scenarios
  • Unified Architecture: Merged factory cache into main instances cache for simplified container management

Modern Type System Overhaul

  • Python 3.12+ Type Alias: Replaced dependency() with clean Inject[T] syntax
  • Rich Options Support: Enhanced Options class with factory caching controls
  • Type-Safe Annotations: Full IDE support with modern Python type hints
  • Qualified Dependencies: Support for multiple implementations with string qualifiers

🔧 Technical Improvements

Enhanced Container System

  • Unified Cache Architecture: Single instances cache supporting Type, (Type, qualifier), and Callable keys
  • Parent Container Inheritance: Factory cache properly inherited across container branches
  • Improved Error Handling: Better error messages with proper context and parameter names
  • Thread Safety Improvements: Enhanced concurrent access patterns

Developer Experience

  • Comprehensive Documentation: Updated API docs, guides, and quickstart for 3.1 features
  • Rich Hook Context: Enhanced injection hooks with detailed execution context
  • Better Debugging: Improved debug logging and error reporting
  • Modern Syntax: Clean, readable dependency injection patterns

📚 Documentation Updates

  • Complete API Documentation: Updated for all new 3.1 features
  • Enhanced Quickstart Guide: Added factory caching examples and best practices
  • Migration Examples: Clear examples showing 3.0 → 3.1 syntax changes
  • Comprehensive Guides: Updated index and navigation structure

💡 Usage Examples

Before (3.0):

from bevy import dependency, inject

@inject
def process_data(
    service: UserService = dependency(),
    logger: Logger = dependency(lambda: Logger("app"))
):
    return service.process()

After (3.1):

from bevy import injectable, auto_inject, Inject
from bevy.injection_types import Options

@auto_inject
@injectable
def process_data(
    service: Inject[UserService],
    logger: Inject[Logger, Options(default_factory=lambda: Logger("app"))]
):
    return service.process()

Factory Caching:

@injectable
def create_expensive_service() -> DatabaseService:
    return DatabaseService(connect_to_database())

@injectable  
def handler_a(service: Inject[DatabaseService, Options(default_factory=create_expensive_service)]):
    return service.query("SELECT * FROM users")

@injectable
def handler_b(service: Inject[DatabaseService, Options(default_factory=create_expensive_service)]):
    return service.query("SELECT * FROM products")

# Both handlers share the same DatabaseService instance!

Qualified Dependencies:

container.add(Database, DatabaseConnection("primary"), qualifier="primary")
container.add(Database, DatabaseConnection("backup"), qualifier="backup")

@injectable
def data_processor(
    primary_db: Inject[Database, Options(qualifier="primary")],
    backup_db: Inject[Database, Options(qualifier="backup")]
):
    return f"Using {primary_db.name} with {backup_db.name} backup"

🔄 Migration Guide

  1. Update Imports: Replace dependency() imports with Inject and Options
  2. Update Syntax: Convert param: Type = dependency() to param: Inject[Type]
  3. Factory Options: Use param: Inject[Type, Options(default_factory=func)] instead of param: Type = dependency(func)
  4. Decorator Order: Ensure @injectable comes after @auto_inject if using both

⚠️ Breaking Changes

  • Removed: dependency() function (replaced with Inject[T] syntax)
  • Changed: Decorator configuration now uses modern @injectable decorator
  • Required: Python 3.12+ for full type alias support

🙏 Acknowledgments

This release represents a significant modernization of Bevy's dependency injection system, bringing it in line with current Python best practices while maintaining the simplicity and power that makes Bevy great.


Full Changelog: View on GitHub

Installation: pip install bevy==3.1.0b1

Feedback: Please report any issues or feedback on our GitHub Issues page.