Skip to content

Types take 2#162

Open
ocefpaf wants to merge 2 commits into
ioos:mainfrom
ocefpaf:types_take_2
Open

Types take 2#162
ocefpaf wants to merge 2 commits into
ioos:mainfrom
ocefpaf:types_take_2

Conversation

@ocefpaf

@ocefpaf ocefpaf commented Jul 8, 2026

Copy link
Copy Markdown
Member

This PR fix the remaining type issues for a full realized env with all the dependencies. There is not much we can do for cftime b/c there are no stubs there, and we need to make a call on the generic Hashable types for the xarray datavars, dims, etc. See pydata/xarray#4834. Or options are:

  • Add inline ignore comments these 8 errors associated to the generic Hashable and wait for a better fix upstream;
  • Remove xarray from the mypy env and add --ignore-missing-imports ;
  • Use typing.cast and cast them to str.

I prefer option to ignore them for now b/c it a way to explicitly remind us to revisit this in the future.

if "siglay" in ds.dims:
siglay = ds["siglay"].isel(node=0)
elevation_index = int(np.absolute(siglay - level).argmin(axis=0).values)
elevation_index = int(np.absolute((siglay - level).to_numpy()).argmin(axis=0))

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nice mypy "find", by converting to a numpy array early, the two numpy methods we use (absolute and argmin) are always applied to a numpy array and avoid surprises, like when a method has the same name, but different behaviour in similar objects. For example the dataframe .std vs the numpy .std methods.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't np.absolute(arrray_like) always return a numpy array anyway?


# Merge the subsetted datasets
ds_out = xr.merge(ds_out)
_ds_out = xr.merge(ds_out)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These happen b/c we are re-using variable names. It is harmless, but I like the readability of easily knowing that the variable changed its type and b/c of that deserves a new name.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm -- does xr.merge() change the data type? Isn't it a dataset before an after?

In this code, there's quite a lot of processing of a Dataset step by step. -- each step usually creating a smaller Dataset -- so there is something to be said for re-using the same name so the old one will be immediately deleted.

The actual data is probably lazy-loaded, so maybe it doesn't matter, but I think it's kind of clean.

If we are going to use a new name at each step, maybe a meaningful one:

ds_merged = xr.merge(ds_out)

(I don't know if that's a good name, I haven't looked to see what this actually doing)

Or even ds_1, ds_2, ....

@ocefpaf

ocefpaf commented Jul 8, 2026

Copy link
Copy Markdown
Member Author

pre-commits is failing with:

/mirrors-mypy:cf-xarray,netcdf4,xarray>=2024.6,matplotlib@v2.2.0 for python@python3.14 exceeds tier max size 250MiB: 339.5MiB

Looks like we will need to run mypy on GitHub Actions instead. I'll do it in this PR tomorrow.

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