Skip to content

Commit

Permalink
Fix (or ignore) new mypy errors
Browse files Browse the repository at this point in the history
  • Loading branch information
timmens committed Jan 21, 2025
1 parent 7d73b0b commit 9026fd9
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .tools/envs/testenv-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ dependencies:
- scipy>=1.2.1 # run, tests
- sqlalchemy # run, tests
- seaborn # dev, tests
- mypy=1.13 # dev, tests
- mypy=1.14.1 # dev, tests
- pyyaml # dev, tests
- jinja2 # dev, tests
- annotated-types # dev, tests
Expand Down
2 changes: 1 addition & 1 deletion .tools/envs/testenv-numpy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ dependencies:
- scipy>=1.2.1 # run, tests
- sqlalchemy # run, tests
- seaborn # dev, tests
- mypy=1.13 # dev, tests
- mypy=1.14.1 # dev, tests
- pyyaml # dev, tests
- jinja2 # dev, tests
- annotated-types # dev, tests
Expand Down
2 changes: 1 addition & 1 deletion .tools/envs/testenv-others.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ dependencies:
- scipy>=1.2.1 # run, tests
- sqlalchemy # run, tests
- seaborn # dev, tests
- mypy=1.13 # dev, tests
- mypy=1.14.1 # dev, tests
- pyyaml # dev, tests
- jinja2 # dev, tests
- annotated-types # dev, tests
Expand Down
2 changes: 1 addition & 1 deletion .tools/envs/testenv-pandas.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ dependencies:
- scipy>=1.2.1 # run, tests
- sqlalchemy # run, tests
- seaborn # dev, tests
- mypy=1.13 # dev, tests
- mypy=1.14.1 # dev, tests
- pyyaml # dev, tests
- jinja2 # dev, tests
- annotated-types # dev, tests
Expand Down
8 changes: 4 additions & 4 deletions src/optimagic/differentiation/derivatives.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ def first_derivative(
f0 = np.array(f0, dtype=np.float64)

# convert the raw evaluations to numpy arrays
raw_evals = _convert_evals_to_numpy(
raw_evals_arr = _convert_evals_to_numpy(
raw_evals=raw_evals,
unpacker=unpacker,
registry=registry,
Expand All @@ -337,9 +337,9 @@ def first_derivative(
)

# apply finite difference formulae
evals_data = np.array(raw_evals).reshape(2, n_steps, len(x), -1)
evals_data = np.transpose(evals_data, axes=(0, 1, 3, 2))
evals = Evals(pos=evals_data[0], neg=evals_data[1])
evals_data = np.array(raw_evals_arr).reshape(2, n_steps, len(x), -1)
evals_data_transposed = np.transpose(evals_data, axes=(0, 1, 3, 2))
evals = Evals(pos=evals_data_transposed[0], neg=evals_data_transposed[1])

jac_candidates = {}
for m in ["forward", "backward", "central"]:
Expand Down
2 changes: 1 addition & 1 deletion src/optimagic/examples/criterion_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def rosenbrock_gradient(params: PyTree) -> PyTree:
l4 = np.delete(x, [0])
l4 = np.append(l4, 0)
l5 = np.full((len(x) - 1), 2)
l5 = np.append(l5, 0)
l5 = np.append(l5, 0) # type: ignore[assignment]
flat = 100 * (4 * (l1**3) + 2 * l2 - 2 * (l3**2) - 4 * (l4 * x)) + 2 * l1 - l5
return _unflatten_gradient(flat, params)

Expand Down
5 changes: 4 additions & 1 deletion src/optimagic/logging/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,10 @@ def _build_history_dataframe(self) -> pd.DataFrame:

times = np.array(history["time"])
times -= times[0]
history["time"] = times.tolist()
# For numpy arrays with ndim = 0, tolist() returns a scalar, which violates the
# type hinting list[Any] from above. As history["time"] is always a list, this
# case is safe to ignore.
history["time"] = times.tolist() # type: ignore[assignment]

df = pd.DataFrame(history)
df = df.merge(
Expand Down
6 changes: 4 additions & 2 deletions src/optimagic/optimization/multistart.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,11 @@ def run_explorations(
"""
internal_problem = internal_problem.with_step_id(step_id)
x_list = list(sample)
x_list: list[NDArray[np.float64]] = list(sample)

raw_values = np.array(internal_problem.exploration_fun(x_list, n_cores=n_cores))
raw_values = np.asarray(
internal_problem.exploration_fun(x_list, n_cores=n_cores), dtype=np.float64
)

is_valid = np.isfinite(raw_values)

Expand Down
6 changes: 1 addition & 5 deletions src/optimagic/optimizers/scipy_optimizers.py
Original file line number Diff line number Diff line change
Expand Up @@ -799,11 +799,7 @@ def _solve_internal_problem(
)
raw_res = scipy.optimize.brute(
func=problem.fun,
ranges=tuple(
map(
tuple, np.column_stack((problem.bounds.lower, problem.bounds.upper))
)
),
ranges=tuple(zip(problem.bounds.lower, problem.bounds.upper, strict=False)),
Ns=self.n_grid_points,
full_output=True,
finish=self.polishing_function,
Expand Down

0 comments on commit 9026fd9

Please sign in to comment.