docstr: Test that methods on class types can have default arguments.
docstr_tok: ["Test","that","methods","on","class","types","can","have","default","arguments","."]
code: def test_default_args(self):
      """
      Test that methods on class types can have default arguments.
      """
  
      @torch.jit.script
      class ClassWithDefaultArgs:
          def __init__(
              self,
              a: int = 1,
              b: Optional[List[int]] = None,
              c: Tuple[int, int, int] = (1, 2, 3),
              d: Optional[Dict[int, int]] = None,
              e: Optional[str] = None,
          ):
              self.int = a
              self.tup = c
              self.str = e
  
              self.list = [1, 2, 3]
              if b is not None:
                  self.list = b
  
              self.dict = {1: 2, 3: 4}
              if d is not None:
                  self.dict = d
  
          def add(self, b: int, scale: float = 1.0) -> float:
              return self.int * scale + b
  
      def all_defaults() -> int:
          obj: ClassWithDefaultArgs = ClassWithDefaultArgs()
          return obj.int + obj.list[2] + obj.tup[1]
  
      def some_defaults() -> int:
          obj: ClassWithDefaultArgs = ClassWithDefaultArgs(b=[5, 6, 7])
          return obj.int + obj.list[2] + obj.dict[1]
  
      def override_defaults() -> int:
          obj: ClassWithDefaultArgs = ClassWithDefaultArgs(
              3, [9, 10, 11], (12, 13, 14), {3: 4}, "str"
          )
          s: int = obj.int
  
          for x in obj.list:
              s += x
  
          for y in obj.tup:
              s += y
  
          s += obj.dict[3]
  
          st = obj.str
          if st is not None:
              s += len(st)
  
          return s
  
      def method_defaults() -> float:
          obj: ClassWithDefaultArgs = ClassWithDefaultArgs()
          return obj.add(3) + obj.add(3, 0.25)
  
      self.checkScript(all_defaults, ())
      self.checkScript(some_defaults, ())
      self.checkScript(override_defaults, ())
      self.checkScript(method_defaults, ())
  
      # The constructor of this class below has some arguments without default values.
      class ClassWithSomeDefaultArgs:  # noqa: B903
          def __init__(
              self,
              a: int,
              b: int = 1,
          ):
              self.a = a
              self.b = b
  
      def default_b() -> int:
          obj: ClassWithSomeDefaultArgs = ClassWithSomeDefaultArgs(1)
          return obj.a + obj.b
  
      def set_b() -> int:
          obj: ClassWithSomeDefaultArgs = ClassWithSomeDefaultArgs(1, 4)
          return obj.a + obj.b
  
      self.checkScript(default_b, ())
      self.checkScript(set_b, ())
  
      # The constructor of this class below has mutable arguments. This should throw
      # an error.
      class ClassWithMutableArgs:  # noqa: B903
          def __init__(
              self,
              a: List[int] = [1, 2, 3],  # noqa: B006
          ):
              self.a = a
  
      def should_fail():
          obj: ClassWithMutableArgs = ClassWithMutableArgs()
  
      with self.assertRaisesRegexWithHighlight(
          RuntimeError,
          "Mutable default parameters are not supported",
          "",
      ):
          torch.jit.script(should_fail)
  
imports: import io
  import os
  import sys
  import unittest
  import torch
  import torch.nn
  from torch.testing import FileCheck
  from typing import Any
  from torch.testing._internal.jit_utils import JitTestCase, make_global
  import torch.testing._internal.jit_utils
  from torch.testing._internal.common_utils import IS_SANDCASTLE, skipIfTorchDynamo
  from typing import List, Tuple, Iterable, Optional, Dict
label_desc: Use image node_id 35 for calling the TestClassType obj's underlying member method code with example usage: obj.test_default_args() and returns: unknown, unknown, s, unknown, unknown, unknown, unknown