code: def test_missing(self):
      # Make sure UserDict doesn't have a __missing__ method
      self.assertEqual(
          hasattr(collections.UserDict, "__missing__"), False
      )
  
      # Test several cases:
      # (D) subclass defines __missing__ method returning a value
      # (E) subclass defines __missing__ method raising RuntimeError
      # (F) subclass sets __missing__ instance variable (no effect)
      # (G) subclass doesn't define __missing__ at all
      class D(collections.UserDict):
          def __missing__(self, key):
              return 42
  
      d = D({1: 2, 3: 4})
      self.assertEqual(d[1], 2)
      self.assertEqual(d[3], 4)
      self.assertNotIn(2, d)
      self.assertNotIn(2, d.keys())
      self.assertEqual(d[2], 42)
  
      class E(collections.UserDict):
          def __missing__(self, key):
              raise RuntimeError(key)
  
      e = E()
      try:
          e[42]
      except RuntimeError as err:
          self.assertEqual(err.args, (42,))
      else:
          self.fail("e[42] didn't raise RuntimeError")
  
      class F(collections.UserDict):
          def __init__(self):
              # An instance variable __missing__ should have no effect
              self.__missing__ = lambda key: None
              collections.UserDict.__init__(self)
  
      f = F()
      try:
          f[42]
      except KeyError as err:
          self.assertEqual(err.args, (42,))
      else:
          self.fail("f[42] didn't raise KeyError")
  
      class G(collections.UserDict):
          pass
  
      g = G()
      try:
          g[42]
      except KeyError as err:
          self.assertEqual(err.args, (42,))
      else:
          self.fail("g[42] didn't raise KeyError")
  
file_path: cpython/Lib/test/test_userdict.py
imports: from test import mapping_tests, support
  import unittest
  import collections
label_desc: Use image node_id 4 for calling the UserDictTest obj's underlying member method code with example usage: obj.test_missing() and returns: int