Repository: pytorch
Issue: [JIT] tuple of float is accepted in List[float] but not in Union[float, List[float]]
Labels: oncall: jit

### 🐛 Describe the bug


Related: https://github.com/pytorch/vision/pull/5569#issuecomment-1063507413

A tuple of float such as `(0.5, 0.5, 0.5)` is accepted on function with input parameter type `List[float]` in JIT, however it is not accepted in `Union[float, List[float]]`. We expect that `Union[X, Y]` should always accept what accepted by `Y`. Furthermore, if we just call using standard python without JIT, the tuple is accepted by both type, `List[float]` and `Union[float, List[float]]` and this should be the expected behaviour.

Here is a script to illustrate:

```
import torch
from typing import Union, List

def foo_list(t: torch.Tensor, b: List[float]) -> torch.Tensor:
    t = t.clone()
    if isinstance(b, float):
        b = [b]
    b = torch.as_tensor(b, dtype=t.dtype, device=t.device)
    if b.ndim == 1:
        b = b.view(-1, 1, 1)
    return t.sub_(b)

def foo_union(t:torch.Tensor, b:Union[float, List[float]]) -> torch.Tensor:
    t = t.clone()
    if isinstance(b, float):
        b = [b]
    b = torch.as_tensor(b, dtype=t.dtype, device=t.device)
    if b.ndim == 1:
        b = b.view(-1, 1, 1)
    return t.sub_(b)

    
class TestClass_List(torch.nn.Module):
    def __init__(self, b):
        super().__init__()
        self.b = b
    def forward(self, t):
        return foo_list(t, self.b)
        
class TestClass_Union(torch.nn.Module):
    def __init__(self, b):
        super().__init__()
        self.b = b
    def forward(self, t):
        return foo_union(t, self.b)

t = torch.rand(3, 2, 2)
b = (0.5, 0.5, 0.5)

# Direct call without JIT -> expected behaviour
TestClass_List(b).forward(t)  # Success
TestClass_Union(b).forward(t)  # Success

# Call with JIT
torch.jit.script(TestClass_List(b))  # Success
torch.jit.script(TestClass_Union(b))  # RuntimeError -> expected to be Success
```

have output:

```
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Input In [129], in <cell line: 44>()
     41 TestClass_Union(b).forward(t)
     43 torch.jit.script(TestClass_List(b))
---> 44 torch.jit.script(TestClass_Union(b))

File ~/.pyenv/versions/3.9.10/envs/explore-3.9/lib/python3.9/site-packages/torch/jit/_script.py:1266, in script(obj, optimize, _frames_up, _rcb, example_inputs)
   1264 if isinstance(obj, torch.nn.Module):
   1265     obj = call_prepare_scriptable_func(obj)
-> 1266     return torch.jit._recursive.create_script_module(
   1267         obj, torch.jit._recursive.infer_methods_to_compile
   1268     )
   1270 if isinstance(obj, dict):
   1271     return create_script_dict(obj)

File ~/.pyenv/versions/3.9.10/envs/explore-3.9/lib/python3.9/site-packages/torch/jit/_recursive.py:454, in create_script_module(nn_module, stubs_fn, share_types, is_tracing)
    452 if not is_tracing:
    453     AttributeTypeIsSupportedChecker().check(nn_module)
--> 454 return create_script_module_impl(nn_module, concrete_type, stubs_fn)

File ~/.pyenv/versions/3.9.10/envs/explore-3.9/lib/python3.9/site-packages/torch/jit/_recursive.py:520, in create_script_module_impl(nn_module, concrete_type, stubs_fn)
    518 # Compile methods if necessary
    519 if concrete_type not in concrete_type_store.methods_compiled:
--> 520     create_methods_and_properties_from_stubs(concrete_type, method_stubs, property_stubs)
    521     # Create hooks after methods to ensure no name collisions between hooks and methods.
    522     # If done before, hooks can overshadow methods that aren't exported.
    523     create_hooks_from_stubs(concrete_type, hook_stubs, pre_hook_stubs)

File ~/.pyenv/versions/3.9.10/envs/explore-3.9/lib/python3.9/site-packages/torch/jit/_recursive.py:371, in create_methods_and_properties_from_stubs(concrete_type, method_stubs, property_stubs)
    368 property_defs = [p.def_ for p in property_stubs]
    369 property_rcbs = [p.resolution_callback for p in property_stubs]
--> 371 concrete_type._create_methods_and_properties(property_defs, property_rcbs, method_defs, method_rcbs, method_defaults)

RuntimeError: 

foo_union(Tensor t, Union(float[], float) b) -> (Tensor):
Expected a value of type 'Union[List[float], float]' for argument 'b' but instead found type 'Tuple[float, float, float]'.
:
  File "/var/folders/x2/kfl26sk57cqcjc72_7568gyc0000gn/T/ipykernel_78763/3007931929.py", line 35
    def forward(self, t):
        return foo_union(t, self.b)
               ~~~~~~~~~ <--- HERE
```


### Versions

```
Collecting environment information...
PyTorch version: 1.12.0.dev20220308
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: macOS 12.2.1 (arm64)
GCC version: Could not collect
Clang version: 13.0.0 (clang-1300.0.29.30)
CMake version: Could not collect
Libc version: N/A

Python version: 3.9.10 (main, Mar  8 2022, 16:21:53)  [Clang 13.0.0 (clang-1300.0.29.30)] (64-bit runtime)
Python platform: macOS-12.2.1-arm64-arm-64bit
Is CUDA available: False
CUDA runtime version: No CUDA
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

Versions of relevant libraries:
[pip3] mypy==0.931
[pip3] mypy-extensions==0.4.3
[pip3] numpy==1.22.3
[pip3] torch==1.12.0.dev20220308
[pip3] torchaudio==0.10.2
[pip3] torchdata==0.4.0a0+823d216
[pip3] torchvision==0.13.0a0+d8654bb
[conda] Could not collect
```