Skip to content

Commit dcc3cfd

Browse files
committed
TypeErrors should be raised for invalid input types, rather than ValueErrors.
1 parent c43f2f1 commit dcc3cfd

4 files changed

Lines changed: 7 additions & 7 deletions

File tree

xarray/core/dataarray.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1172,7 +1172,7 @@ def expand_dims(self, dim=None, axis=None, **dim_kwargs):
11721172
This object, but with an additional dimension(s).
11731173
"""
11741174
if isinstance(dim, int):
1175-
raise ValueError('dim should be str or sequence of strs or dict')
1175+
raise TypeError('dim should be str or sequence of strs or dict')
11761176
elif isinstance(dim, str):
11771177
dim = {dim: 1}
11781178
elif isinstance(dim, (list, tuple)):

xarray/core/dataset.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2362,7 +2362,7 @@ def expand_dims(self, dim=None, axis=None, **dim_kwargs):
23622362
This object, but with an additional dimension(s).
23632363
"""
23642364
if isinstance(dim, int):
2365-
raise ValueError('dim should be str or sequence of strs or dict')
2365+
raise TypeError('dim should be str or sequence of strs or dict')
23662366
elif isinstance(dim, str):
23672367
dim = {dim: 1}
23682368
elif isinstance(dim, (list, tuple)):
@@ -2404,8 +2404,8 @@ def expand_dims(self, dim=None, axis=None, **dim_kwargs):
24042404
elif isinstance(v, int):
24052405
pass # Do nothing if the dimensions value is just an int
24062406
else:
2407-
raise ValueError('The value of new dimension {k} must be '
2408-
'an iterable or an int'.format(k=k))
2407+
raise TypeError('The value of new dimension {k} must be '
2408+
'an iterable or an int'.format(k=k))
24092409

24102410
for k, v in self._variables.items():
24112411
if k not in dim:

xarray/tests/test_dataarray.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,7 +1303,7 @@ def test_expand_dims_error(self):
13031303
coords={'x': np.linspace(0.0, 1.0, 3)},
13041304
attrs={'key': 'entry'})
13051305

1306-
with raises_regex(ValueError, 'dim should be str or'):
1306+
with raises_regex(TypeError, 'dim should be str or'):
13071307
array.expand_dims(0)
13081308
with raises_regex(ValueError, 'lengths of dim and axis'):
13091309
# dims and axis argument should be the same length
@@ -1331,7 +1331,7 @@ def test_expand_dims_error(self):
13311331
array = DataArray(np.random.randn(3, 4), dims=['x', 'dim_0'],
13321332
coords={'x': np.linspace(0.0, 1.0, 3)},
13331333
attrs={'key': 'entry'})
1334-
with pytest.raises(ValueError):
1334+
with pytest.raises(TypeError):
13351335
array.expand_dims({"new_dim": 3.2})
13361336

13371337
# Attempt to use both dim and kwargs

xarray/tests/test_dataset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2020,7 +2020,7 @@ def test_expand_dims_error(self):
20202020
'b': np.linspace(0, 1, 4),
20212021
'c': np.linspace(0, 1, 5)},
20222022
attrs={'key': 'entry'})
2023-
with raises_regex(ValueError, 'value of new dimension'):
2023+
with raises_regex(TypeError, 'value of new dimension'):
20242024
original.expand_dims({"d": 3.2})
20252025

20262026
with raises_regex(ValueError, 'both keyword and positional'):

0 commit comments

Comments
 (0)