
    h                       S r SSKJr  SSKJrJr  SSKrSSKJ	r	  \
\\	\R                  4   \\\R                  4   \\\R                  4   \\\4   4   r\
\\	\R                  4   \\\R                  4   \\\R                  4   \\\R                  4   \\\4   4   r S	   S
S jjr S	     SS jjr  S       SS jjrg)zLContains methods for step compatibility, from old-to-new and new-to-old API.    )annotations)SupportsFloatUnionN)ObsTypec           	     *   [        U 5      S:X  a  U $ [        U 5      S:X  d   eU u  p#pEUSL a0  UR                  SS5      nUUU=(       a    U(       + U=(       a    UU4$ [        U[        5      (       az  [        R
                  " U Vs/ s H  owR                  SS5      PM     sn5      nUU[        R                  " U[        R                  " U5      5      [        R                  " XF5      U4$ [        U[        5      (       ax  [        U5      nUR                  S[        R                  " U[        S95      nUU[        R                  " U[        R                  " U5      5      [        R                  " XF5      U4$ [        S[        U5       35      es  snf )aZ  Function to transform step returns to new step API irrespective of input API.

.. py:currentmodule:: gymnasium.Env

Args:
    step_returns (tuple): Items returned by :meth:`step`. Can be ``(obs, rew, done, info)`` or ``(obs, rew, terminated, truncated, info)``
    is_vector_env (bool): Whether the ``step_returns`` are from a vector environment
      FTimeLimit.truncated)dtypefUnexpected value of infos, as is_vector_envs=False, expects `info` to be a list or dict, actual type: )lenpop
isinstancelistnparraylogical_andlogical_notdictzerosbool	TypeErrortype)	step_returnsis_vector_envobservationsrewardsdonesinfos	truncatedinfonum_envss	            `/home/james-whalen/.local/lib/python3.13/site-packages/gymnasium/utils/step_api_compatibility.py(convert_to_terminated_truncated_step_apir$      s    <A< A%%%.:+u E!		"7?I'i-#)  t$$DIJED/7EJI ubnnY&?@u0  t$$5zH		"7(RV9WXIubnnY&?@u0  xy}  D  zE  yF  G ) Ks   Fc                   [        U 5      S:X  a  U $ [        U 5      S:X  d   eU u  p#pEnUSL a0  U(       d  U(       a  U=(       a    U(       + US'   UUU=(       d    UU4$ [        U[        5      (       aS  [        XeU5       H)  u  pxn	U(       d	  U	(       d  M  U=(       a    U	(       + US'   M+     UU[        R
                  " XE5      U4$ [        U[        5      (       a  [        R
                  " [        R                  " U5      [        R                  " U5      5      (       a.  [        R                  " U[        R                  " U5      5      US'   UU[        R
                  " XE5      U4$ [        S[        U5       35      e)aZ  Function to transform step returns to old step API irrespective of input API.

.. py:currentmodule:: gymnasium.Env

Args:
    step_returns (tuple): Items returned by :meth:`step`. Can be ``(obs, rew, done, info)`` or ``(obs, rew, terminated, truncated, info)``
    is_vector_env (bool): Whether the ``step_returns`` are from a vector environment
r	   r   Fr
   r   )r   r   r   zipr   
logical_orr   anyr   r   r   r   )
r   r   r   r   
terminatedr    r   r!   env_truncatedenv_terminateds
             r#   convert_to_done_step_apir,   R   s|    <A< A%%%>J;ze E!J/8/K^+,'i	  t$$7:*83^ !NN2?2VDVD./	8 j4	  t$$}}RVVI.z0BCC/1~~r~~j90+, j4	  xy}  D  zE  yF  G     c                <    U(       a  [        X5      $ [        X5      $ )a9  Function to transform step returns to the API specified by ``output_truncation_bool``.

.. py:currentmodule:: gymnasium.Env

Done (old) step API refers to :meth:`step` method returning ``(observation, reward, done, info)``
Terminated Truncated (new) step API refers to :meth:`step` method returning ``(observation, reward, terminated, truncated, info)``
(Refer to docs for details on the API change)

Args:
    step_returns (tuple): Items returned by :meth:`step`. Can be ``(obs, rew, done, info)`` or ``(obs, rew, terminated, truncated, info)``
    output_truncation_bool (bool): Whether the output should return two booleans (new API) or one (old) (``True`` by default)
    is_vector_env (bool): Whether the ``step_returns`` are from a vector environment

Returns:
    step_returns (tuple): Depending on ``output_truncation_bool``, it can return ``(obs, rew, done, info)`` or ``(obs, rew, terminated, truncated, info)``

Example:
    This function can be used to ensure compatibility in step interfaces with conflicting API. E.g. if env is written in old API,
    wrapper is written in new API, and the final step output is desired to be in old API.

    >>> import gymnasium as gym
    >>> env = gym.make("CartPole-v0")
    >>> _, _ = env.reset()
    >>> obs, reward, done, info = step_api_compatibility(env.step(0), output_truncation_bool=False)
    >>> obs, reward, terminated, truncated, info = step_api_compatibility(env.step(0), output_truncation_bool=True)

    >>> vec_env = gym.make_vec("CartPole-v0", vectorization_mode="sync")
    >>> _, _ = vec_env.reset()
    >>> obs, rewards, dones, infos = step_api_compatibility(vec_env.step([0]), is_vector_env=True, output_truncation_bool=False)
    >>> obs, rewards, terminations, truncations, infos = step_api_compatibility(vec_env.step([0]), is_vector_env=True, output_truncation_bool=True)

)r$   r,   )r   output_truncation_boolr   s      r#   step_api_compatibilityr0      s    J 7TT'DDr-   )F)r   z*DoneStepType | TerminatedTruncatedStepTypereturnTerminatedTruncatedStepType)r   *TerminatedTruncatedStepType | DoneStepTyper   r   r1   DoneStepType)TF)r   r3   r/   r   r   r   r1   r3   )__doc__
__future__r   typingr   r   numpyr   gymnasium.corer   tuplendarrayr   r   r   r4   r2   r$   r,   r0    r-   r#   <module>r=      s>   R " '  " 	'2::
	-
#$	$


	$* $	'2::
	-
#$	$


	$


	$*	  MR3<3 3p  6<66 6v $((E<(E (E (E 0	(Er-   