Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions dm_env/specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,5 +400,11 @@ def generate_value(self):
def __repr__(self):
return self._REPR_TEMPLATE.format(self=self) # pytype: disable=duplicate-keyword-argument

def __eq__(self, other):
if not isinstance(other, StringArray):
return False
return (super(StringArray, self).__eq__(other) and
self.string_type == other.string_type)

def __reduce__(self):
return type(self), (self.shape, self.string_type, self.name)
25 changes: 25 additions & 0 deletions dm_env/specs_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,5 +486,30 @@ def testSerialization(self, shape, string_type, name):
spec = specs.StringArray(shape=shape, string_type=string_type, name=name)
self.assertEqual(pickle.loads(pickle.dumps(spec)), spec)

@parameterized.parameters(
dict(shape=()), dict(shape=(2,)), dict(shape=(2, 3)))
def testEqual(self, shape):
spec_1 = specs.StringArray(shape=shape, string_type=bytes, name="foo")
spec_2 = specs.StringArray(shape=shape, string_type=bytes, name="bar")
self.assertEqual(spec_1, spec_2)

@parameterized.parameters(
dict(shape=()), dict(shape=(2,)), dict(shape=(2, 3)))
def testNotEqualDifferentStringType(self, shape):
spec_1 = specs.StringArray(shape=shape, string_type=str)
spec_2 = spec_1.replace(string_type=bytes)
self.assertNotEqual(spec_1, spec_2)
self.assertNotEqual(spec_2, spec_1)

def testNotEqualDifferentShape(self):
spec_1 = specs.StringArray(shape=(2,), string_type=str)
spec_2 = specs.StringArray(shape=(3,), string_type=str)
self.assertNotEqual(spec_1, spec_2)

def testIsUnhashable(self):
spec = specs.StringArray(shape=(2,))
with self.assertRaisesRegex(TypeError, "unhashable type"):
hash(spec)

if __name__ == "__main__":
absltest.main()
Loading