Closed ge0ffrey closed 6 months ago
There will be a completely new syntax soon, using typing.Annotated
that is used in Python libraries like Pydantic (https://docs.pydantic.dev/latest/api/standard_library_types/#decimaldecimal) and FastAPI (https://fastapi.tiangolo.com/tutorial/dependencies/?h=annotated#first-steps):
@timefold.solver.planning_entity
@dataclass
class Entity:
code: Annotated[str, timefold.solver.PlanningId]
value: Annotated[Value, timefold.solver.PlanningVariable] = field(default=None)
(dataclass is a Python builtin decorator that adds equality, constructor, etc.; this is equivalent to):
@timefold.solver.planning_entity
class Entity:
code: Annotated[str, timefold.solver.PlanningId]
value: Annotated[Value, timefold.solver.PlanningVariable]
def __init__(self, code: str, value: Value = None):
self.code = code
self.value = value
# also defines __str__, __hash__, __eq__, etc.
...
(PlanningVariable, PlanningId, etc. become classes, and hence PascalCase; planning_entity is still a decorator (since you cannot apply Annotated to a class), and thus a function, and thus snake_case)
(the timefold.solver prefix can be removed if you do from timefold.solver import ...
)
@Christopher-Chianelli I think that Geoffrey's point is that the value range ref is not necessary. A while ago I added a feature that - if there is only one value range - that it is automatically matched.
It's not a matter of syntax; the ref simply shouldn't have to be there.
Yes, but the syntax in the README will be completely changed. That is, it won't be @planning_variable(Timeslot)
, (the old decorator with the parameter made optional) but Annotated[Timeslot, PlanningVariable]
(new Python Annotated syntax). This issue (along with other annotation changes such as removal of CustomShadowVariable (replaced with ShadowVariable and PiggyBackShadowVariable) and nullable -> allows_unassigned) will be fixed in the Annotated refactor.
Done by #23
Are value range provider refs still needed? In Java, by default they are not if there is no ambiguity.
From the readme:
@planning_variable(Timeslot, value_range_provider_refs=["timeslotRange"])