Skip to content

Commit

Permalink
fix UnboundLocalError: local variable 'flattened_dims' referenced bef…
Browse files Browse the repository at this point in the history
…ore assignment
  • Loading branch information
andersy005 committed Jan 26, 2024
1 parent d880621 commit 1524519
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
9 changes: 7 additions & 2 deletions xarray/namedarray/_array_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,12 @@ def permute_dims(x: NamedArray[Any, _DType], axes: _Axes) -> NamedArray[Any, _DT
data type as x.
"""
xp = _get_data_namespace(x)

dims = x.dims
out = x._new(dims=tuple(dims[i] for i in axes), data=xp.permute_dims(x._data, axes))
new_dims = tuple(dims[i] for i in axes)
if isinstance(x._data, _arrayapi):
xp = _get_data_namespace(x)
out = x._new(dims=new_dims, data=xp.permute_dims(x._data, axes))
else:
out = x._new(dims=new_dims, data=x._data.transpose(axes)) # type: ignore[attr-defined]
return out
2 changes: 1 addition & 1 deletion xarray/namedarray/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,7 @@ def permute_dims(
flattened_dims.extend(self.dims)
else:
flattened_dims.extend(item)
dims = tuple(infix_dims(flattened_dims, self.dims, missing_dims))
dims = tuple(infix_dims(flattened_dims, self.dims, missing_dims))

if len(dims) < 2 or dims == self.dims:
# no need to transpose if only one dimension
Expand Down

0 comments on commit 1524519

Please sign in to comment.