Skip to content

Creator

creator

create_type(name, base, **kwargs)

Create a class named name and register it on the creator module.

The new class inherits from base. Each keyword argument becomes an attribute: a class object is stored as an instance attribute (instantiated when each individual is created); any other value is stored as a class attribute.

Warns if name already exists on the module; the old definition is overwritten.

Parameters:

Name Type Description Default
name str

Name of the class to create.

required
base type | object

Type or instance to inherit from. An instance is replaced by its class.

required
**kwargs Any

Attributes added to the new class.

{}
Source code in deap_er/private/creator.py
def create_type(name: str, base: type | object, **kwargs: Any) -> None:
    """Create a class named ``name`` and register it on the ``creator`` module.

    The new class inherits from ``base``. Each keyword argument becomes
    an attribute: a *class object* is stored as an instance attribute
    (instantiated when each individual is created); any other value is
    stored as a class attribute.

    Warns if ``name`` already exists on the module; the old definition
    is overwritten.

    Args:
        name: Name of the class to create.
        base: Type or instance to inherit from. An instance is replaced
            by its class.
        **kwargs: Attributes added to the new class.
    """
    # warn about class definition overwrite
    if name in globals():
        msg = (
            f"You are creating a new class named '{name}', "
            f"which already exists. The old definition will "
            f"be overwritten by the new one."
        )
        warnings.warn(stacklevel=2, message=msg, category=RuntimeWarning)

    array_typecode = None
    if type(base) is array.array:
        array_typecode = base.typecode
        base = type(base)

    # set base to class if base is an instance
    if not hasattr(base, "__module__"):
        base = base.__class__

    # override numpy and array classes
    base = {"array": ArrayOverride, "numpy": NumpyOverride}.get(base.__module__, base)

    # separate kwargs by their type
    inst_attr, cls_attr = {}, {}
    for key, value in kwargs.items():
        condition = type(value) is type
        _dict = inst_attr if condition else cls_attr
        _dict[key] = value
    if array_typecode is not None:
        cls_attr.setdefault("typecode", array_typecode)

    # create the new class
    new_class = type(name, (cast(Any, base),), cls_attr)

    # define the replacement init func
    def new_init_func(self, *args_: Any, **kwargs_: Any) -> None:
        for attr_name, attr_obj in inst_attr.items():
            setattr(self, attr_name, attr_obj())
        if base.__init__ is not object.__init__:
            cast(Any, base.__init__)(self, *args_, **kwargs_)

    # override the init func and set the global name
    new_class.__init__ = new_init_func
    globals()[name] = new_class