gemini-code-assist[bot] commented on code in PR #508:
URL: https://github.com/apache/tvm-ffi/pull/508#discussion_r2972045484
##########
python/tvm_ffi/dataclasses/py_class.py:
##########
@@ -202,6 +202,30 @@ def _collect_own_fields(
return fields
+def _collect_py_methods(cls: type) -> list[tuple[str, Any, bool]] | None:
+ """Extract recognized FFI dunder methods from the class body.
+
+ Only names listed in :data:`_FFI_RECOGNIZED_METHODS` are collected.
+
+ Returns a list of ``(name, func, is_static)`` tuples, or ``None``
+ if no eligible methods were found.
+ """
+ methods: list[tuple[str, Any, bool]] = []
+ for name, value in cls.__dict__.items():
+ if name not in _FFI_RECOGNIZED_METHODS:
+ continue
+ if isinstance(value, staticmethod):
+ func = value.__func__
+ is_static = True
+ elif callable(value):
+ func = value
+ is_static = False
+ else:
+ continue
+ methods.append((name, func, is_static))
+ return methods if methods else None
Review Comment:

This is a great addition! To further improve introspection, consider
extracting the docstring from the collected methods and passing it to the FFI
layer. This would allow the docstrings of Python-defined FFI methods to be
visible in other languages.
You can use `inspect.getdoc()` for this (you'll need to `import inspect`).
This would require corresponding changes in
`python/tvm_ffi/cython/type_info.pxi`:
1. Update `TypeInfo._register_py_methods` to expect a `(name, func,
is_static, doc)` tuple.
2. Update the cdef function `_register_py_methods` to unpack the docstring
and populate `method_info.doc` before calling `TVMFFITypeRegisterMethod`.
```python
def _collect_py_methods(cls: type) -> list[tuple[str, Any, bool, str |
None]] | None:
"""Extract recognized FFI dunder methods from the class body.
Only names listed in :data:`_FFI_RECOGNIZED_METHODS` are collected.
Returns a list of ``(name, func, is_static, doc)`` tuples, or ``None``
if no eligible methods were found.
"""
methods: list[tuple[str, Any, bool, str | None]] = []
for name, value in cls.__dict__.items():
if name not in _FFI_RECOGNIZED_METHODS:
continue
if isinstance(value, staticmethod):
func = value.__func__
is_static = True
elif callable(value):
func = value
is_static = False
else:
continue
doc = inspect.getdoc(func)
methods.append((name, func, is_static, doc))
return methods if methods else None
```
--
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]