gemini-code-assist[bot] commented on code in PR #411:
URL: https://github.com/apache/tvm-ffi/pull/411#discussion_r2690339959
##########
python/tvm_ffi/dataclasses/_utils.py:
##########
@@ -98,6 +97,52 @@ def fill_dataclass_field(type_cls: type, type_field:
TypeField) -> None:
type_field.dataclass_field = rhs
+def method_repr(type_cls: type, type_info: TypeInfo) -> Callable[..., str]:
+ """Generate a ``__repr__`` method for the dataclass.
+
+ The generated representation includes all fields with ``repr=True`` in
+ the format ``ClassName(field1=value1, field2=value2, ...)``.
+ """
+ # Step 0. Collect all fields from the type hierarchy
+ fields: list[TypeField] = []
+ cur_type_info: TypeInfo | None = type_info
+ while cur_type_info is not None:
+ fields.extend(reversed(cur_type_info.fields))
+ cur_type_info = cur_type_info.parent_type_info
+ fields.reverse()
Review Comment:

The logic for collecting fields from the type hierarchy is also present in
`method_init` (lines 153-158 in the full file). To avoid code duplication and
improve maintainability, consider extracting this logic into a shared helper
function. For example:
```python
def _get_all_fields(type_info: TypeInfo) -> list[TypeField]:
"""Collect all fields from the type hierarchy, from parents to
children."""
fields: list[TypeField] = []
cur_type_info: TypeInfo | None = type_info
while cur_type_info is not None:
fields.extend(reversed(cur_type_info.fields))
cur_type_info = cur_type_info.parent_type_info
fields.reverse()
return fields
```
Then both `method_repr` and `method_init` can be simplified to `fields =
_get_all_fields(type_info)`.
##########
python/tvm_ffi/dataclasses/_utils.py:
##########
@@ -98,6 +97,52 @@ def fill_dataclass_field(type_cls: type, type_field:
TypeField) -> None:
type_field.dataclass_field = rhs
+def method_repr(type_cls: type, type_info: TypeInfo) -> Callable[..., str]:
+ """Generate a ``__repr__`` method for the dataclass.
+
+ The generated representation includes all fields with ``repr=True`` in
+ the format ``ClassName(field1=value1, field2=value2, ...)``.
+ """
+ # Step 0. Collect all fields from the type hierarchy
+ fields: list[TypeField] = []
+ cur_type_info: TypeInfo | None = type_info
+ while cur_type_info is not None:
+ fields.extend(reversed(cur_type_info.fields))
+ cur_type_info = cur_type_info.parent_type_info
+ fields.reverse()
+
+ # Step 1. Filter fields that should appear in repr
+ repr_fields: list[str] = []
+ for field in fields:
+ assert field.name is not None
+ assert field.dataclass_field is not None
+ if field.dataclass_field.repr:
+ repr_fields.append(field.name)
+
+ # Step 2. Generate the repr method
+ if not repr_fields:
+ # No fields to show, return a simple class name representation
+ body_lines = [f"return f'{type_cls.__name__}()'"]
+ else:
+ # Build field representations
+ field_reprs = []
+ for field_name in repr_fields:
+ field_reprs.append(f"{field_name}={{self.{field_name}!r}}")
+ fields_str = ", ".join(field_reprs)
Review Comment:

This block can be made more concise by using a generator expression within
`', '.join()`. This is more idiomatic and readable.
```suggestion
fields_str = ", ".join(f"{field_name}={{self.{field_name}!r}}" for
field_name in repr_fields)
```
--
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]