code: def test_local_scope(idtype):
      g = dgl.graph(
          ([0, 1, 2, 3], [1, 2, 3, 4]), idtype=idtype, device=F.ctx()
      )
      g.ndata["h"] = F.zeros((g.num_nodes(), 3))
      g.edata["w"] = F.zeros((g.num_edges(), 4))
  
      # test override
      def foo(g):
          with g.local_scope():
              g.ndata["h"] = F.ones((g.num_nodes(), 3))
              g.edata["w"] = F.ones((g.num_edges(), 4))
  
      foo(g)
      assert F.allclose(g.ndata["h"], F.zeros((g.num_nodes(), 3)))
      assert F.allclose(g.edata["w"], F.zeros((g.num_edges(), 4)))
  
      # test out-place update
      def foo(g):
          with g.local_scope():
              g.nodes[[2, 3]].data["h"] = F.ones((2, 3))
              g.edges[[2, 3]].data["w"] = F.ones((2, 4))
  
      foo(g)
      assert F.allclose(g.ndata["h"], F.zeros((g.num_nodes(), 3)))
      assert F.allclose(g.edata["w"], F.zeros((g.num_edges(), 4)))
  
      # test out-place update 2
      def foo(g):
          with g.local_scope():
              g.apply_nodes(
                  lambda nodes: {"h": nodes.data["h"] + 10}, [2, 3]
              )
              g.apply_edges(
                  lambda edges: {"w": edges.data["w"] + 10}, [2, 3]
              )
  
      foo(g)
      assert F.allclose(g.ndata["h"], F.zeros((g.num_nodes(), 3)))
      assert F.allclose(g.edata["w"], F.zeros((g.num_edges(), 4)))
  
      # test auto-pop
      def foo(g):
          with g.local_scope():
              g.ndata["hh"] = F.ones((g.num_nodes(), 3))
              g.edata["ww"] = F.ones((g.num_edges(), 4))
  
      foo(g)
      assert "hh" not in g.ndata
      assert "ww" not in g.edata
  
      # test nested scope
      def foo(g):
          with g.local_scope():
              g.ndata["hh"] = F.ones((g.num_nodes(), 3))
              g.edata["ww"] = F.ones((g.num_edges(), 4))
              with g.local_scope():
                  g.ndata["hhh"] = F.ones((g.num_nodes(), 3))
                  g.edata["www"] = F.ones((g.num_edges(), 4))
              assert "hhh" not in g.ndata
              assert "www" not in g.edata
  
      foo(g)
      assert "hh" not in g.ndata
      assert "ww" not in g.edata
  
      # test initializer1
      g = dgl.graph(([0, 1], [1, 1]), idtype=idtype, device=F.ctx())
      g.set_n_initializer(dgl.init.zero_initializer)
  
      def foo(g):
          with g.local_scope():
              g.nodes[0].data["h"] = F.ones((1, 1))
              assert F.allclose(g.ndata["h"], F.tensor([[1.0], [0.0]]))
  
      foo(g)
  
      # test initializer2
      def foo_e_initializer(shape, dtype, ctx, id_range):
          return F.ones(shape)
  
      g.set_e_initializer(foo_e_initializer, field="h")
  
      def foo(g):
          with g.local_scope():
              g.edges[0, 1].data["h"] = F.ones((1, 1))
              assert F.allclose(g.edata["h"], F.ones((2, 1)))
              g.edges[0, 1].data["w"] = F.ones((1, 1))
              assert F.allclose(g.edata["w"], F.tensor([[1.0], [0.0]]))
  
      foo(g)
  
      # test exception handling
      def foo(g):
          try:
              with g.local_scope():
                  g.ndata["hh"] = F.ones((g.num_nodes(), 1))
                  # throw TypeError
                  1 + "1"
          except TypeError:
              pass
          assert "hh" not in g.ndata
  
      foo(g)
  
imports: import warnings
  from collections import defaultdict
  import backend
  import dgl
  import networkx
  import numpy
  from utils import parametrize_idtype
label_desc: Use image node_id 18 for calling a global function with example usage: test_local_scope(idtype) and returns: F