gemini-code-assist[bot] commented on code in PR #49:
URL: https://github.com/apache/tvm-ffi/pull/49#discussion_r2377298556


##########
python/tvm_ffi/cython/type_info.pxi:
##########
@@ -101,6 +102,21 @@ class TypeMethod:
     func: object
     is_static: bool
 
+    def as_callable(self, object cls):
+        """Create a Python method attribute for this method on ``cls``."""
+        cdef str name = self.name
+        cdef str doc = self.doc or f"Method `{name}` of class 
`{cls.__qualname__}`"
+        cdef object func = self.func
+        if self.is_static:
+            func = staticmethod(func)
+        else:
+            func = _member_method_wrapper(func)
+        func.__module__ = cls.__module__
+        func.__name__ = name
+        func.__qualname__ = f"{cls.__qualname__}.{name}"
+        func.__doc__ = doc
+        return func

Review Comment:
   Thanks for checking, junrushao. I've double-checked the behavior, and the 
`AttributeError` indeed occurs when attempting to set attributes like 
`__doc__`, `__module__`, `__name__`, or `__qualname__` directly on a 
`staticmethod` object itself. 
   
   This is because `staticmethod` is a descriptor. When you create `func = 
staticmethod(func_obj)`, `func` becomes a `staticmethod` object. This object 
does not expose these special attributes for direct assignment. Instead, these 
attributes should be set on the *underlying function object* (`func_obj`) 
*before* it is passed to `staticmethod`.
   
   Your local test might not have reproduced the issue if you were accessing 
these attributes through the class after the `staticmethod` was bound (e.g., 
`MyClass.my_static_method.__doc__`). In such cases, Python's descriptor 
protocol would typically retrieve the `__doc__` from the underlying function, 
making it seem like the assignment worked. However, the direct assignment 
`func.__doc__ = doc` where `func` *is* the `staticmethod` object will raise an 
`AttributeError`.
   
   The suggested change ensures that these attributes are set on the actual 
function object (or the `_member_method_wrapper` function for non-static 
methods) *before* `staticmethod` is applied, thus avoiding the `AttributeError` 
and correctly propagating the metadata.
   
   Here's the corrected logic again for reference:
   ```python
           cdef object func
           if self.is_static:
               func = self.func
           else:
               func = _member_method_wrapper(self.func)
   
           func.__module__ = cls.__module__
           func.__name__ = name
           func.__qualname__ = f"{cls.__qualname__}.{name}"
           func.__doc__ = doc
   
           if self.is_static:
               return staticmethod(func)
           return func
   ```
   
   ```suggestion
           cdef object func
           if self.is_static:
               func = self.func
           else:
               func = _member_method_wrapper(self.func)
   
           func.__module__ = cls.__module__
           func.__name__ = name
           func.__qualname__ = f"{cls.__qualname__}.{name}"
           func.__doc__ = doc
   
           if self.is_static:
               return staticmethod(func)
           return func
   ```



-- 
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]

Reply via email to