You are viewing a plain text version of this content. The canonical link for it is here.
Posted to discuss-archive@tvm.apache.org by "junrushao (via GitHub)" <gi...@apache.org> on 2026/04/11 07:50:40 UTC

[PR] fix(container): add element-wise __eq__ to Array and Map [tvm-ffi]

junrushao opened a new pull request, #533:
URL: https://github.com/apache/tvm-ffi/pull/533

   ## Summary
   - `Array.__eq__` and `Map.__eq__` inherited `Object.__eq__` which compared by handle identity, so two containers with identical elements compared as not-equal
   - Add element-wise `__eq__` to `Array` (supports comparison with Array, list, tuple) and `Map` (supports comparison with any Mapping)
   - Retain handle-based `__hash__` for backward compatibility
   - 12 regression tests added
   
   ## Test plan
   - [x] `pytest tests/python/test_container.py::TestBug9ArrayEquality` — all 12 new tests pass
   - [x] All existing tests pass
   - [x] Pre-commit hooks pass


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] fix(container): add element-wise __eq__ to Array and Map [tvm-ffi]

Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
gemini-code-assist[bot] commented on code in PR #533:
URL: https://github.com/apache/tvm-ffi/pull/533#discussion_r3067764247


##########
python/tvm_ffi/container.py:
##########
@@ -210,6 +210,23 @@ def __radd__(self, other: Iterable[T]) -> Array[T]:
         """Concatenate two arrays."""
         return type(self)(itertools.chain(other, self))
 
+    def __eq__(self, other: object) -> bool:
+        """Element-wise equality comparison."""
+        if isinstance(other, (Array, list, tuple)):
+            if len(self) != len(other):
+                return False
+            return all(
+                (a == b)
+                if not (isinstance(a, core.CObject) and isinstance(b, core.CObject))
+                else a.same_as(b) or a == b
+                for a, b in zip(self, other)
+            )

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   The element comparison logic is a bit complex and can be simplified for better readability. The current implementation uses a conditional expression to provide a fast path for `CObject` identity checks. While this is a valid optimization, a simpler `all(a == b for a, b in zip(self, other))` is more Pythonic and much easier to understand.
   
   The recursive calls to `__eq__` will correctly handle all types, including nested containers and other `CObject` types. The base `Object.__eq__` is already an identity check, so the main benefit of the current complex logic is short-circuiting for nested `Array` and `Map` objects that are the same instance. The readability improvement from simplification likely outweighs the performance gain in most common scenarios.
   
   ```python
               return all(a == b for a, b in zip(self, other))
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] fix(container): add element-wise __eq__ to Array and Map [tvm-ffi]

Posted by "junrushao (via GitHub)" <gi...@apache.org>.
junrushao closed pull request #533: fix(container): add element-wise __eq__ to Array and Map
URL: https://github.com/apache/tvm-ffi/pull/533


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org