Repository: pytorch
Issue: TorchScript: `Return value was annotated as having type float but is actually of type int` violates PEP 484
Labels: oncall: jit

### 🐛 Describe the bug

```python
import torch


class M(torch.nn.Module):
    def __init__(self, a: float):
        super().__init__()
        self.a=a

    def forward(self) -> float:
        return self.a


print(torch.jit.script(M(6))())
```

Throw an error:
```
Traceback (most recent call last):
  File "/home/njzjz/tmp/torch_float_int.py", line 13, in <module>
    print(torch.jit.script(M(1))())
          ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/njzjz/anaconda3/lib/python3.12/site-packages/torch/jit/_script.py", line 1338, in script
    return torch.jit._recursive.create_script_module(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/njzjz/anaconda3/lib/python3.12/site-packages/torch/jit/_recursive.py", line 558, in create_script_module
    return create_script_module_impl(nn_module, concrete_type, stubs_fn)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/njzjz/anaconda3/lib/python3.12/site-packages/torch/jit/_recursive.py", line 635, in create_script_module_impl
    create_methods_and_properties_from_stubs(
  File "/home/njzjz/anaconda3/lib/python3.12/site-packages/torch/jit/_recursive.py", line 467, in create_methods_and_properties_from_stubs
    concrete_type._create_methods_and_properties(
RuntimeError:
Return value was annotated as having type float but is actually of type int:
  File "/home/njzjz/tmp/torch_float_int.py", line 10
    def forward(self) -> float:
        return self.a
        ~~~~~~~~~~~~~ <--- HERE
```

However, [PEP 484](https://peps.python.org/pep-0484/#the-numeric-tower) says

> Rather than requiring that users write import numbers and then use numbers.Float etc., this PEP proposes a straightforward shortcut that is almost as effective: when an argument is annotated as having type float, an argument of type int is acceptable; similar, for an argument annotated as having type complex, arguments of type float or int are acceptable. This does not handle classes implementing the corresponding ABCs or the fractions.Fraction class, but we believe those use cases are exceedingly rare.

So `int` should be acceptable for type `float`.




### Versions

Collecting environment information...
PyTorch version: 2.3.1+cpu
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.4 LTS (x86_64)
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Clang version: 14.0.0-1ubuntu1.1
CMake version: version 3.29.0
Libc version: glibc-2.35

Python version: 3.12.2 | packaged by conda-forge | (main, Feb 16 2024, 20:50:58) [GCC 12.3.0] (64-bit runtime)
Python platform: Linux-5.15.153.1-microsoft-standard-WSL2-x86_64-with-glibc2.35
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
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

CPU:
Architecture:                       x86_64
CPU op-mode(s):                     32-bit, 64-bit
Address sizes:                      39 bits physical, 48 bits virtual
Byte Order:                         Little Endian
CPU(s):                             8
On-line CPU(s) list:                0-7
Vendor ID:                          GenuineIntel
Model name:                         11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz
CPU family:                         6
Model:                              140
Thread(s) per core:                 2
Core(s) per socket:                 4
Socket(s):                          1
Stepping:                           1
BogoMIPS:                           5990.42
Flags:                              fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon rep_good nopl xtopology tsc_reliable nonstop_tsc cpuid pni pclmulqdq vmx ssse3 fma cx16 pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves avx512vbmi umip avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq rdpid movdiri movdir64b fsrm avx512_vp2intersect md_clear flush_l1d arch_capabilities
Virtualization:                     VT-x
Hypervisor vendor:                  Microsoft
Virtualization type:                full
L1d cache:                          192 KiB (4 instances)
L1i cache:                          128 KiB (4 instances)
L2 cache:                           5 MiB (4 instances)
L3 cache:                           12 MiB (1 instance)
Vulnerability Gather data sampling: Unknown: Dependent on hypervisor status
Vulnerability Itlb multihit:        Not affected
Vulnerability L1tf:                 Not affected
Vulnerability Mds:                  Not affected
Vulnerability Meltdown:             Not affected
Vulnerability Mmio stale data:      Not affected
Vulnerability Retbleed:             Mitigation; Enhanced IBRS
Vulnerability Spec rstack overflow: Not affected
Vulnerability Spec store bypass:    Mitigation; Speculative Store Bypass disabled via prctl and seccomp
Vulnerability Spectre v1:           Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:           Mitigation; Enhanced IBRS, IBPB conditional, RSB filling, PBRSB-eIBRS SW sequence
Vulnerability Srbds:                Not affected
Vulnerability Tsx async abort:      Not affected

Versions of relevant libraries:
[pip3] mypy==1.10.0
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.26.4
[pip3] optree==0.11.0
[pip3] torch==2.3.1+cpu
[conda] cudatoolkit               11.8.0              h4ba93d1_12    conda-forge
[conda] libblas                   3.9.0            21_linux64_mkl    conda-forge
[conda] libcblas                  3.9.0            21_linux64_mkl    conda-forge
[conda] liblapack                 3.9.0            21_linux64_mkl    conda-forge
[conda] libmagma                  2.7.2                h09b5827_2    conda-forge
[conda] libmagma_sparse           2.7.2                h09b5827_2    conda-forge
[conda] libtorch                  2.1.2           cpu_generic_h7795521_0    conda-forge
[conda] magma                     2.7.2                h4aca40b_2    conda-forge
[conda] mkl                       2024.0.0         ha957f24_49657    conda-forge
[conda] numpy                     1.26.4          py312heda63a1_0    conda-forge
[conda] optree                    0.11.0                   pypi_0    pypi
[conda] torch                     2.3.1+cpu                pypi_0    pypi

cc @EikanWang @jgong5 @wenzhe-nrv @sanchitintel