Skip to content

fix(dataclasses): Add customized __str__ method to registry #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 27, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion python/mlc/dataclasses/py_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def __str__(self) -> str:
return self.__repr__()


def py_class(
def py_class( # noqa: PLR0915
type_key: str | type | None = None,
*,
init: bool = True,
Expand Down Expand Up @@ -155,6 +155,9 @@ def __new__(cls, *args: typing.Any, **kwargs: typing.Any) -> type[ClsType]:
type_add_method(type_index, "__str__", fn, 1) # static
attach_method(super_type_cls, type_cls, "__repr__", fn, check_exists=True)
attach_method(super_type_cls, type_cls, "__str__", fn, check_exists=True)
elif (fn := vars(super_type_cls).get("__str__", None)) is not None:
assert callable(fn)
type_add_method(type_index, "__str__", fn, 1)
add_vtable_methods_for_type_cls(super_type_cls, type_index=type_index)
return type_cls

Expand Down
Loading