Dayuxiaoshui commented on code in PR #411:
URL: https://github.com/apache/tvm-ffi/pull/411#discussion_r2698165780


##########
python/tvm_ffi/dataclasses/_utils.py:
##########
@@ -58,14 +58,13 @@ def type_info_to_cls(
     def _add_method(name: str, func: Callable[..., Any]) -> None:
         if name == "__ffi_init__":
             name = "__c_ffi_init__"
-        if name in attrs:  # already defined
-            return
+        # Allow overriding methods (including from base classes like 
Object.__repr__)
+        # by always adding to attrs, which will be used when creating the new 
class
         func.__module__ = cls.__module__
         func.__name__ = name
         func.__qualname__ = f"{cls.__qualname__}.{name}"
         func.__doc__ = f"Method `{name}` of class `{cls.__qualname__}`"
         attrs[name] = func
-        setattr(cls, name, func)

Review Comment:
    The setattr(cls, name, func) line was removed because methods are already 
added to the attrs dictionary (line 67) and the new class is created via 
type(cls.__name__, cls_bases, attrs) (line 76), which automatically includes 
all attributes from attrs. Setting methods on cls (the input template class) is 
unnecessary since cls is not the final class being created - the final class 
gets all methods from attrs when created by type().



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