2022-09-08 10:10:07 +01:00
""" Set of Error classes for gymnasium. """
2022-05-10 15:35:45 +01:00
import warnings
2016-04-27 08:00:58 -07:00
class Error ( Exception ) :
2022-05-10 15:35:45 +01:00
""" Error superclass. """
2016-04-27 08:00:58 -07:00
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
# Local errors
2021-07-29 02:26:34 +02:00
2016-09-23 01:04:26 -07:00
class Unregistered ( Error ) :
2022-05-10 15:35:45 +01:00
""" Raised when the user requests an item from the registry that does not actually exist. """
2016-09-23 01:04:26 -07:00
2021-07-29 02:26:34 +02:00
2016-09-23 01:04:26 -07:00
class UnregisteredEnv ( Unregistered ) :
2022-05-10 15:35:45 +01:00
""" Raised when the user requests an env from the registry that does not actually exist. """
2016-09-23 01:04:26 -07:00
2021-07-29 02:26:34 +02:00
2022-01-19 13:50:25 -05:00
class NamespaceNotFound ( UnregisteredEnv ) :
2022-05-10 15:35:45 +01:00
""" Raised when the user requests an env from the registry where the namespace doesn ' t exist. """
2022-01-19 13:50:25 -05:00
class NameNotFound ( UnregisteredEnv ) :
2022-05-10 15:35:45 +01:00
""" Raised when the user requests an env from the registry where the name doesn ' t exist. """
2022-01-19 13:50:25 -05:00
class VersionNotFound ( UnregisteredEnv ) :
2022-05-10 15:35:45 +01:00
""" Raised when the user requests an env from the registry where the version doesn ' t exist. """
2022-01-19 13:50:25 -05:00
2016-09-23 01:04:26 -07:00
class UnregisteredBenchmark ( Unregistered ) :
2022-05-10 15:35:45 +01:00
""" Raised when the user requests an env from the registry that does not actually exist. """
2016-04-27 08:00:58 -07:00
2021-07-29 02:26:34 +02:00
2016-05-18 02:23:43 -07:00
class DeprecatedEnv ( Error ) :
2022-05-10 15:35:45 +01:00
""" Raised when the user requests an env from the registry with an older version number than the latest env with the same name. """
2016-05-29 09:07:09 -07:00
2021-07-29 02:26:34 +02:00
2022-01-19 13:50:25 -05:00
class RegistrationError ( Error ) :
2022-05-10 15:35:45 +01:00
""" Raised when the user attempts to register an invalid env. For example, an unversioned env when a versioned env exists. """
2022-01-19 13:50:25 -05:00
2016-05-29 09:07:09 -07:00
class UnseedableEnv ( Error ) :
2022-05-10 15:35:45 +01:00
""" Raised when the user tries to seed an env that does not support seeding. """
2016-05-18 02:23:43 -07:00
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
class DependencyNotInstalled ( Error ) :
2022-05-10 15:35:45 +01:00
""" Raised when the user has not installed a dependency. """
2016-04-27 08:00:58 -07:00
2021-07-29 02:26:34 +02:00
2022-05-10 15:35:45 +01:00
class UnsupportedMode ( Error ) :
""" Raised when the user requests a rendering mode not supported by the environment. """
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
2022-11-28 14:34:32 +01:00
class InvalidMetadata ( Error ) :
""" Raised when the metadata of an environment is not valid. """
2022-05-10 15:35:45 +01:00
class ResetNeeded ( Error ) :
""" When the order enforcing is violated, i.e. step or render is called before reset. """
2021-07-29 02:26:34 +02:00
2022-05-10 15:35:45 +01:00
class ResetNotAllowed ( Error ) :
2022-07-10 02:18:06 +05:30
""" When the monitor is active, raised when the user tries to step an environment that ' s not yet terminated or truncated. """
2016-04-27 08:00:58 -07:00
2021-07-29 02:26:34 +02:00
2022-05-10 15:35:45 +01:00
class InvalidAction ( Error ) :
""" Raised when the user performs an action not contained within the action space. """
2016-05-04 11:40:24 +10:00
2021-07-29 02:26:34 +02:00
2022-10-20 11:30:14 +02:00
class MissingArgument ( Error ) :
""" Raised when a required argument in the initializer is missing. """
2022-12-02 01:04:34 +01:00
class InvalidProbability ( Error ) :
""" Raised when given an invalid value for a probability. """
2022-11-20 00:57:10 +01:00
class InvalidBound ( Error ) :
""" Raised when the clipping an array with invalid upper and/or lower bound. """
2016-04-27 08:00:58 -07:00
# API errors
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
class APIError ( Error ) :
2022-09-08 10:10:07 +01:00
""" Deprecated, to be removed at gymnasium 1.0. """
2022-05-10 15:35:45 +01:00
2021-07-29 02:26:34 +02:00
def __init__ (
self ,
message = None ,
http_body = None ,
http_status = None ,
json_body = None ,
headers = None ,
) :
2022-05-10 15:35:45 +01:00
""" Initialise API error. """
2021-11-14 14:50:40 +01:00
super ( ) . __init__ ( message )
2016-04-27 08:00:58 -07:00
2022-09-08 10:10:07 +01:00
warnings . warn ( " APIError is deprecated and will be removed at gymnasium 1.0 " )
2022-05-10 15:35:45 +01:00
2021-07-29 02:26:34 +02:00
if http_body and hasattr ( http_body , " decode " ) :
2016-04-27 08:00:58 -07:00
try :
2021-07-29 02:26:34 +02:00
http_body = http_body . decode ( " utf-8 " )
Update the flake8 pre-commit ignores (#2778)
* Remove additional ignores from flake8
* Remove all unused imports
* Remove all unused imports
* Update flake8 and pyupgrade
* F841, removed unused variables
* E731, removed lambda assignment to variables
* Remove E731, F403, F405, F524
* Remove E722, bare exceptions
* Remove E712, compare variable == True or == False to is True or is False
* Remove E402, module level import not at top of file
* Added --pre-file-ignores
* Add --per-file-ignores removing E741, E302 and E704
* Add E741, do not use variables named ‘l’, ‘O’, or ‘I’ to ignore issues in classic control
* Fixed issues for pytest==6.2
* Remove unnecessary # noqa
* Edit comment with the removal of E302
* Added warnings and declared module, attr for pyright type hinting
* Remove unused import
* Removed flake8 E302
* Updated flake8 from 3.9.2 to 4.0.1
* Remove unused variable
2022-04-26 16:18:37 +01:00
except Exception :
2021-11-20 11:41:27 -05:00
http_body = " <Could not decode body as utf-8.> "
2016-04-27 08:00:58 -07:00
self . _message = message
self . http_body = http_body
self . http_status = http_status
self . json_body = json_body
self . headers = headers or { }
2021-07-29 02:26:34 +02:00
self . request_id = self . headers . get ( " request-id " , None )
2016-04-27 08:00:58 -07:00
def __unicode__ ( self ) :
2022-05-10 15:35:45 +01:00
""" Returns a string, if request_id is not None then make message other use the _message. """
2016-04-27 08:00:58 -07:00
if self . request_id is not None :
msg = self . _message or " <empty message> "
2021-11-14 14:50:40 +01:00
return f " Request { self . request_id } : { msg } "
2016-04-27 08:00:58 -07:00
else :
return self . _message
2018-02-03 08:02:55 +01:00
def __str__ ( self ) :
2022-05-10 15:35:45 +01:00
""" Returns the __unicode__. """
2022-05-09 16:22:49 +01:00
return self . __unicode__ ( )
2016-04-27 08:00:58 -07:00
class APIConnectionError ( APIError ) :
2022-09-08 10:10:07 +01:00
""" Deprecated, to be removed at gymnasium 1.0. """
2016-04-27 08:00:58 -07:00
class InvalidRequestError ( APIError ) :
2022-09-08 10:10:07 +01:00
""" Deprecated, to be removed at gymnasium 1.0. """
2022-05-10 15:35:45 +01:00
2021-07-29 02:26:34 +02:00
def __init__ (
self ,
message ,
param ,
http_body = None ,
http_status = None ,
json_body = None ,
headers = None ,
) :
2022-05-10 15:35:45 +01:00
""" Initialises the invalid request error. """
2021-11-14 14:50:40 +01:00
super ( ) . __init__ ( message , http_body , http_status , json_body , headers )
2016-04-27 08:00:58 -07:00
self . param = param
class AuthenticationError ( APIError ) :
2022-09-08 10:10:07 +01:00
""" Deprecated, to be removed at gymnasium 1.0. """
2016-04-27 08:00:58 -07:00
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
class RateLimitError ( APIError ) :
2022-09-08 10:10:07 +01:00
""" Deprecated, to be removed at gymnasium 1.0. """
2016-04-27 08:00:58 -07:00
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
# Video errors
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
class VideoRecorderError ( Error ) :
2022-05-10 15:35:45 +01:00
""" Unused error. """
2016-04-27 08:00:58 -07:00
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
class InvalidFrame ( Error ) :
2022-05-10 15:35:45 +01:00
""" Error message when an invalid frame is captured. """
2016-12-23 16:21:42 -08:00
2021-07-29 02:26:34 +02:00
2016-12-23 16:21:42 -08:00
# Wrapper errors
2021-07-29 02:26:34 +02:00
2016-12-23 16:21:42 -08:00
class DoubleWrapperError ( Error ) :
2022-05-10 15:35:45 +01:00
""" Error message for when using double wrappers. """
2016-12-27 18:28:29 -08:00
class WrapAfterConfigureError ( Error ) :
2022-05-10 15:35:45 +01:00
""" Error message for using wrap after configure. """
2017-03-05 19:54:09 -08:00
class RetriesExceededError ( Error ) :
2022-05-10 15:35:45 +01:00
""" Error message for retries exceeding set number. """
2019-06-21 17:29:44 -04:00
2021-07-29 02:26:34 +02:00
2023-04-16 13:04:55 +02:00
class DeprecatedWrapper ( ImportError ) :
""" Error message for importing an old version of a wrapper. """
2019-06-21 17:29:44 -04:00
# Vectorized environments errors
2021-07-29 02:26:34 +02:00
2019-06-21 17:29:44 -04:00
class AlreadyPendingCallError ( Exception ) :
2022-05-10 15:35:45 +01:00
""" Raised when `reset`, or `step` is called asynchronously (e.g. with `reset_async`, or `step_async` respectively), and `reset_async`, or `step_async` (respectively) is called again (without a complete call to `reset_wait`, or `step_wait` respectively). """
def __init__ ( self , message : str , name : str ) :
""" Initialises the exception with name attributes. """
2021-11-14 14:50:40 +01:00
super ( ) . __init__ ( message )
2019-06-21 17:29:44 -04:00
self . name = name
2021-07-29 02:26:34 +02:00
2019-06-21 17:29:44 -04:00
class NoAsyncCallError ( Exception ) :
2022-05-10 15:35:45 +01:00
""" Raised when an asynchronous `reset`, or `step` is not running, but `reset_wait`, or `step_wait` (respectively) is called. """
2021-07-29 02:26:34 +02:00
2022-05-10 15:35:45 +01:00
def __init__ ( self , message : str , name : str ) :
""" Initialises the exception with name attributes. """
2021-11-14 14:50:40 +01:00
super ( ) . __init__ ( message )
2019-06-21 17:29:44 -04:00
self . name = name
2021-07-29 02:26:34 +02:00
2019-06-21 17:29:44 -04:00
class ClosedEnvironmentError ( Exception ) :
2022-05-10 15:35:45 +01:00
""" Trying to call `reset`, or `step`, while the environment is closed. """
2020-09-21 22:38:51 +02:00
2021-07-29 02:26:34 +02:00
2020-09-21 22:38:51 +02:00
class CustomSpaceError ( Exception ) :
2022-09-08 10:10:07 +01:00
""" The space is a custom gymnasium.Space instance, and is not supported by `AsyncVectorEnv` with `shared_memory=True`. """