Skip to content

Silence xarray FutureWarning by adding compat parameter#446

Open
nunnsy wants to merge 2 commits into
ecmwf:masterfrom
nunnsy:master
Open

Silence xarray FutureWarning by adding compat parameter#446
nunnsy wants to merge 2 commits into
ecmwf:masterfrom
nunnsy:master

Conversation

@nunnsy

@nunnsy nunnsy commented Apr 11, 2026

Copy link
Copy Markdown

Description

xarray is changing the default compat argument for xr.merge from 'no_conflicts' to 'override', triggering a FutureWarning on every call through merge_datasets(). Explicitly passing compat='no_conflicts' preserves the current strict behaviour (conflicting variable values raise) and silences the warning.

Exact warning example

[cfgrib/xarray_store.py:51]: FutureWarning: In a future version of xarray the default value for compat will change from compat='no_conflicts' to compat='override'. This is likely to lead to different results when combining overlapping variables with the same name. To opt in to new defaults and get rid of these warnings now use `set_options(use_new_combine_kwarg_defaults=True) or set compat explicitly.
  o = xr.merge([o, ds], **kwargs)

Change

  • cfgrib/xarray_store.py — added compat="no_conflicts" to the merge_datasets(...) call in open_datasets():
# before
for ds in merge_datasets(
    type_of_level_datasets[type_of_level], join="exact", combine_attrs="identical"
):

# after
for ds in merge_datasets(
    type_of_level_datasets[type_of_level], join="exact", combine_attrs="identical", compat="no_conflicts"
):

compat='override' is intentionally avoided — it would silently accept conflicting variable values, which is a regression in safety.

Contributor Declaration

By opening this pull request, I affirm the following:

  • All authors agree to the Contributor License Agreement.
  • The code follows the project's coding standards.
  • I have performed self-review and added comments where needed.
  • I have added or updated tests to verify that my changes are effective and functional.
  • I have run all existing tests and confirmed they pass.

Copilot AI and others added 2 commits April 11, 2026 11:43
…mpat

Silence xarray FutureWarning by explicitly passing `compat='no_conflicts'` to `xr.merge`
Comment thread cfgrib/xarray_store.py
for i, o in enumerate(merged):
if all(o.attrs[k] == ds.attrs[k] for k in o.attrs):
try:
o = xr.merge([o, ds], **kwargs)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

It's possible to move the change here if desirable. Thought best to keep generic given other kwargs were in the call already amended.

@nunnsy

nunnsy commented Apr 18, 2026

Copy link
Copy Markdown
Author

@iainrussell for your review :)

@Metamess

Metamess commented May 5, 2026

Copy link
Copy Markdown
Contributor

Commenting to lend support to this PR, can't wait for these FutureWarnings to go away. Thanks @nunnsy for being on top of it!

Personally, I would move not just the new compat="no_conflicts" to the xr.merge() call, but the other two xr.merge parameters as well: join="exact", combine_attrs="identical". Not that it matters very much in the end, since that one call in open_datasets() is the only usage of merge_datasets(), but I believe these parameters are integral to the behavior of merge_datasets(), so I'm not sure if there is any benefit to not having them inside the function. Especially since there is no way for called of open_datasets() to influence the merge kwargs (neither **kwargs nor backend_kwargs are routed to the merge_datasets() call in any way).

That said, the fix as initially suggested would be the smallest required change to fix the FutureWarnings issue, and be consistent with the current approach of controlling the xr.merge() via the **kwargs of merge_datasets()

@andrew-s28

Copy link
Copy Markdown

Oops, I seem to have missed this PR when I submitted #447 on this last week. Thanks all for being on top of that, I add +1 support for this PR for silencing these very noisy warnings. I will close that issue when this is merged 😄

@Metamess

Copy link
Copy Markdown
Contributor

@iainrussell would you have a moment to take a look at this PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants