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_serialization_preserves_type_annotations(self):
      # This function will fail after being torch.jit.save'd and
      # torch.jit.load'd if the type annotations aren't preserved
      # for Union during serialization. We need the `Union[str, int]`
      # annotation to make sure that `y` is typed as a Union instead
      # of as a str in one branch and an int in the other
      def fn(x: int) -> str:
          if x % 2:
              y: Union[str, int] = "bar"
          else:
              y: Union[str, int] = x
          if isinstance(y, str):
              return y
          else:
              return "baz"
  
      self.checkScript(fn, (1,))
      self.checkScript(fn, (8,))
  
imports: import io
  import os
  import sys
  import torch
  from torch.testing import FileCheck
  from enum import Enum
  from textwrap import dedent
  from typing import Dict, List, Optional, Tuple, Union
  from torch.testing._internal.jit_utils import JitTestCase, make_global
label_desc: Use image node_id 47 for calling the TestUnion obj's underlying member method code with example usage: obj.test_union_serialization_preserves_type_annotations() and returns: y, str