Conversation
Remove the Python-side field descriptor system (_utils.py, field.py, complex c_class.py) that is being replaced by C++ reflection-driven metadata. The c_class decorator now delegates directly to register_object. - Delete python/tvm_ffi/dataclasses/_utils.py - Delete python/tvm_ffi/dataclasses/field.py - Simplify c_class.py to a pass-through to register_object - Simplify dataclasses/__init__.py to export only c_class - Delete tests/python/test_dataclasses_c_class.py - Update testing.py: add Object base, remove field() usage - Fix _add_class_attrs to always override __c_ffi_init__ (prevents inherited base-class init from masking derived-class init) - Update test_repr.py: use positional args for derived constructors Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Split Cython Object into CObject (extension type) + Object (Python class with metaclass that auto-adds __slots__=()). This prevents per-instance __dict__ on all Object subclasses by default. - object.pxi: CObject base, _ObjectSlotsMeta, Object(CObject), object_repr(), OpaquePyObject(CObject), simplified fallback cls - function.pxi: all Object → CObject casts - error.pxi: Error(CObject) + __slots__ - tensor.pxi: Tensor(CObject) + __slots__ - device.pxi: __slots__, kDLMAIA=17, kDLTrn=18 - dtype.pxi: __slots__ - base.pxi: __slots__ on ByteArrayArg - string.pxi: Object → CObject - type_info.pxi: Object → CObject in FieldGetter/FieldSetter - core.pyi: export CObject, Object(CObject), object_repr - container.py: remove redundant __repr__ (now on CObject) - access_path.py: remove super().__init__() call - module.py: __slots__ + _tvm_ffi_attr_cache pattern - registry.py: add ffi.Shape to deepcopy-supported containers, use _ffi_api.DeepCopy directly, remove _get_deep_copy_func - test_object.py: slots enforcement + direct-init guard tests Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…w C++ APIs Merge deep_copy.cc and repr_print.cc into a unified dataclass.cc using shared ObjectGraphDFS infrastructure. Add new C++ reflection capabilities: - RecursiveEq/Hash/Compare for structural comparison and hashing - MakeInit/RegisterAutoInit for auto-generated __ffi_init__ constructors - GetKwargsObject for kwargs-based initialization - New reflection traits: compare, hash, kw_only, init - New ObjectDef destructor auto-init registration - Rename test_repr.py → test_dataclass_repr.py Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Expose RecursiveEq/Lt/Le/Gt/Ge to Python tests. Add TestCompare, TestCustomCompare, and TestEqWithoutHash test classes and comprehensive test_dataclass_compare.py covering primitives, containers, reflected objects, CompareOff flags, custom hooks, cycles, and ordering laws. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Expose RecursiveHash to Python tests. Add TestHash and TestCustomHash test classes and comprehensive test_dataclass_hash.py covering primitives, containers, reflected objects, HashOff flags, custom hooks, cycle detection, consistency laws, and aliasing invariants. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Wire up C++ auto-generated __ffi_init__ to Python __init__ via reflection metadata. TypeField now carries c_init, c_kw_only, c_has_default flags from the C++ bitmask. _make_init builds a kwargs-aware __init__ with proper inspect.Signature. _install_init dispatches between auto-init, plain __ffi_init__, and a guard TypeError for types without constructors. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Rewrite the `@c_class` decorator to combine `register_object` with `_install_dataclass_dunders`, which installs structural `__init__`, `__repr__`, `__eq__`/`__ne__`, `__hash__`, and ordering operators from C++ reflection metadata. Migrate all test objects from `@register_object` to `@c_class`. Add test_dataclass_c_class.py and rename test_copy.py to test_dataclass_copy.py with additional cycle/Shape coverage. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Allow creating `tvm_ffi.Function` directly from a Python callable or from an existing Function instance, with proper ref-counting and error handling for non-callable and moved-from objects. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Update tvm-ffi-stubgen to emit `__init__` stubs from C++ reflection metadata. For auto-init types, walks the parent chain to collect init-eligible fields with proper kw_only/has_default handling. For non-auto-init types with `__c_ffi_init__`, generates positional-only `__init__` from the factory signature. Regenerate inline stubs and remove now-unnecessary ty:ignore comments from test files. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Add tvm-ffi-stubgen(begin/end) marker blocks and TYPE_CHECKING stubs (__ffi_shallow_copy__, __c_ffi_init__) for ~13 test classes in testing.py - Reorder classes (SchemaAllTypes, create_object, etc.) to match final state - Use keyword args in test_dataclass_compare.py and test_dataclass_hash.py for _TestCxxClassDerived/_TestCxxClassDerivedDerived constructors Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Add docs/guides/dataclass_reflection.rst (new guide) - Update docs/conf.py with intersphinx and autodoc settings - Update docs/index.rst to include new guide - Minor updates to object_and_class.rst and export_func_cls.rst Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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 new reflection-driven dataclass system for TVM FFI objects. It centralizes deep copy, string representation, structural hashing, and comparison logic within a dedicated C++ module. The core enhancement lies in extending the FFI reflection mechanism with new field-level flags that dictate how fields behave in these structural operations and how constructors are automatically generated. On the Python side, the 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. Changelog
Activity
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. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a major refactoring and enhancement of the FFI reflection system, focusing on providing a more Python-native, dataclass-like experience for FFI objects. Key changes include:
- Auto-Generated
__init__: A powerful mechanism to automatically generate__init__methods for C++ objects from their reflection metadata. This supports default values, keyword-only arguments, and inheritance, significantly simplifying the Python-side boilerplate. - Consolidated Dataclass Operations: The logic for deep copy, repr printing, recursive hashing, and recursive comparison has been consolidated into a new
dataclass.ccfile. The implementations now use an iterative DFS approach, making them robust against deep object graphs and stack overflows. - Structural Comparison and Hashing: New FFI functions (
RecursiveEq,RecursiveHash, etc.) provide comprehensive structural comparison and hashing capabilities, with support for custom hooks and fine-grained field exclusion. - Improved Python API: The
@c_classdecorator is now a clean, declarative way to register FFI objects and opt-in to these new dataclass features. The old, complex Python-side dataclass emulation has been removed in favor of the more robust C++ implementation. - Architectural Improvements: The
Objectbase class has been refactored to use a metaclass that enforces__slots__, improving memory efficiency and preventing accidental attribute setting. TheModule.__getattr__has also been optimized with caching.
Overall, this is an exceptionally well-designed and implemented set of changes that greatly improves the usability and power of the FFI layer. The code is clean, the new APIs are intuitive, and the features are backed by extensive tests. I have no concerns with the proposed changes.
Commits