Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved typing of align & broadcast #8234

Merged
merged 5 commits into from
Oct 9, 2023
Merged

Conversation

headtr1ck
Copy link
Collaborator

@headtr1ck headtr1ck commented Sep 26, 2023

  • User visible changes (including notable bug fixes) are documented in whats-new.rst

This PR improves the typing of align.
Before: the type of the inputs was reduced to the common superclass and the return type was the same. This often required casts or ignores when mixing classes (e.g. da, ds = xr.align(da, ds).
Now: the return types are exactly the same as the input types if the number of passed arguments is <=5.

Only downside: it requires some ugly overloads with type ignores on align. Maybe someone knows how to type this better?

@max-sixty
Copy link
Collaborator

I hear you on the ugly overloads, but I don't think there's a way around it atm, and it will be nice to get accurate types here.

I looked at whether something like ParamSpec would work — ideally we'd define the inputs as a sequence of T_Alignable and map them to the corresponding outputs — but I don't think it's designed for this use case

@headtr1ck headtr1ck added the plan to merge Final call for comments label Oct 5, 2023
@headtr1ck headtr1ck merged commit 7aa207b into pydata:main Oct 9, 2023
26 of 28 checks passed
dcherian added a commit to dcherian/xarray that referenced this pull request Oct 14, 2023
* upstream/main: (46 commits)
  xfail flaky test (pydata#8299)
  Most of mypy 1.6.0 passing (pydata#8296)
  Rename `reset_encoding` to `drop_encoding` (pydata#8287)
  Enable `.rolling_exp` to work on dask arrays (pydata#8284)
  Fix `GroupBy` import (pydata#8286)
  Ask bug reporters to confirm they're using a recent version of xarray (pydata#8283)
  Add pyright type checker (pydata#8279)
  Improved typing of align & broadcast (pydata#8234)
  Update ci-additional.yaml (pydata#8280)
  Fix time encoding regression (pydata#8272)
  Allow a function in `.sortby` method (pydata#8273)
  make more args kw only (except 'dim') (pydata#6403)
  Use duck array ops in more places (pydata#8267)
  Don't raise rename warning if it is a no operation (pydata#8266)
  Mandate kwargs on `to_zarr` (pydata#8257)
  copy  the `dtypes` module to the `namedarray` package. (pydata#8250)
  Add xarray-regrid to ecosystem.rst (pydata#8270)
  Use strict type hinting for namedarray (pydata#8241)
  Update type annotation for center argument of dataaray_plot methods (pydata#8261)
  [pre-commit.ci] pre-commit autoupdate (pydata#8262)
  ...
@headtr1ck headtr1ck deleted the typedalign branch December 18, 2023 20:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plan to merge Final call for comments topic-typing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants