optapy - Support attribute level annotations/decorators
Christopher-Chianelli opened this issue · comments
Christopher Chianelli commented
Currently, annotations/decorators can only be specified on a getter
(i.e. like this:
@planning_entity
class Entity:
value: int
def __init__(self, value=None):
self.value = value
@planning_variable(int, value_range_provider_ref=['value_range'])
def get_value(self):
return self.value
def set_value(self, value):
self.value = value
)
This code is quite verbose, so we would like to support attribute level annotations/decorators.
However, unlike Java, you cannot put an annotation on an attribute (other than declaring what type an attribute is).
There are several options:
Do it on a class level (on @planning_entity
/@planning_solution
):
@planning_entity(
planning_variables=[PlanningVariable('value', int, value_range_provider_ref=['value_range'])]
)
class Entity:
value: int
def __init__(self, value=None):
self.value = value
Do it as a static/class attribute
@planning_entity
class Entity:
value: int = PlanningVariable(int, value_range_provider_ref=['value_range'])
def __init__(self, value=None):
self.value = value
Do it with Annotated
(https://docs.python.org/3/library/typing.html#typing.Annotated)
@planning_entity
class Entity:
value: Annotated[int, PlanningVariable(value_range_provider_ref=['value_range'])]
def __init__(self, value=None):
self.value = value