code: def test_tensor_properties(self):
      def should_keep(tensor, name):
          if inspect.isroutine(getattr(tensor, name)):
              return False
          if name.startswith("_"):
              return False
          return True
  
      tensor = torch.arange(4, dtype=torch.float).view(2, 2)
      keys = dir(tensor)
  
      # real and imag are only implemented for complex tensors.
      self.assertRaises(
          RuntimeError, lambda: should_keep(tensor, "imag")
      )
      keys.remove("imag")
  
      properties = [p for p in keys if should_keep(tensor, p)]
  
      code_template = """
          def fn(x):
              return x.{}
          """
  
      EQUALITY_MISMATCH = {
          # TorchScript doesn't have real enums so they return an int instead
          # of the actual value
          "dtype",
          "layout",
      }
      MISSING_PROPERTIES = {
          "grad_fn",
          # This is an undocumented property so it's not included
          "output_nr",
          # This has a longer implementation, maybe not worth copying to
          # TorchScript if named tensors don't work there anyways
          "names",
      }
  
      for p in properties:
          if p in MISSING_PROPERTIES:
              continue
          code = code_template.format(p)
          cu = torch.jit.CompilationUnit()
          cu.define(code)
          if p in EQUALITY_MISMATCH:
              continue
          self.assertEqual(getattr(tensor, p), cu.fn(tensor))
  
imports: import os
  import sys
  import inspect
  import unittest
  from typing import Dict, List
  import torch
  from torch.testing import FileCheck
  from torch.testing._internal.jit_utils import JitTestCase, RUN_CUDA
label_desc: Use image node_id 1 for calling the TestTensorBuiltins obj's underlying member method code with example usage: obj.test_tensor_properties() and returns: True, False, False