gemini-code-assist[bot] commented on PR #498: URL: https://github.com/apache/tvm-ffi/pull/498#issuecomment-4014930325
## Summary of Changes Hello, I'm Gemini Code Assist[^1]! 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 addresses a fundamental flaw in the Python FFI layer's type checking, where `isinstance` and `issubclass` operations for C++-backed objects were unreliable. By removing the problematic metaclass overrides, the system now correctly reflects the underlying C++ object hierarchy. Furthermore, it introduces a sophisticated, performance-optimized type reflection and conversion framework, enhancing the accuracy and flexibility of type-related operations within the TVM FFI. These changes significantly improve the robustness and predictability of type interactions between Python and C++ components. ### Highlights * **Corrected Type Checking Logic**: Removed the erroneous `__instancecheck__` and `__subclasscheck__` methods from `_ObjectSlotsMeta` in `python/tvm_ffi/cython/object.pxi`. This resolves a critical bug where `isinstance` and `issubclass` checks for `CObject` subclasses incorrectly returned `True` for any FFI object, regardless of its actual type, ensuring proper type hierarchy validation. * **Enhanced Type Reflection and Conversion**: Introduced a new `CAny` class for owned FFI values and significantly enhanced the `TypeSchema` with robust type checking and conversion capabilities. This includes new methods like `from_annotation`, `check_value`, and `convert`, powered by a new Cython module (`type_check.pxi`) for efficient, C-level dispatch. * **Refactored FFI Reflection Mechanisms**: Updated the C++ FFI reflection system to support function objects as field setters (`kTVMFFIFieldFlagBitSetterIsFunctionObj`) and unified object creation through `CreateEmptyObject`. This streamlines object instantiation and field manipulation, allowing for more flexible and powerful reflection. * **Comprehensive Regression Testing**: Added 22 new regression tests in `TestIsinstanceIssubclass` to thoroughly verify the corrected `isinstance`/`issubclass` behavior across various FFI object types, including containers, inheritance hierarchies, and cross-hierarchy checks, ensuring the fix is robust. 🧠**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](https://codeassist.google/code-review/login) to enable Memory in your admin console.** <details> <summary><b>Changelog</b></summary> * **include/tvm/ffi/c_api.h** * Added `kTVMFFIFieldFlagBitSetterIsFunctionObj` flag to `TVMFFIFieldFlagBitMask`. * Updated `TVMFFIFieldInfo::setter` type to `void*` to support function object setters. * **include/tvm/ffi/function.h** * Added `CreateEmptyObject` function to create objects using native C++ creators or Python's `__ffi_new__`. * Added `HasCreator` function to check if a type supports reflection creation. * **include/tvm/ffi/reflection/accessor.h** * Added `CallFieldSetter` to dispatch field setting based on `kTVMFFIFieldFlagBitSetterIsFunctionObj`. * Updated `FieldSetter::operator()` to use `CallFieldSetter` for field assignment. * Modified `SetFieldToDefault` to utilize `CallFieldSetter` for default value application. * **include/tvm/ffi/reflection/creator.h** * Updated `ObjectCreator` constructor to use `HasCreator` for type validation. * Modified `ObjectCreator::operator()` to use `CreateEmptyObject` and `CallFieldSetter`. * **include/tvm/ffi/reflection/init.h** * Updated `MakeInit` to use `HasCreator` for creator availability checks. * Modified `MakeInit` to use `CreateEmptyObject` for object instantiation. * Changed `MakeInit` to use `CallFieldSetter` for setting object fields. * **include/tvm/ffi/reflection/overload.h** * Cast `ReflectionDefBase::FieldSetter<T>` to `void*` for `info.setter` assignment. * **include/tvm/ffi/reflection/registry.h** * Cast `FieldSetter<T>` to `void*` for `info.setter` assignment. * **python/tvm_ffi/__init__.py** * Imported `CAny` from `core` module. * **python/tvm_ffi/core.pyi** * Added `CAny` class definition with `__init__`, `type_index`, and `to_py` methods. * Added `origin_type_index` attribute to `TypeSchema`. * Added `from_type_index`, `from_annotation`, `check_value`, `convert`, and `to_json` static methods/methods to `TypeSchema`. * **python/tvm_ffi/cython/base.pxi** * Added `cause_chain` and `extra_context` fields to `TVMFFIErrorMetadata`. * Introduced new `kTVMFFIFieldFlagBitMask` values for structural equality/hashing, representation, and comparison. * Added `kTVMFFIFieldFlagBitSetterIsFunctionObj` flag. * Updated `TVMFFIFieldInfo::setter` to `void*`. * Added `TVMFFISEqHashKind` enum. * Changed `TVMFFITypeMetadata::total_size` to `int32_t` and added `structural_eq_hash_kind`. * Added new C API functions for type registration and error handling. * Modified `TVMFFIPyCallFieldSetter` signature to include `field_flags`. * **python/tvm_ffi/cython/core.pyx** * Included `type_check.pxi` for type conversion logic. * **python/tvm_ffi/cython/object.pxi** * Removed `__instancecheck__` and `__subclasscheck__` methods from `_ObjectSlotsMeta`. * Added `flags` attribute to `FieldSetter` cdef class. * Added `CAny` cdef class for owned `TVMFFIAny` values, including `__cinit__`, `__init__`, `__dealloc__`, `type_index`, `to_py`, and `__repr__`. * **python/tvm_ffi/cython/tvm_ffi_python_helpers.h** * Updated `TVMFFIPyCallManager::SetField` to handle `field_flags` and dispatch setter calls accordingly. * Modified `TVMFFIPyCallFieldSetter` to accept `field_flags` for setter dispatch. * **python/tvm_ffi/cython/type_check.pxi** * Added new file `type_check.pxi` implementing a Cython-based type conversion and checking system for `TypeSchema`. * Defined `_ConvertError` for signaling conversion failures. * Implemented `_TypeConverter` cdef class for efficient dispatch of type conversions. * Provided various `_tc_convert_*` and `_dispatch_*` functions for handling different primitive and composite types. * Included logic for `__tvm_ffi_value__` protocol fallback and recursion depth protection. * Exposed `_type_schema_check_value` and `_type_schema_convert` for Python-level `TypeSchema` methods. * **python/tvm_ffi/cython/type_info.pxi** * Imported `typing`, `collections.abc`, `cached_property`, and `UnionType`. * Updated `FieldSetter` to include `flags` and use the new `TVMFFIPyCallFieldSetter` signature. * Expanded `_TYPE_SCHEMA_ORIGIN_CONVERTER` and added `_ORIGIN_TO_TYPE_INDEX` and `_TYPE_INDEX_TO_ORIGIN` maps. * Added `origin_type_index` to `TypeSchema` and updated `__post_init__` logic. * Implemented `_converter` as a `cached_property` for lazy converter building. * Added `from_type_index`, `from_annotation`, `check_value`, `convert`, and `to_json` methods to `TypeSchema`. * Introduced helper functions `_annotation_union` and `_annotation_cobject` for `from_annotation`. * **python/tvm_ffi/dataclasses/__init__.py** * Imported and exposed the new `Field` class. * **python/tvm_ffi/dataclasses/field.py** * Added new file `field.py` defining the `Field` class for describing fields in Python-defined TVM-FFI types, including various configuration options. * **rust/tvm-ffi-sys/src/c_api.rs** * Updated `TVMFFIFieldInfo::setter` to `*mut c_void` to align with C API changes. * **src/ffi/extra/reflection_extra.cc** * Modified `MakeObjectFromPackedArgs` to use `CreateEmptyObject` for object creation. * Updated `MakeObjectFromPackedArgs` to use `reflection::CallFieldSetter` for field assignment. * **src/ffi/extra/serialization.cc** * Updated `ObjectGraphDeserializer` to use `CreateEmptyObject` for object instantiation. * Modified `ObjectGraphDeserializer` to use `reflection::CallFieldSetter` for field assignment during deserialization. * **src/ffi/object.cc** * Initialized `structural_eq_hash_kind` for `Object` in `TypeTable`. * **tests/python/test_dataclass_init.py** * Updated `test_parent_isinstance_child_due_to_metaclass` to `test_parent_not_isinstance_child` and changed assertion to `assert not isinstance`. * **tests/python/test_object.py** * Added `TestIsinstanceIssubclass` class with 22 new regression tests for `isinstance`/`issubclass` behavior across various FFI object types. </details> <details> <summary><b>Activity</b></summary> * All 1517 existing Python tests passed. * 22 new tests were added to verify correct `isinstance`/`issubclass` behavior for containers, inheritance hierarchies, and cross-hierarchy checks. * Pre-commit hooks (ruff, cython-lint, ty, etc.) passed. </details> <details> <summary><b>Using Gemini Code Assist</b></summary> <br> The full guide for Gemini Code Assist can be found on our [documentation page](https://developers.google.com/gemini-code-assist/docs/review-github-code), here are some quick tips. <b>Invoking Gemini</b> You can request assistance from Gemini at any point by creating a comment using either `/gemini <command>` or `@gemini-code-assist <command>`. Below is a summary of the supported commands on the current page. Feature | Command | Description --- | --- | --- Code Review | `/gemini review` | Performs a code review for the current pull request in its current state. Pull Request Summary | `/gemini summary` | Provides a summary of the current pull request in its current state. Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in pull request comments and review comments. Help | `/gemini help` | Displays a list of available commands. <b>Customization</b> 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 `.gemini/` folder in the base of the repository. Detailed instructions can be found [here](https://developers.google.com/gemini-code-assist/docs/customize-gemini-behavior-github). <b>Limitations & Feedback</b> Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with :thumbsup: and :thumbsdown: 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](https://google.qualtrics.com/jfe/form/SV_2cyuGuTWsEw84yG). <b>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](https://cloud.google.com/products/gemini/code-assist).</b> </details> [^1]: Review the [Privacy Notices](https://policies.google.com/privacy), [Generative AI Prohibited Use Policy](https://policies.google.com/terms/generative-ai/use-policy), [Terms of Service](https://policies.google.com/terms), and learn how to configure Gemini Code Assist in GitHub [here](https://developers.google.com/gemini-code-assist/docs/customize-gemini-behavior-github). Gemini can make mistakes, so double check it and [use code with caution](https://support.google.com/legal/answer/13505487). -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
