Repository: pytorch
Issue: TorchScript raises an OSError when using frozen dataclass
Labels: oncall: jit

### 🐛 Describe the bug

Running the following code:

```python
import torch
from dataclasses import dataclass
from typing import Tuple

@torch.jit.script
@dataclass(frozen=True)
class Info:
    pass

class Model:
    def __init__(self, info: Tuple[Info]):
        self.infos = [i for i in info]

    def forward(self, x):
        return self.infos
```

Got:

```
Traceback (most recent call last):
  File "/Users/hguandl/Library/micromamba/envs/ml/lib/python3.11/site-packages/torch/_sources.py", line 23, in get_source_lines_and_file
    sourcelines, file_lineno = inspect.getsourcelines(obj)
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/hguandl/Library/micromamba/envs/ml/lib/python3.11/inspect.py", line 1240, in getsourcelines
    lines, lnum = findsource(object)
                  ^^^^^^^^^^^^^^^^^^
  File "/Users/hguandl/Library/micromamba/envs/ml/lib/python3.11/inspect.py", line 1077, in findsource
    raise OSError('could not get source code')
OSError: could not get source code

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/hguandl/submit/test.py", line 5, in <module>
    @torch.jit.script
     ^^^^^^^^^^^^^^^^
  File "/Users/hguandl/Library/micromamba/envs/ml/lib/python3.11/site-packages/torch/jit/_script.py", line 1375, in script
    _compile_and_register_class(obj, _rcb, qualified_name)
  File "/Users/hguandl/Library/micromamba/envs/ml/lib/python3.11/site-packages/torch/jit/_recursive.py", line 59, in _compile_and_register_class
    ast = get_jit_class_def(obj, obj.__name__)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/hguandl/Library/micromamba/envs/ml/lib/python3.11/site-packages/torch/jit/frontend.py", line 299, in get_jit_class_def
    method_defs = [
                  ^
  File "/Users/hguandl/Library/micromamba/envs/ml/lib/python3.11/site-packages/torch/jit/frontend.py", line 300, in <listcomp>
    get_jit_def(obj, name, self_name=self_name, is_classmethod=is_classmethod(obj))
  File "/Users/hguandl/Library/micromamba/envs/ml/lib/python3.11/site-packages/torch/jit/frontend.py", line 331, in get_jit_def
    parsed_def = parse_def(fn) if not isinstance(fn, _ParsedDef) else fn
                 ^^^^^^^^^^^^^
  File "/Users/hguandl/Library/micromamba/envs/ml/lib/python3.11/site-packages/torch/_sources.py", line 120, in parse_def
    sourcelines, file_lineno, filename = get_source_lines_and_file(
                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/hguandl/Library/micromamba/envs/ml/lib/python3.11/site-packages/torch/_sources.py", line 32, in get_source_lines_and_file
    raise OSError(msg) from e
OSError: Can't get source for <function Info.__delattr__ at 0x13f7218a0>. TorchScript requires source access in order to carry out compilation, make sure original .py files are available.
```


### Versions

Collecting environment information...
PyTorch version: 2.3.0
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: macOS 14.6.1 (arm64)
GCC version: Could not collect
Clang version: 15.0.0 (clang-1500.3.9.4)
CMake version: version 3.30.3
Libc version: N/A

Python version: 3.11.9 | packaged by conda-forge | (main, Apr 19 2024, 18:34:54) [Clang 16.0.6 ] (64-bit runtime)
Python platform: macOS-14.6.1-arm64-arm-64bit
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:
Apple M2 Max

Versions of relevant libraries:
[pip3] numpy==1.26.4
[pip3] optree==0.11.0
[pip3] torch==2.3.0
[conda] Could not collect

cc @EikanWang @jgong5 @wenzhe-nrv @sanchitintel