Skip to content

Commit

Permalink
Update error message for direction init argument in History class
Browse files Browse the repository at this point in the history
  • Loading branch information
timmens committed Nov 30, 2024
1 parent e6b6066 commit d8fefc1
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/optimagic/optimization/history.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import warnings
from dataclasses import dataclass
from functools import partial
from typing import Any
from typing import Any, Literal

import numpy as np
from numpy.typing import NDArray
Expand All @@ -23,7 +23,7 @@ class History:
# TODO: add counters for the relevant evaluations
def __init__(
self,
direction: Direction,
direction: Direction | Literal["minimize", "maximize"],
params: list[PyTree] | None = None,
fun: list[float | None] | None = None,
time: list[float] | None = None,
Expand All @@ -38,9 +38,13 @@ def __init__(
recover a history from a log.
"""
if direction not in [Direction.MINIMIZE, Direction.MAXIMIZE]:
raise ValueError(f"Invalid direction: {direction}.")
self.direction = direction
try:
self.direction = Direction(direction)
except ValueError:
valid_options = list(Direction.__members__.values())
msg = f"Invalid direction: '{direction}'. Choose from {valid_options}."
raise ValueError(msg) from None

Check warning on line 46 in src/optimagic/optimization/history.py

View check run for this annotation

Codecov / codecov/patch

src/optimagic/optimization/history.py#L43-L46

Added lines #L43 - L46 were not covered by tests

self._params = params if params is not None else []
self._fun = fun if fun is not None else []
self._time = time if time is not None else []
Expand Down

0 comments on commit d8fefc1

Please sign in to comment.