mirror of
https://github.com/Farama-Foundation/Gymnasium.git
synced 2025-08-30 09:55:39 +00:00
gym.spaces.Tuple inherits from collections.abc.Sequence (#2637)
* gym.spaces.Tuple inherits from collections.abc.Sequence Following the PR I did a few months back (https://github.com/openai/gym/pull/2446), the tuple wrapper of gym should inherits from the abstract interface of Python. It is important for type check via `isinstance` and enable using such objects transparently with libraries such as [dmtree](https://github.com/deepmind/tree). It will bring a way helper methods along the way but it cannot be avoided to interoperability: : `__iter__`, `__reversed__`, `index`, and `count` Personally I don't think it is an issue since it is new features and it is not conflicting. As the previous PR, this patch is NOT removing any existing feature and should not break backward compatibility. * Add unit test * Fix unit tests * Final fix. * Remove irrelevant comment. * Fix black formatter.
This commit is contained in:
@@ -86,7 +86,7 @@ def test_roundtripping(space):
|
||||
)
|
||||
def test_equality(space):
|
||||
space1 = space
|
||||
space2 = copy.copy(space)
|
||||
space2 = copy.deepcopy(space)
|
||||
assert space1 == space2, f"Expected {space1} to equal {space2}"
|
||||
|
||||
|
||||
@@ -379,6 +379,23 @@ def test_seed_subspace_incorrelated(space):
|
||||
assert len(states) == len(set(states))
|
||||
|
||||
|
||||
def test_tuple():
|
||||
spaces = [Discrete(5), Discrete(10), Discrete(5)]
|
||||
space_tuple = Tuple(spaces)
|
||||
|
||||
assert len(space_tuple) == len(spaces)
|
||||
assert space_tuple.count(Discrete(5)) == 2
|
||||
assert space_tuple.count(MultiBinary(2)) == 0
|
||||
for i, space in enumerate(space_tuple):
|
||||
assert space == spaces[i]
|
||||
for i, space in enumerate(reversed(space_tuple)):
|
||||
assert space == spaces[len(spaces) - 1 - i]
|
||||
assert space_tuple.index(Discrete(5)) == 0
|
||||
assert space_tuple.index(Discrete(5), 1) == 2
|
||||
with pytest.raises(ValueError):
|
||||
space_tuple.index(Discrete(10), 0, 1)
|
||||
|
||||
|
||||
def test_multidiscrete_as_tuple():
|
||||
# 1D multi-discrete
|
||||
space = MultiDiscrete([3, 4, 5])
|
||||
|
Reference in New Issue
Block a user