gemini-code-assist[bot] commented on PR #495: URL: https://github.com/apache/tvm-ffi/pull/495#issuecomment-3989338875
## 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 significantly refactors the Python FFI type system to enhance type safety, consistency, and performance. It introduces a new owned-value wrapper, `CAny`, which standardizes how FFI values are handled in Python, and updates the `TypeSchema` to support direct parsing of Python type annotations. These changes streamline the interaction between Python and the TVM FFI, ensuring more predictable type conversions and robust error handling, while also introducing breaking changes to the `TypeSchema.convert` API. ### Highlights * **Introduction of CAny**: A new Python-visible Cython class, `CAny`, has been introduced to wrap owned `TVMFFIAny` structs, ensuring proper reference counting for FFI values. * **TypeSchema.convert() returns CAny**: The `TypeSchema.convert()` method now returns a `CAny` instance instead of plain Python objects, requiring users to call `.to_py()` to retrieve the Python value. * **TypeSchema.from_annotation() added**: A new static method, `TypeSchema.from_annotation()`, has been added to parse Python type annotations (e.g., `list[int]`, `Optional[str]`) into `TypeSchema` objects. * **Removal of redundant methods**: The `try_check_value` and `try_convert` methods have been removed, consolidating error handling into `check_value` and `convert` which now raise `TypeError`. * **Guaranteed FFI types on conversion**: The `convert().to_py()` method now guarantees that string, bytes, and container types are converted to their respective FFI wrapper types (`tvm_ffi.String`, `tvm_ffi.Bytes`, `ffi.Array/List/Map/Dict`). 🧠**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> * **python/tvm_ffi/__init__.py** * Imported `CAny` from `tvm_ffi.core`. * **python/tvm_ffi/core.pyi** * Added `CAny` class definition with `__init__`, `type_index` property, and `to_py` method. * Updated `TypeSchema` with `origin_type_index` field, new `__init__` parameters, and new static methods `from_type_index` and `from_annotation`. * Added `check_value` and `convert` methods to `TypeSchema`. * **python/tvm_ffi/cython/core.pyx** * Included `type_check.pxi` for type conversion logic. * **python/tvm_ffi/cython/object.pxi** * Implemented the `CAny` cdef class, including `__cinit__`, `__init__` for packing Python values, `__dealloc__` for reference counting, `type_index` property, `to_py` for Python object conversion, and `__repr__`. * **python/tvm_ffi/cython/type_check.pxi** * Added a new file containing the core type conversion and checking logic for `TypeSchema`. * Defined `_ConvertError` for signaling conversion failures. * Introduced `_TypeConverter` cdef class for efficient dispatch of type conversions via C function pointers. * Implemented various `_tc_convert_*` functions for leaf types (int, float, bool, str, bytes, None, Device, dtype, opaque_ptr, tensor, callable). * Included `_tc_convert_object` for handling FFI object types. * Provided `_dispatch_convert_elems` and `_dispatch_convert_mapping` for container element conversion. * Defined `_dispatch_optional`, `_dispatch_array`, `_dispatch_list`, `_dispatch_map`, `_dispatch_dict`, `_dispatch_union`, and `_dispatch_tuple` for composite types. * Added `_type_convert_dispatch` and `_type_convert_dispatch_with_fallback` for the main dispatch mechanism, including `__tvm_ffi_value__` protocol handling and cycle protection. * Exposed `_type_schema_check_value` and `_type_schema_convert` as public API helpers for `TypeSchema`. * **python/tvm_ffi/cython/type_info.pxi** * Added imports for `typing`, `collections.abc`, `functools.cached_property`, and `types.UnionType`. * Extended `_TYPE_SCHEMA_ORIGIN_CONVERTER` to include C++ STL types (`std::vector`, `std::optional`, `std::variant`, `std::tuple`, `std::map`, `std::unordered_map`, `std::function`) and `ObjectRValueRef`. * Introduced new constants `_ORIGIN_TYPE_INDEX_STRUCTURAL`, `_ORIGIN_TYPE_INDEX_UNKNOWN`, `_ORIGIN_TO_TYPE_INDEX`, and `_TYPE_INDEX_TO_ORIGIN` for managing type indices. * Modified `TypeSchema.__post_init__` to validate arguments for structural types and compute `origin_type_index`. * Added `_converter` as a `cached_property` to `TypeSchema` for lazy initialization of the type converter. * Updated `TypeSchema.from_json_obj` to handle non-dict elements in `args` and improve error messages. * Added `TypeSchema.from_type_index` static method to create a schema from a TVM FFI type index. * Implemented `TypeSchema.from_annotation` static method to convert Python type annotations into `TypeSchema`. * Added `check_value` and `convert` methods to `TypeSchema`, delegating to the new type checking and conversion logic. * Introduced `_annotation_union` helper function for processing `Union` type annotations. </details> <details> <summary><b>Activity</b></summary> * Introduced `CAny` class and refactored `TypeSchema.convert` to return `CAny`. * Added `TypeSchema.from_annotation` for parsing Python type annotations. * Removed `try_check_value` and `try_convert` methods, standardizing error handling. * Ensured `convert().to_py()` guarantees FFI types for various Python objects. * Extensively tested the changes with `uv run pytest tests/python -x`, resulting in 1465 passed, 25 skipped, and 1 xfailed tests. * Verified code quality with `uv run pre-commit run --all-files`, with all 27 hooks passing. * Added new test classes `TestCAny` (20 tests) and `TestConvertToFFITypes` (17 tests) to cover new functionality. * Included new `TestFromAnnotation*` classes for structural equality of various type annotations. * Rewrote approximately 500 existing tests to adapt to the new API, specifically handling `try_check_value` and `convert` changes. </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]
