elasticai.creator.ir.base.ir_data_meta#

Module Contents#

Classes#

IrDataMeta

This implementation tries to find a good compromise between avoiding boiler plate code and compatibility with static type checkers.

API#

class elasticai.creator.ir.base.ir_data_meta.IrDataMeta[source]#

Bases: type

This implementation tries to find a good compromise between avoiding boiler plate code and compatibility with static type checkers.

The metaclass provides automatic generation of an init function and will convert any type annotations with Attribute types into corresponding MandatoryFields. To not generate an init function define your class like this

class C(metaclass=IrDataMeta, create_init=False):
  name: str

We recommend inheriting from the IrData class instead as this provides useful checks for data integrity and an init function that will be detected by static type checkers.

IMPORTANT: Type annotations for names starting with _ are excluded from this!

NOTE: Other options of implementation have been considered and tried:

  • dynamically add fields and init function after class creation

    • (-)(-) neither fields nor data attribute detected by static type checkers

    • (-) set_name callbacks are not triggered

    • (+) easier to understand

    • (-) slots need to be defined manually by the user, which will certainly lead to hard to find bugs.

  • a decorator that builds a new class taking the decorated class as a prototype

    • (+)(-) type hints for fields are picked up

    • (-) a lot of boiler plate to make type checkers detect init and other attributes

    • (+) if it wasn’t for the type checks, this would be easy to understand and maintain and have the least amount of boiler plate

property required_fields: types.MappingProxyType[str, type]#

In IrData and its children this will be available as a class property. We add it here because python3.13 removed the ability to combine classmethod and property

classmethod __prepare__(name, bases, create_init=False, **kwds)[source]#

called before parsing class body and thus before metaclass instantiation, hence this is a classmethod

__new__(name: str, bases: tuple[type, ...], namespace: dict[str, Any], **kwds) elasticai.creator.ir.base.ir_data_meta.IrDataMeta[source]#

Create and return a new IrDataMeta object. This is the type for new IrData classes. Fields defined in the namespace are fields of the class, not fields of the instantiated object. E.g., the deriving class will feature an __init__ method, the owner of this method is the class, i.e., an object of type IRDataMeta. When accessing such a method attribute via the dot from an object with metaclass IrDataMeta, the method will be bound to that specific instance on the fly.

As such, when dynamically creating the mandatory fields below, this happens once per class definition, not per instantiation of such a class.