Fix mypy drift: cast xarray returns, drop deprecated numpy plugin#72
Draft
thodson-usgs wants to merge 1 commit intoxarray-contrib:mainfrom
Draft
Fix mypy drift: cast xarray returns, drop deprecated numpy plugin#72thodson-usgs wants to merge 1 commit intoxarray-contrib:mainfrom
thodson-usgs wants to merge 1 commit intoxarray-contrib:mainfrom
Conversation
Recent mypy (1.20) is stricter about returning Any from functions declared to return concrete xarray types. Add `cast()` calls at 7 return sites where xarray operations (reindex_like, transpose, copy, rename, concat, chunk bookkeeping) come back typed as Any. Drop two `# type: ignore` comments on xr.concat calls that no longer trip mypy. Also drop `plugins = numpy.typing.mypy_plugin` from the mypy config — the plugin is deprecated in numpy 2.3+ and slated for removal. Runs `hatch run lint` / `hatch run test` / `hatch build` fully green locally. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Author
|
@BSchilperoort , I've left this PR in draft, but it notes three options for getting the CI passing. Do you have a preference? I don't have an opinion on this one. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Recent mypy (1.20+) is stricter about
no-any-return: functions declaredto return concrete xarray types can no longer silently return
Any. Severalxarray operations (
reindex_like,transpose,copy,rename,concat,chunk bookkeeping) now come back typed as
Any, tripping the check at 7return sites across
utils.py,methods/_shared.py,methods/interp.py,and
methods/conservative.py.mainlast passed CI on 2026-02-06. If the lint job re-ran today againstcurrent package versions it would fail on these 9 pre-existing errors, so
in-flight PRs (#70 / #71) can't go green without resolving the drift.
This PR also drops
plugins = numpy.typing.mypy_pluginfrom[tool.mypy]since that plugin is deprecated in numpy 2.3+ and slated for removal
(orthogonal to the
no-any-returnwork, but cleaned up here for clarity).Question to the maintainers — which fix do you prefer?
What this PR currently does is option A below, but it's not the only
reasonable choice and the commit is easy to rewrite. I'd appreciate a
preference before this is merged.
A. Add
cast()at each of the 7 return sites (what this PR does).Preserves
warn_return_any = true— future leaks ofAnyback throughfunction boundaries still trip mypy, so the strictness is kept where
it's catching real bugs, with explicit acknowledgement at the known
false-positive sites.
cast(...)calls, acastimport in 4 files.B. Drop
warn_return_any = trueglobally from[tool.mypy].One-line change, all 7 errors disappear. Consistent with the per-module
override that #70 already adds for
conservative_2dfor exactly thisreason — xarray's stubs don't narrow well enough for this check to be
net-positive project-wide.
C. Per-return
# type: ignore[no-any-return]comments at each site.Halfway between A and B — no
castimport, but an inline waiver ateach site rather than a blanket silencing.
Happy to rewrite to B or C if either is preferred; just drop a comment.
Verified locally (option A)
hatch run lint→ green (ruff + mypy + ruff format --check)hatch run test→ 71 passed, 1 skipped, 1 xfailedhatch build→ sdist + wheel builtTest plan
🤖 Generated with Claude Code