class_docstr: This class tests the functionality of `Union`.
  
  Note: It's important to be able to refine the type of a `Union` to
  one of its internal types. Currently, there are differences in the
  way Python expects `isinstance` checks and the way TorchScript
  expects `isinstance` checks. This means that we can't use
  `checkScript` in our test cases because either the eager mode or the
  script mode wouldn't run! So, some test cases have separate but
  equivalent functions to emulate `checkScript`.
class_docstr_tok: ["This","class","tests","the","functionality","of","`","Union","`",".","Note",":","It","'s","important","to","be","able","to","refine","the","type","of","a","`","Union","`","to","one","of","its","internal","types",".","Currently",",","there","are","differences","in","the","way","Python","expects","`","isinstance","`","checks","and","the","way","TorchScript","expects","`","isinstance","`","checks",".","This","means","that","we","ca","n't","use","`","checkScript","`","in","our","test","cases","because","either","the","eager","mode","or","the","script","mode","would","n't","run","!","So",",","some","test","cases","have","separate","but","equivalent","functions","to","emulate","`","checkScript","`","."]
code: def test_union_optional_of_union_is_flattened(self):
      @torch.jit.script
      def fn(flag: int) -> str | int | None:
          y: int | str | None = "foo"
          if flag == 0:
              x: Optional[int | str] = y
          elif flag == 1:
              x: Optional[int | str] = 1
          else:
              x: Optional[int | str] = None
          return x
  
      # Can't use `checkScript` because it will flag the fact that
      # the original code has `Optional[Union[int, str]]` but the
      # saved/loaded code has `Union[int, NoneType, str]` (even
      # though this is exactly what we want)
      self.assertEqual(fn(0), "foo")
      self.assertEqual(fn(1), 1)
      self.assertEqual(fn(2), None)
  
      buffer = io.BytesIO()
      torch.jit.save(fn, buffer)
      buffer = io.BytesIO(buffer.getvalue())
      l = torch.jit.load(buffer)
  
      s = l.code
  
      FileCheck().check("Union[int, NoneType, str]").check(
          "Union[int, NoneType, str]"
      ).run(s)
  
imports: import io
  import os
  import sys
  import unittest
  from enum import Enum
  from textwrap import dedent
  from typing import Dict, List, Optional, Tuple, Union
  import torch
  from torch.testing import FileCheck
  from torch.testing._internal.jit_utils import JitTestCase, make_global
label_desc: Use image node_id 25 for calling the TestUnion obj's underlying member method code with example usage: obj.test_union_optional_of_union_is_flattened() and returns: x