Repository: pytorch/vision

Issue:
Setting a list of `float` type values to `padding` argument of `RandomCrop()` gets the indirect error message
### 🐛 Describe the bug

Setting a list of `float` type values to `padding` argument of [RandomCrop()](https://pytorch.org/vision/main/generated/torchvision.transforms.v2.RandomCrop.html) gets the indirect error message as shown below:

```python
from torchvision.datasets import OxfordIIITPet
from torchvision.transforms.v2 import RandomCrop

my_data = OxfordIIITPet(
    root="data",                                # ↓↓↓↓↓↓↓↓↓↓↓↓
    transform=RandomCrop(size=[100, 200], padding=[10.8, 20.3])
)

my_data[0][0] # Error
```

```
TypeError: randint() received an invalid combination of arguments - got (int, float, size=tuple), but expected one of:
 * (int high, tuple of ints size, *, torch.Generator generator, Tensor out = None, torch.dtype dtype = None, torch.layout layout = None, torch.device device = None, bool pin_memory = False, bool requires_grad = False)
 * (int high, tuple of ints size, *, Tensor out = None, torch.dtype dtype = None, torch.layout layout = None, torch.device device = None, bool pin_memory = False, bool requires_grad = False)
 * (int low, int high, tuple of ints size, *, torch.Generator generator, Tensor out = None, torch.dtype dtype = None, torch.layout layout = None, torch.device device = None, bool pin_memory = False, bool requires_grad = False)
 * (int low, int high, tuple of ints size, *, Tensor out = None, torch.dtype dtype = None, torch.layout layout = None, torch.device device = None, bool pin_memory = False, bool requires_grad = False)
```

So, the error message should be something direct like below:

> TypeError: `padding` should be an integer or tuple or list of integers, but got 10.8

### Versions

```python
import torchvision

torchvision.__version__ # '0.20.1'
```

Discussion:
@NicolasHug: Happy to consider a PR @hyperkai 

Solution patch:
diff --git a/torchvision/transforms/v2/_utils.py b/torchvision/transforms/v2/_utils.py
index dd65ca4d9c..92d5bc1a2c 100644
--- a/torchvision/transforms/v2/_utils.py
+++ b/torchvision/transforms/v2/_utils.py
@@ -81,11 +81,13 @@ def _get_fill(fill_dict, inpt_type):
 
 
 def _check_padding_arg(padding: Union[int, Sequence[int]]) -> None:
-    if not isinstance(padding, (numbers.Number, tuple, list)):
-        raise TypeError("Got inappropriate padding arg")
 
-    if isinstance(padding, (tuple, list)) and len(padding) not in [1, 2, 4]:
-        raise ValueError(f"Padding must be an int or a 1, 2, or 4 element tuple, not a {len(padding)} element tuple")
+    err_msg = f"Padding must be an int or a 1, 2, or 4 element of tuple or list, got {padding}."
+    if isinstance(padding, (tuple, list)):
+        if len(padding) not in [1, 2, 4] or not all(isinstance(p, int) for p in padding):
+            raise ValueError(err_msg)
+    elif not isinstance(padding, int):
+        raise ValueError(err_msg)
 
 
 # TODO: let's use torchvision._utils.StrEnum to have the best of both worlds (strings and enums)


Test patch:
diff --git a/test/test_transforms_v2.py b/test/test_transforms_v2.py
index d27b268205..157d4faaae 100644
--- a/test/test_transforms_v2.py
+++ b/test/test_transforms_v2.py
@@ -3022,12 +3022,18 @@ class TestCrop:
         with pytest.raises(ValueError, match="Please provide only two dimensions"):
             transforms.RandomCrop([10, 12, 14])
 
-        with pytest.raises(TypeError, match="Got inappropriate padding arg"):
+        with pytest.raises(ValueError, match="Padding must be an int or a 1, 2, or 4"):
             transforms.RandomCrop([10, 12], padding="abc")
 
         with pytest.raises(ValueError, match="Padding must be an int or a 1, 2, or 4"):
             transforms.RandomCrop([10, 12], padding=[-0.7, 0, 0.7])
 
+        with pytest.raises(ValueError, match="Padding must be an int or a 1, 2, or 4"):
+            transforms.RandomCrop([10, 12], padding=0.5)
+
+        with pytest.raises(ValueError, match="Padding must be an int or a 1, 2, or 4"):
+            transforms.RandomCrop([10, 12], padding=[0.5, 0.5])
+
         with pytest.raises(TypeError, match="Got inappropriate fill arg"):
             transforms.RandomCrop([10, 12], padding=1, fill="abc")
 
@@ -3892,12 +3898,18 @@ class TestPad:
         check_transform(transforms.Pad(padding=[1]), make_input())
 
     def test_transform_errors(self):
-        with pytest.raises(TypeError, match="Got inappropriate padding arg"):
+        with pytest.raises(ValueError, match="Padding must be"):
             transforms.Pad("abc")
 
-        with pytest.raises(ValueError, match="Padding must be an int or a 1, 2, or 4"):
+        with pytest.raises(ValueError, match="Padding must be an int or a 1, 2, or 4 element of tuple or list"):
             transforms.Pad([-0.7, 0, 0.7])
 
+        with pytest.raises(ValueError, match="Padding must be an int or a 1, 2, or 4 element of tuple or list"):
+            transforms.Pad(0.5)
+
+        with pytest.raises(ValueError, match="Padding must be an int or a 1, 2, or 4 element of tuple or list"):
+            transforms.Pad(padding=[0.5, 0.5])
+
         with pytest.raises(TypeError, match="Got inappropriate fill arg"):
             transforms.Pad(12, fill="abc")