Skip to content

fix: type check with numpy 2.5.4 isscalar stubs - #1031

Merged
FabianHofmann merged 2 commits into
PyPSA:masterfrom
MaykThewessen:fix/mypy-numpy-isscalar
Oct 20, 2026
Merged

FabianHofmann merged 2 commits into
PyPSA:masterfrom
MaykThewessen:fix/mypy-numpy-isscalar

Conversation

@MaykThewessen

Copy link
Copy Markdown
Contributor

Note

AI-assisted. Claude found the cause and wrote the fix, and ran the checks listed below. I reviewed the diff.

Changes proposed in this Pull Request

"Check types" fails on every new CI run since numpy 2.5.4 (linopy/expressions.py:1278: No overload variant of "__add__" ... matches argument type "Number"). numpy 2.5.4 added numbers.Number to the TypeIs of np.isscalar, and xarray's DataArray.__add__ does not accept Number. This casts the scalar in the legacy _add_constant branch to float | np.number, the scalar members of ConstantLike. No runtime change. This unblocks #1021, #1022, #1024 and #1025, among others.

Checklist

  • AI-generated content is marked (see AGENTS.md).
  • Code changes are sufficiently documented; i.e. new functions contain docstrings and further explanations may be given in doc.
  • Unit tests for new features were added (if applicable).
  • A note for the release notes doc/release_notes.rst of the upcoming release is included.
  • I consent to the release of this PR's code under the MIT license.
What was checked (AI-generated)
  • The failing CI job installed numpy 2.5.4; the last passing one (2026-10-10) had 2.5.3. Other versions (mypy 2.4.0, pandas 3.0.6, xarray 2026.9.0) are the same.
  • Reproduced with numpy 2.5.4 and mypy 2.4.0; with the cast, linopy/expressions.py type-checks under numpy 2.5.3 and 2.5.4.
  • test/test_linear_expression.py passes.

🤖 Generated with Claude Code

numpy 2.5.4 widened the TypeIs of np.isscalar to include numbers.Number,
which xarray's DataArray.__add__ does not accept, so mypy fails on the
legacy scalar branch of _add_constant. Cast to the scalar members of
ConstantLike; no runtime change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codspeed

codspeed Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 181 untouched benchmarks
⏩ 181 skipped benchmarks1


Comparing MaykThewessen:fix/mypy-numpy-isscalar (ff5e305) with master (2d6aaf9)

Open in CodSpeed

Footnotes

  1. 181 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

MaykThewessen added a commit to MaykThewessen/linopy that referenced this pull request Oct 12, 2026
MaykThewessen added a commit to MaykThewessen/linopy that referenced this pull request Oct 12, 2026
MaykThewessen added a commit to MaykThewessen/linopy that referenced this pull request Oct 12, 2026
MaykThewessen added a commit to MaykThewessen/linopy that referenced this pull request Oct 12, 2026
@FabianHofmann
FabianHofmann merged commit 86d6ad2 into PyPSA:master Oct 20, 2026
25 checks passed
@FabianHofmann FabianHofmann mentioned this pull request Oct 21, 2026
5 tasks done
FabianHofmann added a commit that referenced this pull request Oct 21, 2026
* fix: type check with numpy 2.5.4 isscalar stubs (#1031)

* fix: type check with numpy 2.5.4 isscalar stubs

numpy 2.5.4 widened the TypeIs of np.isscalar to include numbers.Number,
which xarray's DataArray.__add__ does not accept, so mypy fails on the
legacy scalar branch of _add_constant. Cast to the scalar members of
ConstantLike; no runtime change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor: route legacy scalar add through _combined_with_constant instead of cast

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Fabian <fab.hof@gmx.de>
(cherry picked from commit 86d6ad2)

* fix: keep dash-named parameters in netcdf round trip (#1025)

* fix: keep dash-named parameters, attributes and MultiIndex dims in read_netcdf

read_netcdf assigned each data variable and attribute to its container
by splitting the key at its last dash. Parameters are stored as
parameters-<name>, so a parameter named my-param was assigned to
parameters-my and dropped; the same happened to parameter attributes
and _multiindex attributes of dimensions with a dash in their name.
Keys now belong to the longest known container prefix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: type check with numpy 2.5.4 isscalar stubs

numpy 2.5.4 widened the TypeIs of np.isscalar to include numbers.Number,
which xarray's DataArray.__add__ does not accept, so mypy fails on the
legacy scalar branch of _add_constant. Cast to the scalar members of
ConstantLike; no runtime change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs: move release note to upcoming version

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(io): compute netcdf key owners once

* refactor(io): compactify netcdf key owner lookup

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Fabian <fab.hof@gmx.de>
(cherry picked from commit 4117547)

* fix(solvers): probe the Xpress licence, run solving tests on licensed solvers (#995)

* fix(solvers): probe the Xpress licence, run solving tests on licensed solvers (#994)

* test(io): to_mosek builds a task without a licence, gate it on available_solvers

* test(io): name a mosek task without a licence, gate its set_names cases on available_solvers

* test(io): gate the highs direct QP solve on licensed_solvers

(cherry picked from commit 09d4fde)

* fix: keep auxiliary coordinates of dense containers in netcdf round trip (#1024)

* fix: keep auxiliary coordinates of dense containers in netcdf round trip

read_netcdf dropped every non-index coordinate except MultiIndex levels
to keep out the scalar coordinates of other containers that xarray
attaches when selecting one container's data variables. That also
dropped the auxiliary coordinates of dense variables, constraints,
expressions and parameters, such as the levels on a flat dimension that
v1 recommends instead of a MultiIndex.

Only drop scalar coordinates owned by another container, matched on the
longest prefix since names may contain dashes. Coordinates with
dimensions only attach to the container whose dimensions they share.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test: drop narrative comments, cover dash-named parameter with scalar coord

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Fabian <fab.hof@gmx.de>
(cherry picked from commit 44ff525)

* docs: release notes for v0.10.1

---------

Co-authored-by: Mayk Thewessen <mayk_12@hotmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants