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


##########
python/tvm_ffi/registry.py:
##########
@@ -273,15 +273,25 @@ def _add_class_attrs(type_cls: type, type_info: TypeInfo) 
-> type:
         name = field.name
         if not hasattr(type_cls, name):  # skip already defined attributes
             setattr(type_cls, name, field.as_property(type_cls))
+    has_c_init = False
     for method in type_info.methods:
         name = method.name
         if name == "__ffi_init__":
             name = "__c_ffi_init__"
+            has_c_init = True
         if not hasattr(type_cls, name):
             setattr(type_cls, name, method.as_callable(type_cls))
+    if "__init__" not in type_cls.__dict__ and has_c_init:
+        setattr(type_cls, "__init__", getattr(type_cls, "__ffi_init__"))
+    elif not type_info.type_key.startswith("ffi."):
+        setattr(type_cls, "__init__", __init__invalid)

Review Comment:
   ![critical](https://www.gstatic.com/codereviewagent/critical.svg)
   
   The current logic for adding `__init__` can incorrectly override a 
user-defined `__init__` method. If a class has its own `__init__` method, the 
first `if` condition `"__init__" not in type_cls.__dict__ and has_c_init` will 
be false. This will cause the `elif` block to be evaluated, which could then 
overwrite the existing `__init__` with `__init__invalid` if the type key 
doesn't start with `"ffi."`. 
   
   The logic should be nested to ensure we only add an `__init__` method if one 
is not already present on the class.
   
   ```suggestion
       if "__init__" not in type_cls.__dict__:
           if has_c_init:
               setattr(type_cls, "__init__", getattr(type_cls, "__ffi_init__"))
           elif not type_info.type_key.startswith("ffi."):
               setattr(type_cls, "__init__", __init__invalid)
   ```



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