Skip to content

Commit e1d0a5e

Browse files
committed
fix: add appropriate error message when validating padding argument.
1 parent dcd1e42 commit e1d0a5e

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

test/test_transforms_v2.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -3028,6 +3028,12 @@ def test_errors(self):
30283028
with pytest.raises(ValueError, match="Padding must be an int or a 1, 2, or 4"):
30293029
transforms.RandomCrop([10, 12], padding=[-0.7, 0, 0.7])
30303030

3031+
with pytest.raises(ValueError, match="Padding must be an int or a 1, 2, or 4 element of tuple or list"):
3032+
transforms.RandomCrop([10, 12], padding=0.5)
3033+
3034+
with pytest.raises(ValueError, match="Padding must be an int or a 1, 2, or 4"):
3035+
transforms.RandomCrop([10, 12], padding=[0.5, 0.5])
3036+
30313037
with pytest.raises(TypeError, match="Got inappropriate fill arg"):
30323038
transforms.RandomCrop([10, 12], padding=1, fill="abc")
30333039

@@ -3887,9 +3893,15 @@ def test_transform_errors(self):
38873893
with pytest.raises(TypeError, match="Got inappropriate padding arg"):
38883894
transforms.Pad("abc")
38893895

3890-
with pytest.raises(ValueError, match="Padding must be an int or a 1, 2, or 4"):
3896+
with pytest.raises(ValueError, match="Padding must be an int or a 1, 2, or 4 element of tuple or list"):
38913897
transforms.Pad([-0.7, 0, 0.7])
38923898

3899+
with pytest.raises(ValueError, match="Padding must be an int or a 1, 2, or 4 element of tuple or list"):
3900+
transforms.Pad(0.5)
3901+
3902+
with pytest.raises(ValueError, match="Padding must be an int or a 1, 2, or 4 element of tuple or list"):
3903+
transforms.Pad(padding=[0.5, 0.5])
3904+
38933905
with pytest.raises(TypeError, match="Got inappropriate fill arg"):
38943906
transforms.Pad(12, fill="abc")
38953907

torchvision/transforms/v2/_utils.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,14 @@ def _check_padding_arg(padding: Union[int, Sequence[int]]) -> None:
8484
if not isinstance(padding, (numbers.Number, tuple, list)):
8585
raise TypeError("Got inappropriate padding arg")
8686

87-
if isinstance(padding, (tuple, list)) and len(padding) not in [1, 2, 4]:
88-
raise ValueError(f"Padding must be an int or a 1, 2, or 4 element tuple, not a {len(padding)} element tuple")
87+
err_msg = "Padding must be an int or a 1, 2, or 4 element of tuple or list, got {value} in {value_type}."
88+
if isinstance(padding, numbers.Number):
89+
if not isinstance(padding, int):
90+
raise ValueError(err_msg.format(value=padding, value_type=type(padding).__name__))
91+
92+
elif isinstance(padding, (tuple, list)):
93+
if len(padding) not in [1, 2, 4] or any(map(lambda x: not isinstance(x, int), padding)):
94+
raise ValueError(err_msg.format(value=padding, value_type=type(padding).__name__))
8995

9096

9197
# TODO: let's use torchvision._utils.StrEnum to have the best of both worlds (strings and enums)

0 commit comments

Comments
 (0)