CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-rodi

Implementation of dependency injection for Python 3

Overall
score

92%

Overview
Eval results
Files

container-configuration.mddocs/

Container Configuration

Core service registration and container configuration functionality. The Container class provides the primary interface for setting up dependency injection services with various registration patterns and service lifestyles.

Capabilities

Container Initialization

Creates a new dependency injection container with optional configuration for strict mode and scoped service handling.

def __init__(self, *, strict: bool = False, scope_cls: Optional[Type[ActivationScope]] = None):
    """
    Initialize a new Container instance.
    
    Args:
        strict: If True, prevents automatic resolution of unregistered types
        scope_cls: Custom ActivationScope class for scoped services
    """

Basic Service Registration

General-purpose service registration method that supports multiple registration patterns including type-to-type, instance, and factory registration.

def register(self, obj_type, sub_type=None, instance=None, *args, **kwargs) -> Container:
    """
    Register a service in the container.
    
    Args:
        obj_type: The type to register (base type or service key)
        sub_type: Concrete implementation type (for interface registration)
        instance: Specific instance to register (for singleton registration)
        *args: Constructor arguments for the service
        **kwargs: Constructor keyword arguments for the service
        
    Returns:
        Container instance for method chaining
    """

Usage examples:

container = Container()

# Register concrete type
container.register(UserService)

# Register interface to implementation
container.register(IUserRepository, SQLUserRepository)

# Register with constructor arguments
container.register(DatabaseService, connection_string="localhost")

Singleton Registration

Registers services with singleton lifetime - only one instance will be created and reused across all resolutions.

def add_singleton(self, base_type: Type, concrete_type: Optional[Type] = None) -> Container:
    """
    Register a service as singleton.
    
    Args:
        base_type: Base type or interface to register
        concrete_type: Concrete implementation (defaults to base_type)
        
    Returns:
        Container instance for method chaining
    """

Transient Registration

Registers services with transient lifetime - a new instance is created for every resolution request.

def add_transient(self, base_type: Type, concrete_type: Optional[Type] = None) -> Container:
    """
    Register a service as transient.
    
    Args:
        base_type: Base type or interface to register
        concrete_type: Concrete implementation (defaults to base_type)
        
    Returns:
        Container instance for method chaining
    """

Scoped Registration

Registers services with scoped lifetime - one instance per scope (typically per request or operation).

def add_scoped(self, base_type: Type, concrete_type: Optional[Type] = None) -> Container:
    """
    Register a service as scoped.
    
    Args:
        base_type: Base type or interface to register
        concrete_type: Concrete implementation (defaults to base_type)
        
    Returns:
        Container instance for method chaining
    """

Instance Registration

Directly registers a specific instance as a singleton service.

def add_instance(self, instance: Any, declared_class: Optional[Type] = None) -> Container:
    """
    Register a specific instance as a singleton service.
    
    Args:
        instance: The instance to register
        declared_class: Type to register the instance as (defaults to instance type)
        
    Returns:
        Container instance for method chaining
    """

Type Binding

Low-level method for binding types with explicit lifestyle specification.

def bind_types(self, obj_type: Any, concrete_type: Any = None, life_style: ServiceLifeStyle = ServiceLifeStyle.TRANSIENT) -> Container:
    """
    Bind types with explicit lifestyle specification.
    
    Args:
        obj_type: Base type to bind
        concrete_type: Concrete implementation type
        life_style: Service lifestyle (TRANSIENT, SCOPED, SINGLETON)
        
    Returns:
        Container instance for method chaining
    """

Service Provider Building

Builds the optimized service provider for fast service resolution after all registrations are complete.

def build_provider(self) -> Services:
    """
    Build the service provider for efficient service resolution.
    
    Returns:
        Services instance for resolving registered services
    """

Provider Property Access

Provides lazy access to the built service provider, building it on first access if needed.

@property
def provider(self) -> Services:
    """
    Get the service provider, building it if not already built.
    
    Returns:
        Services instance for resolving registered services
    """

Container Inspection

Check if a type is registered in the container.

def __contains__(self, item) -> bool:
    """
    Check if a type is registered in the container.
    
    Args:
        item: Type to check for registration
        
    Returns:
        True if the type is registered, False otherwise
    """

Factory Registration (Low-Level)

Low-level method for registering factory functions with explicit lifestyle specification.

def register_factory(self, factory: Callable, return_type: Optional[Type], life_style: ServiceLifeStyle) -> None:
    """
    Register a factory function with explicit lifestyle specification.
    
    Args:
        factory: Factory function to create service instances
        return_type: Type that the factory returns (inferred if not provided)
        life_style: Service lifestyle (TRANSIENT, SCOPED, SINGLETON)
    """

Generic Resolution

Resolve services directly from the container (typically used during configuration phase).

def resolve(self, obj_type: Union[Type[T], str], scope=None, *args, **kwargs) -> T:
    """
    Resolve a service instance from the container.
    
    Args:
        obj_type: Type or name of service to resolve
        scope: Optional activation scope for scoped services
        *args: Additional constructor arguments
        **kwargs: Additional constructor keyword arguments
        
    Returns:
        Instance of the requested service type
    """

Install with Tessl CLI

npx tessl i tessl/pypi-rodi

docs

aliases-names.md

container-configuration.md

factory-registration.md

index.md

scoped-services.md

service-resolution.md

tile.json