Repository: pytorch
Issue: [jit] Failed to load a saved scripted function
Labels: oncall: jit

### 🐛 Describe the bug

The following code is failing:
```python
import torch  # '1.13.0.dev20220707'
from typing import Union, List


def func(padding: Union[int, List[int]]) -> None:

    if isinstance(padding, 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")


s = torch.jit.script(func)
s.save("test")
ss = torch.jit.load("test")
```
Output:
```
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
/var/folders/8r/t4lgs4t95xjgyj6g63kz10mm0000gn/T/ipykernel_47279/2244443921.py in <module>
----> 1 ss = torch.jit.load("test")

~/opt/miniconda3/envs/test/lib/python3.8/site-packages/torch/jit/_serialization.py in load(f, map_location, _extra_files)
    160     cu = torch._C.CompilationUnit()
    161     if isinstance(f, str) or isinstance(f, pathlib.Path):
--> 162         cpp_module = torch._C.import_ir_module(cu, str(f), map_location, _extra_files)
    163     else:
    164         cpp_module = torch._C.import_ir_module_from_buffer(

RuntimeError: 

Type mismatch: padding0 is set to type List[int] in the true branch and type int in the false branch:
  File "code/__torch__.py", line 9
    _0 = "Padding must be an int or a 1, 2, or 4 element tuple, not a {} element tuple"
    _1 = isinstance(padding, list)
    if _1:
    ~~~~~~
      padding1 = unchecked_cast(List[int], padding)
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      _3 = torch.len(padding1)
      ~~~~~~~~~~~~~~~~~~~~~~~~
      _4 = torch.__not__(torch.__contains__([1, 2, 4], _3))
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      _2, padding0 = _4, padding1
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    else:
    ~~~~~
      _2, padding0 = False, unchecked_cast(int, padding)
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
    if _2:
      padding2 = unchecked_cast(List[int], padding0)
and was used here:
  File "code/__torch__.py", line 17
      _2, padding0 = False, unchecked_cast(int, padding)
    if _2:
      padding2 = unchecked_cast(List[int], padding0)
                                           ~~~~~~~~ <--- HERE
      _5 = torch.format(_0, torch.len(padding2))
      ops.prim.RaiseException(_5, "builtins.ValueError")
```

Looks like the issue is related to the call `len(padding)` in the value error message. 
1) if we split if condition into two then everything works:
```diff
def func(padding: Union[int, List[int]]) -> None:

-    if isinstance(padding, list) and len(padding) not in [1, 2, 4]:
+    if isinstance(padding, list):
+        if 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")

```

2) If removed `len(padding)` from message error then everything works.
```diff
def func(padding: Union[int, List[int]]) -> None:

    if isinstance(padding, 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")
+        raise ValueError(f"Padding must be an int or a 1, 2, or 4 element tuple")
```


### Versions

1.13.0.dev20220707