Repository navigation
v3.1.0b1
Pre-release
Pre-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_resultparameter inOptionsclass - 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 cleanInject[T]syntax - Rich Options Support: Enhanced
Optionsclass 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
- Update Imports: Replace
dependency()imports withInjectandOptions - Update Syntax: Convert
param: Type = dependency()toparam: Inject[Type] - Factory Options: Use
param: Inject[Type, Options(default_factory=func)]instead ofparam: Type = dependency(func) - Decorator Order: Ensure
@injectablecomes after@auto_injectif using both
⚠️ Breaking Changes
- Removed:
dependency()function (replaced withInject[T]syntax) - Changed: Decorator configuration now uses modern
@injectabledecorator - 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.