Skip to content

Commit f63b829

Browse files
authored
fix(dataclasses): Add customized __str__ method to registry (#64)
1 parent 12b2773 commit f63b829

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

python/mlc/dataclasses/py_class.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def __str__(self) -> str:
4444
return self.__repr__()
4545

4646

47-
def py_class(
47+
def py_class( # noqa: PLR0915
4848
type_key: str | type | None = None,
4949
*,
5050
init: bool = True,
@@ -155,6 +155,9 @@ def __new__(cls, *args: typing.Any, **kwargs: typing.Any) -> type[ClsType]:
155155
type_add_method(type_index, "__str__", fn, 1) # static
156156
attach_method(super_type_cls, type_cls, "__repr__", fn, check_exists=True)
157157
attach_method(super_type_cls, type_cls, "__str__", fn, check_exists=True)
158+
elif (fn := vars(super_type_cls).get("__str__", None)) is not None:
159+
assert callable(fn)
160+
type_add_method(type_index, "__str__", fn, 1)
158161
add_vtable_methods_for_type_cls(super_type_cls, type_index=type_index)
159162
return type_cls
160163

0 commit comments

Comments
 (0)