code: def _gym_space_contains(space: gym.Space, x: Any) -> None:
      """Strengthened version of gym.Space.contains.
      Giving more diagnostic information on why validation fails.
  
      Throw exception rather than returning true or false.
      """
      if isinstance(space, spaces.Dict):
          if not isinstance(x, dict) or len(x) != len(space):
              raise GymSpaceValidationError(
                  "Sample must be a dict with same length as space.",
                  space,
                  x,
              )
          for k, subspace in space.spaces.items():
              if k not in x:
                  raise GymSpaceValidationError(
                      f"Key {k} not found in sample.", space, x
                  )
              try:
                  _gym_space_contains(subspace, x[k])
              except GymSpaceValidationError as e:
                  raise GymSpaceValidationError(
                      f"Subspace of key {k} validation error.", space, x
                  ) from e
  
      elif isinstance(space, spaces.Tuple):
          if isinstance(x, (list, np.ndarray)):
              x = tuple(
                  x
              )  # Promote list and ndarray to tuple for contains check
          if not isinstance(x, tuple) or len(x) != len(space):
              raise GymSpaceValidationError(
                  "Sample must be a tuple with same length as space.",
                  space,
                  x,
              )
          for i, (subspace, part) in enumerate(zip(space, x)):
              try:
                  _gym_space_contains(subspace, part)
              except GymSpaceValidationError as e:
                  raise GymSpaceValidationError(
                      f"Subspace of index {i} validation error.",
                      space,
                      x,
                  ) from e
  
      else:
          if not space.contains(x):
              raise GymSpaceValidationError(
                  "Validation error reported by gym.", space, x
              )
  
imports: from __future__ import annotations
  from typing import Any, Generic, TypeVar
  import gym
  import numpy
  from gym import spaces
  from qlib.typehint import final
  from .simulator import ActType, StateType
label_desc: Use image node_id 1 for calling a global function with example usage: _gym_space_contains(space, x) without return types