Skip to content

[JIT] cannot access to initialized attribute in script-annotated class __getitem__ #20004

@qbx2

Description

@qbx2

🐛 Bug

@torch.jit.script
class Pair:
    def __init__(self, first, second):
        self.first = first
        self.second = second

    def sum(self):
        return self.first + self.second

    def __getitem__(self, k):
        return self.first
$ python test.py
Traceback (most recent call last):
  File "test.py", line 4, in <module>
    @torch.jit.script
  File "/Users/qbx2/anaconda3/lib/python3.7/site-packages/torch/jit/__init__.py", line 819, in script
    _jit_script_class_compile(ast, _rcb)
RuntimeError: 
Tried to access to nonexistent attribute first. Did you forget to initialize it in __init__()?:
def __getitem__(self, k):
    return self.first
           ~~~~~~~~~~ <--- HERE

Expected behavior

It should compile the class.

Environment

$ python collect_env.py
Collecting environment information...
PyTorch version: 1.1.0
Is debug build: No
CUDA used to build PyTorch: Could not collect

OS: Mac OSX 10.14.3
GCC version: Could not collect
CMake version: version 3.14.0

Python version: 3.7
Is CUDA available: No
CUDA runtime version: Could not collect
GPU models and configuration: Could not collect
Nvidia driver version: 1.1.0
cuDNN version: Probably one of the following:
/usr/local/cuda/lib/libcudnn.7.dylib
/usr/local/cuda/lib/libcudnn.dylib
/usr/local/cuda/lib/libcudnn_static.a

Versions of relevant libraries:
[pip3] numpy==1.16.2
[pip3] numpydoc==0.8.0
[pip3] torch==1.1.0
[pip3] torchvision==0.2.2
[conda] blas 1.0 mkl
[conda] mkl 2019.3 199
[conda] mkl-include 2019.3 199
[conda] mkl-service 1.1.2 py37hfbe908c_5
[conda] mkl_fft 1.0.10 py37h5e564d8_0
[conda] mkl_random 1.0.2 py37h27c97d8_0
[conda] pytorch 1.1.0 py3.7_0 pytorch
[conda] torch 1.1.0a0+6732358 dev_0
[conda] torchvision 0.2.2 py_3 pytorch

Metadata

Metadata

Assignees

Labels

high priorityoncall: jitAdd this issue/PR to JIT oncall triage queuetriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions