Skip to content

UgridDataset.data_vars doesn't preserve type #367

@JoerivanEngelen

Description

@JoerivanEngelen

Problem

I ran into an issue where I assumed a call to UgridDataset.data_vars would return a mapping of variable names to UgridDataArrays, but unfortunately for me it returned a mapping to regular xarray DataArrays.

import xarray as xr
import xugrid as xu
import numpy as np

grid = xu.data.xoxo()
ds = xr.DataArray(np.arange(283), dims=("mesh2d_nFaces")).to_dataset(name="var")
uds = xu.UgridDataset(ds, grids=[grid])
print(type(uds.data_vars["var"])) #prints xr.DataArray

Context

MyPy gave me some difficulties, I used data_vars to work around this. Python runs both examples without any issues (except for the bug described above).

def my_func(var: xu.UgridDataArray | xr.DataArray):
     return var

my_func(**dict(**uds.data_vars))  # MyPy is OK
my_func(**uds)  # MyPy fails

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions