code: def flatten_space(
      space: Space,
  ) -> Union[Dict, Sequence, Tuple, Graph]:
      """Flatten a space into a space that is as flat as possible.
  
      This function will attempt to flatten `space` into a single :class:`Box` space.
      However, this might not be possible when `space` is an instance of :class:`Graph`,
      :class:`Sequence` or a compound space that contains a :class:`Graph` or :class:`Sequence`space.
      This is equivalent to :func:`flatten`, but operates on the space itself. The
      result for non-graph spaces is always a `Box` with flat boundaries. While
      the result for graph spaces is always a `Graph` with `node_space` being a `Box`
      with flat boundaries and `edge_space` being a `Box` with flat boundaries or
      `None`. The box has exactly :func:`flatdim` dimensions. Flattening a sample
      of the original space has the same effect as taking a sample of the flattenend
      space.
  
      Example::
  
          >>> box = Box(0.0, 1.0, shape=(3, 4, 5))
          >>> box
          Box(3, 4, 5)
          >>> flatten_space(box)
          Box(60,)
          >>> flatten(box, box.sample()) in flatten_space(box)
          True
  
      Example that flattens a discrete space::
  
          >>> discrete = Discrete(5)
          >>> flatten_space(discrete)
          Box(5,)
          >>> flatten(box, box.sample()) in flatten_space(box)
          True
  
      Example that recursively flattens a dict::
  
          >>> space = Dict({"position": Discrete(2), "velocity": Box(0, 1, shape=(2, 2))})
          >>> flatten_space(space)
          Box(6,)
          >>> flatten(space, space.sample()) in flatten_space(space)
          True
  
  
      Example that flattens a graph::
  
          >>> space = Graph(node_space=Box(low=-100, high=100, shape=(3, 4)), edge_space=Discrete(5))
          >>> flatten_space(space)
          Graph(Box(-100.0, 100.0, (12,), float32), Box(0, 1, (5,), int64))
          >>> flatten(space, space.sample()) in flatten_space(space)
          True
  
      Args:
          space: The space to flatten
  
      Returns:
          A flattened Box
  
      Raises:
          NotImplementedError: if the space is not defined in ``gym.spaces``.
      """
      raise NotImplementedError(f"Unknown space: `{space}`")
  
imports: import operator
  from collections import OrderedDict
  from functools import reduce, singledispatch
  from typing import Dict
  from typing import TypeVar, Union, cast
  import numpy
  from gym.spaces import Box, Dict, Discrete, Graph, GraphInstance, MultiBinary, MultiDiscrete, Sequence, Space, Text, Tuple
label_desc: Use image node_id 27 for calling a global function with example usage: flatten_space(space) without return types