Skip to content

Include string_type in StringArray equality - #27

Open
1AbdullahButt wants to merge 1 commit into
google-deepmind:masterfrom
1AbdullahButt:fix/26-stringarray-eq-string-type
Open

1AbdullahButt wants to merge 1 commit into
google-deepmind:masterfrom
1AbdullahButt:fix/26-stringarray-eq-string-type

Conversation

@1AbdullahButt

Copy link
Copy Markdown

Fixes #26.

Cause. StringArray inherited Array.__eq__, which only compares shape and dtype. Every StringArray has dtype=object, so StringArray((2,), string_type=str) and the same spec with string_type=bytes compared equal, even though each rejects the other's values in validate.

Fix. StringArray.__eq__ now also compares string_type, in the same way BoundedArray.__eq__ adds its bounds on top of the base comparison. Names are still ignored, as for the other specs. Defining __eq__ keeps the class unhashable, as before.

Tests. New tests in StringArrayTest: equal specs with different names (scalar, 1-D, 2-D), str vs bytes compare unequal both ways (scalar, 1-D, 2-D, made with replace), different shapes are unequal, and the spec is still unhashable.

What I ran (Python 3 venv with absl-py, numpy, dm-tree, pytest):

  • python -m pytest -q dm_env/specs_test.py: 109 passed.
  • With the specs.py change reverted, python -m pytest -q dm_env/specs_test.py -k StringArray: the 3 testNotEqualDifferentStringType cases fail, the rest pass.

AI disclosure: this change was written with Claude Code (an AI coding assistant) and reviewed before submitting.

🤖 Generated with Claude Code

StringArray inherited Array.__eq__, which only compares shape and dtype.
Every StringArray has dtype object, so a str spec and a bytes spec compared
equal even though they accept different values. Compare string_type too,
the same way BoundedArray extends equality with its bounds.

Fixes google-deepmind#26.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

StringArray equality ignores string_type and equates incompatible str/bytes specs

1 participant