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


##########
python/tvm_ffi/stub/python_generator/utils.py:
##########
@@ -155,16 +155,33 @@ def render_object_methods(
         input_ty_map = ty_map
     indent_str = " " * indent
     ret = []
-    for method in info.methods:
-        func_name = method.schema.name.rsplit(".", 1)[-1]
-        if func_name == "__ffi_init__":
-            # __ffi_init__ is installed as an instance method (self, *args, 
**kwargs) -> None
-            # by _install_ffi_init_attr, regardless of the C++ static 
registration.
-            ret.append(_render_ffi_init_from_method(method, ty_map, indent, 
input_ty_map))
-            continue
-        if not method.is_member:
-            ret.append(f"{indent_str}@staticmethod")
-        ret.append(render_func_signature(method, ty_map, indent, input_ty_map))
+    index = 0
+    while index < len(info.methods):
+        method = info.methods[index]
+        group = [method]
+        index += 1
+        while index < len(info.methods):
+            candidate = info.methods[index]
+            if (
+                candidate.schema.name != method.schema.name
+                or candidate.is_member != method.is_member
+            ):
+                break
+            group.append(candidate)
+            index += 1
+
+        for candidate in group:
+            func_name = candidate.schema.name.rsplit(".", 1)[-1]
+            if len(group) > 1:
+                ret.append(f"{indent_str}@overload")
+            if func_name == "__ffi_init__":
+                # __ffi_init__ is installed as an instance method (self, 
*args, **kwargs) -> None
+                # by _install_ffi_init_attr, regardless of the C++ static 
registration.
+                ret.append(_render_ffi_init_from_method(candidate, ty_map, 
indent, input_ty_map))
+                continue
+            if not candidate.is_member:
+                ret.append(f"{indent_str}@staticmethod")
+            ret.append(render_func_signature(candidate, ty_map, indent, 
input_ty_map))

Review Comment:
   ![high](https://www.gstatic.com/codereviewagent/high-priority.svg)
   
   The current grouping logic assumes that overloaded methods are always 
contiguous in `info.methods`. However, if overloaded methods are registered 
non-contiguously (for example, if another method is registered in between 
them), they will not be grouped together. This will result in missing 
`@overload` decorators and duplicate method definitions in the generated stubs, 
which is invalid Python and will cause type checker errors.
   
   We can group the methods robustly by their name and member status while 
preserving the order of their first occurrence.
   
   ```python
       groups = []
       seen = {}
       for method in info.methods:
           key = (method.schema.name, method.is_member)
           if key not in seen:
               group = []
               groups.append(group)
               seen[key] = group
           seen[key].append(method)
   
       for group in groups:
           for candidate in group:
               func_name = candidate.schema.name.rsplit(".", 1)[-1]
               if len(group) > 1:
                   ret.append(f"{indent_str}@overload")
               if func_name == "__ffi_init__":
                   # __ffi_init__ is installed as an instance method (self, 
*args, **kwargs) -> None
                   # by _install_ffi_init_attr, regardless of the C++ static 
registration.
                   ret.append(_render_ffi_init_from_method(candidate, ty_map, 
indent, input_ty_map))
                   continue
               if not candidate.is_member:
                   ret.append(f"{indent_str}@staticmethod")
               ret.append(render_func_signature(candidate, ty_map, indent, 
input_ty_map))
   ```



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