feat(python): add structural equality/hashing support to py_class and field#507
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a robust structural equality and hashing mechanism for Python classes defined using Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces structural equality and hashing for py_class objects, controlled by a new structure parameter on the @py_class decorator and field helper. The implementation is robust, spanning Python and Cython layers, and is accompanied by excellent, comprehensive documentation that clearly explains the new concepts. The PR also includes a substantial expansion of the test suite, adding hundreds of new tests that cover a wide range of py_class functionality, significantly improving the project's test coverage and maintainability. A minor CI fix is also included. The changes are of high quality, and I have no further feedback.
…nd `field` (#53) Add a `structure=` parameter to `@py_class()` and `field()` that controls structural equality and hashing behavior for Python-defined FFI dataclasses. Class-level `structure=` accepts: "var" (default, identity semantics), "tree", "const-tree", "dag". Field-level `structure=` accepts: "ignore" and "def". These map to the C++ `TVMFFISEqHashKind` enum and `SEqHashIgnore`/`SEqHashDef` field flags, dispatched through Cython-accelerated `_ffi_seq_hash_kind` and `_ffi_field_info_seq_hash` attributes on object types. Additional changes in this squash: - Expand `py_class` test coverage with 349 tests ported from mlc-python covering field parsing, defaults, inheritance, JSON serialization, nested structures, and edge cases. - Add `docs/concepts/structural_eq_hash.rst` documenting the structural equality/hashing design. - Pin `astral-sh/setup-uv` to v7.3.1 (`5a095e7a`) and `pypa/cibuildwheel` to v3.3.1 (`298ed2fb`), both Apache-approved SHAs, fixing CI permission failures.
f57e9c8 to
20b9b7d
Compare
py_class and field
…peMethod (#508) ## Summary - Extend `@py_class` to detect and register recognized FFI dunder methods (`__ffi_repr__`, `__ffi_eq__`, `__s_equal__`, etc.) defined on Python dataclasses, making them callable from C++ and other FFI languages - `_FFI_RECOGNIZED_METHODS` allowlist in `py_class.py` gates which dunders are collected; `_collect_py_methods()` scans class dict and `TypeInfo._register_py_methods()` registers via the C API (`TVMFFITypeRegisterMethod` + `TVMFFITypeRegisterAttr`) - No C++ changes — `dataclass.cc` is unmodified from upstream ## Changes since v1 - Rebased onto upstream/main (`d8bd1890`) which adds structural equality/hashing support (#507) - Resolved merge conflicts: preserved `structure_kind` / `STRUCTURE_KIND_MAP` from upstream - Applied Gemini Code Assist review feedback: - **HIGH**: Fixed `func_any` memory leak in `_register_py_methods` by adding `TVMFFIObjectDecRef` in a `try/finally` block - **MEDIUM**: Changed `_collect_py_methods` to iterate `cls.__dict__.items()` instead of `list(cls.__dict__)` + second lookup ## Test plan - [x] 12 new dedicated tests covering allowlist filtering, registration, round-trip dispatch (repr, eq, compare, hash) - [x] All 693 tests pass (including structural equality tests that now work with the `structure_kind` base from #507) - [ ] CI: lint, C++ tests, Python tests, Rust tests 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Summary
structure=parameter to@py_class()andfield()controlling structural equality/hashing behavior. Class-level values:"var","tree","const-tree","dag". Field-level values:"ignore","def". Dispatched via Cython-accelerated_ffi_seq_hash_kindand_ffi_field_info_seq_hashattributes, mapping to C++TVMFFISEqHashKindandSEqHashIgnore/SEqHashDeffield flags.py_classtest coverage with 349 tests ported from mlc-python (field parsing, defaults, inheritance, JSON serialization, nested structures, edge cases).docs/concepts/structural_eq_hash.rstdocumenting the structural equality/hashing design.astral-sh/setup-uvto v7.3.1 andpypa/cibuildwheelto v3.3.1 (Apache-approved SHAs), fixing CI permission failures in https://github.com/apache/tvm-ffi/actions/runs/23389552989.Test plan
uv run pytest -vvs tests/python/test_dataclass_py_class.py tests/python/test_structural_py_class.py— 349 passed