class_docstr: A suite of tests for with statements.
class_docstr_tok: ["A","suite","of","tests","for","with","statements","."]
docstr: Check that errors related to with-statements are detected and reported correctly.
docstr_tok: ["Check","that","errors","related","to","with-statements","are","detected","and","reported","correctly","."]
code: def test_with_errors(self):
      """
      Check that errors related to with-statements are detected and reported correctly.
      """
  
      @torch.jit.script
      class NoEnterNoExit:
          """
          This class is missing __enter__ and __exit__ methods.
          """
  
          def __init__(self):
              self.count = 1
  
      @torch.jit.script
      class BadEnter:
          """
          This class has an __enter__ method with an incorrect signature.
          """
  
          def __init__(self):
              self.count = 1
  
          def __enter__(self, incr: int):  # noqa: PLE0302
              self.count += incr
  
          def __exit__(self, type: Any, value: Any, tb: Any):
              pass
  
      @torch.jit.script
      class BadExit:
          """
          This class has an __exit__ method with an incorrect signature.
          """
  
          def __init__(self):
              self.count = 1
  
          def __enter__(self):
              self.count += 1
  
          def __exit__(self, type: Any, value: Any):  # noqa: PLE0302
              pass
  
      @torch.jit.script
      class ExitIncorrectTypes:
          """
          This class has an __exit__ method with unsupported argument types.
          """
  
          def __init__(self):
              self.count = 1
  
          def __enter__(self):
              self.count += 1
  
          def __exit__(self, type: Any, value: int, tb: int):
              pass
  
      def test_no_enter_no_exit(
          x: torch.Tensor, cm: NoEnterNoExit
      ) -> torch.Tensor:
          with cm as _:
              pass
  
          return x
  
      def test_bad_enter(x: torch.Tensor, cm: BadEnter) -> torch.Tensor:
          with cm as _:
              pass
  
          return x
  
      def test_bad_exit(x: torch.Tensor, cm: BadExit) -> torch.Tensor:
          with cm as _:
              pass
  
          return x
  
      def test_exit_incorrect_types(
          x: torch.Tensor, cm: ExitIncorrectTypes
      ) -> torch.Tensor:
          with cm as _:
              pass
  
          return x
  
      def test_enter_without_object():
          with "not_object" as obj:
              pass
  
      test_tensor = torch.randn(5, dtype=torch.double)
  
      with self.assertRaisesRegexWithHighlight(
          RuntimeError,
          r"does not define __enter__ and __exit__ methods",
          "cm",
      ):
          self.checkScript(
              test_no_enter_no_exit, (test_tensor, NoEnterNoExit())
          )
  
      with self.assertRaisesRegexWithHighlight(
          RuntimeError,
          r"__enter__ must have only one argument and one return value",
          "cm",
      ):
          self.checkScript(test_bad_enter, (test_tensor, BadEnter()))
  
      with self.assertRaisesRegexWithHighlight(
          RuntimeError, r"__exit__ must have four arguments", "cm"
      ):
          self.checkScript(test_bad_exit, (test_tensor, BadExit()))
  
      with self.assertRaisesRegexWithHighlight(
          RuntimeError,
          r"argument 2 of __exit__ must have Any type",
          "cm",
      ):
          self.checkScript(
              test_exit_incorrect_types,
              (test_tensor, ExitIncorrectTypes()),
          )
  
      with self.assertRaisesRegexWithHighlight(
          RuntimeError, r"must return an object", '"not_object"'
      ):
          self.checkScript(test_enter_without_object, ())
  
file_path: pytorch/test/jit/test_with.py
imports: import os
  import sys
  from typing import Any, List
  import torch
  from torch.testing._internal.common_utils import skipIfTorchDynamo
  from torch.testing._internal.jit_utils import JitTestCase, make_global
label_desc: Use image node_id 4 for calling the TestWith obj's underlying member method code with example usage: obj.test_with_errors() and returns: x, x, x, x