Repository: pytorch
Issue: torch.package fails to import if dataclass and __future__.annotations present
Labels: oncall: package/deploy,imported

### 🐛 Describe the bug

The `torch.package` fails to import model if `dataclass` and `__future__.annotations` present.
Here is a minimal example with two files:

`save.py`:
```python
from __future__ import annotations
from dataclasses import dataclass
import torch

@dataclass
class DC:
    a: int

class Module:
    def __init__(self, a: DC) -> None:
        self._module = torch.nn.Linear(a.a, a.a)
    
    def return_self(self) -> Module:
        return self

if __name__ == "__main__":
    model = Module(DC(3))
    with torch.package.PackageExporter("package.pt") as pe:
        pe.extern("__future__")
        pe.intern("__main__")
        pe.save_pickle("my_model", "model.pkl", model)
```

`load.py`:
```python
import torch
pi = torch.package.PackageImporter("package.pt")
model = pi.load_pickle("my_model", "model.pkl")
print(model)
```

The command `python save.py` would proceed normally without any error, however, when executing `python load.py`, I got this error:
```
Traceback (most recent call last):
  File "/home/ubuntu/git/sldense/sldense1/fitters/load.py", line 5, in <module>
    model = pi.load_pickle("my_model", "model.pkl")
  File "/home/ubuntu/anaconda3/envs/py39/lib/python3.9/site-packages/torch/package/package_importer.py", line 254, in load_pickle
    result = unpickler.load()
  File "/home/ubuntu/anaconda3/envs/py39/lib/python3.9/pickle.py", line 1212, in load
    dispatch[key[0]](self)
  File "/home/ubuntu/anaconda3/envs/py39/lib/python3.9/pickle.py", line 1528, in load_global
    klass = self.find_class(module, name)
  File "/home/ubuntu/anaconda3/envs/py39/lib/python3.9/site-packages/torch/package/_package_unpickler.py", line 25, in find_class
    mod = self._importer.import_module(module)
  File "/home/ubuntu/anaconda3/envs/py39/lib/python3.9/site-packages/torch/package/package_importer.py", line 132, in import_module
    return self._gcd_import(name)
  File "/home/ubuntu/anaconda3/envs/py39/lib/python3.9/site-packages/torch/package/package_importer.py", line 443, in _gcd_import
    return self._find_and_load(name)
  File "/home/ubuntu/anaconda3/envs/py39/lib/python3.9/site-packages/torch/package/package_importer.py", line 413, in _find_and_load
    return self._do_find_and_load(name)
  File "/home/ubuntu/anaconda3/envs/py39/lib/python3.9/site-packages/torch/package/package_importer.py", line 403, in _do_find_and_load
    module = self._load_module(name, parent)
  File "/home/ubuntu/anaconda3/envs/py39/lib/python3.9/site-packages/torch/package/package_importer.py", line 353, in _load_module
    return self._make_module(name, cur.source_file, isinstance(cur, _PackageNode), parent)  # type: ignore[attr-defined]
  File "/home/ubuntu/anaconda3/envs/py39/lib/python3.9/site-packages/torch/package/package_importer.py", line 336, in _make_module
    exec(code, ns)
  File "<torch_package_0>.__main__.py", line 6, in <module>
  File "/home/ubuntu/anaconda3/envs/py39/lib/python3.9/dataclasses.py", line 1021, in dataclass
    return wrap(cls)
  File "/home/ubuntu/anaconda3/envs/py39/lib/python3.9/dataclasses.py", line 1013, in wrap
    return _process_class(cls, init, repr, eq, order, unsafe_hash, frozen)
  File "/home/ubuntu/anaconda3/envs/py39/lib/python3.9/dataclasses.py", line 863, in _process_class
    cls_fields = [_get_field(cls, name, type)
  File "/home/ubuntu/anaconda3/envs/py39/lib/python3.9/dataclasses.py", line 863, in <listcomp>
    cls_fields = [_get_field(cls, name, type)
  File "/home/ubuntu/anaconda3/envs/py39/lib/python3.9/dataclasses.py", line 714, in _get_field
    and _is_type(f.type, cls, typing, typing.ClassVar,
  File "/home/ubuntu/anaconda3/envs/py39/lib/python3.9/dataclasses.py", line 660, in _is_type
    ns = sys.modules.get(cls.__module__).__dict__
AttributeError: 'NoneType' object has no attribute '__dict__'
```

Some points that I have tried:

* Removing the first line `from __future__ import annotations` and changing `def return_self(self) -> Module` to `def return_self(self) -> "Module"` solves the problem
* Removing the `dataclass` also solves the problem
* Changing `pe.export("__future__")` to `pe.import("__future__")` does not make the error disappear

For a small code, this is not a problem, as I can just remove all the `__future__.annotations` import and adjust the types into strings.
However, if one uses a library a large codebase that includes `__future__.annotations`, removing those is a big task.

### Versions

```
PyTorch version: 1.11.0+cu113
Is debug build: False
CUDA used to build PyTorch: 11.3
ROCM used to build PyTorch: N/A

OS: Ubuntu 18.04.5 LTS (x86_64)
GCC version: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Clang version: Could not collect
CMake version: version 3.21.2
Libc version: glibc-2.27

Python version: 3.9.7 | packaged by conda-forge | (default, Sep 29 2021, 19:23:11)  [GCC 9.4.0] (64-bit runtime)
Python platform: Linux-5.4.0-1069-aws-x86_64-with-glibc2.27
Is CUDA available: True
CUDA runtime version: 11.6.55
GPU models and configuration: GPU 0: Tesla T4
Nvidia driver version: 510.47.03
cuDNN version: Probably one of the following:
/usr/local/cuda-10.1/targets/x86_64-linux/lib/libcudnn.so.7.6.5
/usr/local/cuda-10.2/targets/x86_64-linux/lib/libcudnn.so.7.6.5
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn.so.8.0.5
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_adv_infer.so.8.0.5
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_adv_train.so.8.0.5
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_cnn_infer.so.8.0.5
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_cnn_train.so.8.0.5
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_ops_infer.so.8.0.5
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_ops_train.so.8.0.5
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn.so.8.0.5
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_adv_infer.so.8.0.5
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_adv_train.so.8.0.5
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_cnn_infer.so.8.0.5
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_cnn_train.so.8.0.5
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_ops_infer.so.8.0.5
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_ops_train.so.8.0.5
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

Versions of relevant libraries:
[pip3] functorch==0.1.0
[pip3] mypy==0.941
[pip3] mypy-extensions==0.4.3
[pip3] numpy==1.21.2
[pip3] pytorch-lightning==1.3.0
[pip3] torch==1.11.0+cu113
[pip3] torch-geometric==2.0.3
[pip3] torchaudio==0.11.0+cu113
[pip3] torchmetrics==0.5.1
[pip3] torchvision==0.12.0+cu113
[conda] blas                      1.0                         mkl  
[conda] cudatoolkit               11.3.1               h2bc3f7f_2  
[conda] ffmpeg                    4.3                  hf484d3e_0    pytorch
[conda] functorch                 0.1.0                    pypi_0    pypi
[conda] mkl                       2021.4.0           h06a4308_640  
[conda] mkl-service               2.4.0            py39h7e14d7c_0    conda-forge
[conda] mkl_fft                   1.3.1            py39h0c7bc48_1    conda-forge
[conda] mkl_random                1.2.2            py39hde0f152_0    conda-forge
[conda] mypy                      0.941                    pypi_0    pypi
[conda] mypy_extensions           0.4.3            py39hf3d152e_4    conda-forge
[conda] numpy                     1.21.2           py39h20f2e39_0  
[conda] numpy-base                1.21.2           py39h79a1101_0  
[conda] pytorch                   1.11.0          py3.9_cuda11.3_cudnn8.2.0_0    pytorch
[conda] pytorch-lightning         1.3.0                    pypi_0    pypi
[conda] pytorch-mutex             1.0                        cuda    pytorch
[conda] torch                     1.11.0.dev20211229+cu111          pypi_0    pypi
[conda] torch-geometric           2.0.3                    pypi_0    pypi
[conda] torchaudio                0.11.0+cu113             pypi_0    pypi
[conda] torchmetrics              0.5.1                    pypi_0    pypi
[conda] torchvision               0.12.0+cu113             pypi_0    pypi
```