pyecs.helpers.Unsafe#

Functions

auto_unsafe(cls)

Class decorator that automatically generates unsafe versions for all methods that return StatusCodes.FAILURE.

generate_unsafe([exception_type, error_message])

Decorator that automatically generates an unsafe version of a method.

process_unsafe_methods(cls)

Process all methods decorated with @generate_unsafe and add their unsafe versions to the class.

pyecs.helpers.Unsafe.generate_unsafe(exception_type=<class 'pyecs.exceptions.Exceptions.OperationFailedError'>, error_message=None)[source]#

Decorator that automatically generates an unsafe version of a method.

The unsafe version raises an exception instead of returning StatusCodes.FAILURE. The decorated function gets a new unsafe attribute that is the exception-raising version.

Return type:

Callable[[Callable[[ParamSpec(P)], TypeVar(R)]], Callable[[ParamSpec(P)], TypeVar(R)]]

Parameters:
Usage:

@generate_unsafe(ComponentNotFoundError, “Component not found on entity”) def get_component(self, entity: Entity, component_type: type[Component]) -> Component | Literal[StatusCodes.FAILURE]:

# This automatically generates: # Safe version: component = world.get_component(entity, Position) # Returns FAILURE on error # Strict version: component = world.get_component_or_raise(entity, Position) # Raises exception on error

pyecs.helpers.Unsafe.process_unsafe_methods(cls)[source]#

Process all methods decorated with @generate_unsafe and add their unsafe versions to the class.

Return type:

type[TypeVar(C)]

Parameters:

cls (type[C])

pyecs.helpers.Unsafe.auto_unsafe(cls)[source]#

Class decorator that automatically generates unsafe versions for all methods that return StatusCodes.FAILURE.

Return type:

type[TypeVar(C)]

Parameters:

cls (type[C])

Usage:

@auto_unsafe class ECSWorld:

def get_component(self, …) -> Component | Literal[StatusCodes.FAILURE]: