Repository: pytorch
Issue: When TorchScripted module has bad type annotation you get bad error message
Labels: oncall: jit,module: bootcamp,triaged

Suppose that you incorrectly annotated bias in Conv as `Tensor` rather than `Optional[Tensor]`. When you script it you get this error:

```
              
======================================================================                                  
ERROR: test_dropout_eval (__main__.TestScript)                                                                
----------------------------------------------------------------------                                        
Traceback (most recent call last):                                                                            
  File "test/test_jit.py", line 8581, in test_dropout_eval                                                    
    script_mod = ScriptMod()                                                                                  
  File "/data/users/ezyang/pytorch-tmp/torch/jit/__init__.py", line 1528, in init_then_script    
    original_init(self, *args, **kwargs)                                                                      
  File "test/test_jit.py", line 8550, in __init__                                                             
    self.Conv2d_1a_3x3 = ScriptedConv2d(3, 32, kernel_size=3, stride=2)                                       
  File "/data/users/ezyang/pytorch-tmp/torch/jit/__init__.py", line 1534, in init_then_script                 
    self.__dict__["_actual_script_module"] = torch.jit._recursive.create_script_module(self, make_stubs)        File "/data/users/ezyang/pytorch-tmp/torch/jit/_recursive.py", line 305, in create_script_module                return create_script_module_impl(nn_module, concrete_type, stubs_fn)                                        File "/data/users/ezyang/pytorch-tmp/torch/jit/_recursive.py", line 360, in create_script_module_impl           script_module = torch.jit.RecursiveScriptModule._construct(cpp_module, init_fn)                           
  File "/data/users/ezyang/pytorch-tmp/torch/jit/__init__.py", line 1673, in _construct                       
    init_fn(script_module)                                                                                    
  File "/data/users/ezyang/pytorch-tmp/torch/jit/_recursive.py", line 341, in init_fn                             scripted = create_script_module_impl(orig_value, sub_concrete_type, infer_methods_to_compile)               File "/data/users/ezyang/pytorch-tmp/torch/jit/_recursive.py", line 360, in create_script_module_impl           script_module = torch.jit.RecursiveScriptModule._construct(cpp_module, init_fn)                             File "/data/users/ezyang/pytorch-tmp/torch/jit/__init__.py", line 1673, in _construct             
    init_fn(script_module)                                                                                    
  File "/data/users/ezyang/pytorch-tmp/torch/jit/_recursive.py", line 326, in init_fn                             cpp_module.setattr(name, orig_value)                                                                      RuntimeError: Unable to cast Python instance of type <class 'NoneType'> to C++ type 'at::Tensor'       
```

This is a super useless error message. It doesn't tell you any context about the error in question.

cc @suo