Table of Contents

Class InfoExtensions

Namespace
Gym.Envs
Assembly
Gym.dll

Typed reads of an info dict (DR-78): info.Get<double>("x_position") reads a value as the .NET type the caller wants, converting what Gymnasium's envs store (NumPy scalars, which arrive as 0-d arrays, and Python numbers) where a plain cast would throw.

public static class InfoExtensions
Inheritance
InfoExtensions
Inherited Members

Remarks

An extension rather than a member of Info, so it reads any string-keyed dict the same way: an Info, a Metadata, a typed info record's dict (EnvInfo). Python's dict has no such method; it stands for float(info["x_position"]).

The conversions keep every value: a float32 or int scalar read as double widens exactly, an integer read as a narrower integer is range-checked (OverflowException), and a float is never read as an integer unless it is a whole number. Anything else, a non-0-d array read as a number included, is refused with InvalidCastException.

Methods

Get<T>(IReadOnlyDictionary<string, object?>, string)

The value under key read as T, or default(T) when the key is missing: info.Get<double>("x_position"), info.Get<NDArray>("action_mask"), info.Get<double?>("x_velocity") (null on a reset's info, which lacks it).

public static T? Get<T>(this IReadOnlyDictionary<string, object?> info, string key)

Parameters

info IReadOnlyDictionary<string, object>

The dict.

key string

The key.

Returns

T

The value as T, or default(T) when the key is missing. Footgun: a missing key and a stored default read the same (0.0 for double); read a Nullable<T>, or check ContainsKey, to tell them apart. An array read as NDArray is the info's own (it is disposed with the step result that holds it); a number read as NDArray is a new array the caller owns.

Type Parameters

T

The type to read as: the stored value's own type, a numeric type for a number or a NumPy scalar (a 0-d array), bool for a bool or a NumPy bool, NDArray for an array or a number (a new 0-d array then), or any of these as Nullable<T>.

Exceptions

ArgumentNullException

Thrown when info or key is null.

InvalidCastException

Thrown when the value cannot be read as T: another kind of value, a float with a fraction read as an integer, an array of more than one element read as a number, or None read as a non-nullable value type.

OverflowException

Thrown when an integer does not fit T.