feat(dataclass): Introduce more utility methods in tvm_ffi.dataclasses.*#555
Merged
junrushao merged 1 commit intoapache:mainfrom Apr 17, 2026
Merged
Conversation
tvm_ffi.dataclasses.*
Contributor
|
Warning Gemini is experiencing higher than usual traffic and was unable to create the review. Please try again in a few hours by commenting |
…dd CObject id_/is_ aliases Architecture: - Reflection bridge now completes the round-trip between the C++ TVMFFIFieldInfo struct and the Python @c_class dataclass facade. TypeField (Cython-side dataclass) grew six new attributes (c_default, c_default_factory, c_repr, c_compare, c_hash, c_structural_eq) mirroring the flag bits and default_value_or_factory union; _attach_field_objects synthesizes a user-facing Field per reflected field and stashes it on TypeField.dataclass_field so dataclasses.fields() exposes everything a @py_class-authored field would carry. - No boundary changes on the CObject side: id_ / is_ are thin aliases on the Cython base class; all subclasses (Object, Tensor, Function, etc.) pick them up via inheritance. Public Interfaces: - tvm_ffi.dataclasses.Field objects returned by fields() on @c_class-registered types now report default, default_factory, repr, compare, hash, structural_eq sourced from the C++ reflection registry (previously always MISSING / defaults). - tvm_ffi.core.TypeField (formerly internal) stub in core.pyi grew six new attributes matching the above. - New CObject.id_ property (int) and CObject.is_(other) -> bool. No removals; no signature changes. - No ABI change; no C++ header change. UI/UX: none Behavioral Changes: - @c_class fields now surface their C++-declared defaults, repr/compare/hash opt-outs, and SEqHash ignore/def marks through the public fields() API. Callers that previously relied on these attributes being MISSING / all-True would see real values. - id_ returns the raw handle address (identical to __chandle__()). is_ tests handle identity (identical to same_as()). No change to __chandle__ / same_as semantics. Docs: Inline docstrings updated on CObject.id_ and CObject.is_. No separate docs files required. core.pyi stubs mirror the new surface. Tests: - Executed: uv run pytest tests/python -> 2148 passed, 38 skipped, 3 xfailed. - Executed: pre-commit run --all-files -> all 27 hooks passed. - New coverage: test_c_class_field_defaults_missing_when_unspecified, test_c_class_field_default_value_from_cxx, test_c_class_field_default_respects_init_false, test_c_class_field_default_respects_kw_only, test_c_class_field_repr_flag_from_cxx, test_c_class_field_compare_flag_from_cxx, test_c_class_field_hash_flag_from_cxx, test_c_class_field_structural_eq_default_none (all in tests/python/test_dataclass_c_class.py), test_c_class_default_value_from_cxx (in tests/python/test_dataclass_common.py), and test_id_and_is (in tests/python/test_object.py). Untested Edge Cases: - structural_eq == "def" / "ignore" states: no C++ fixture currently sets SEqHashDef or SEqHashIgnore on a visible field; only the default pass-through (None) is covered. Adding a fixture is a follow-up. - Pickling round-trip of synthesized Field objects (they originate from reflection, not user source). - is_ / same_as against PyNativeObject proxies (String, Bytes): existing limitation of same_as preserved unchanged.
73f007e to
ce6ec72
Compare
16 tasks
yaoyaoding
approved these changes
Apr 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR introduces:
tvm_ffi.dataclasses.fieldssimilar to dataclasses.fieldstvm_ffi.dataclasses.replacesimilar to dataclasses.replacetvm_ffi.dataclasses.is_dataclasssimilar to dataclasses.is_dataclasstvm_ffi.Object.id_similar to idtvm_ffi.Object.is_similar to Python'siskeywordTest plan
uv run pytest tests/python— 2148 passed, 38 skipped, 3 xfailedpre-commit run --all-files— 27 hooks passedid_/is_alias parity