Skip to content

feat(dataclass): Introduce more utility methods in tvm_ffi.dataclasses.*#555

Merged
junrushao merged 1 commit intoapache:mainfrom
junrushao:junrushao/2026-04-17/ffi-compat
Apr 17, 2026
Merged

feat(dataclass): Introduce more utility methods in tvm_ffi.dataclasses.*#555
junrushao merged 1 commit intoapache:mainfrom
junrushao:junrushao/2026-04-17/ffi-compat

Conversation

@junrushao
Copy link
Copy Markdown
Member

@junrushao junrushao commented Apr 17, 2026

Summary

This PR introduces:

Test plan

  • uv run pytest tests/python — 2148 passed, 38 skipped, 3 xfailed
  • pre-commit run --all-files — 27 hooks passed
  • New tests for every new attribute/flag and for id_/is_ alias parity
  • CI lint + C++ unit tests + Rust tests

@junrushao junrushao changed the title feat(dataclass,core): plumb C++ field traits through reflection and add CObject id_/is_ feat(dataclass): Introduce more utility methods in tvm_ffi.dataclasses.* Apr 17, 2026
@gemini-code-assist
Copy link
Copy Markdown
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 /gemini review.

…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.
@junrushao junrushao force-pushed the junrushao/2026-04-17/ffi-compat branch from 73f007e to ce6ec72 Compare April 17, 2026 18:51
@junrushao junrushao mentioned this pull request Apr 17, 2026
16 tasks
@junrushao junrushao merged commit b8d8104 into apache:main Apr 17, 2026
9 checks passed
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.

2 participants