Skip to content

feat(dataclasses): Introduce mlc.dataclasses.stringify #69

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 2 commits into from
May 10, 2025

Conversation

potatomashed
Copy link
Collaborator

@potatomashed potatomashed commented May 10, 2025

With this PR, a dataclass defined in C++ could automatically have a __str__ method that prints it just like Python's dataclass, e.g.

mlc.testing.c_class@0x000103b1bb28(bool_=False, i8=8, i16=16, i32=32, i64=64, f32=1.500000, f64=2.500000, 
raw_ptr=0xdeadbeef, dtype=float8, device=cuda:0, any="hello", func=object.Func@0x000103a7b910, 
ulist=[1, 2.000000, "three", object.Func@0x000103a7b910], 
udict={"2": 2.000000, "4": object.Func@0x000103a7b910, "1": 1, "3": "three"}, 
str_="world", str_readonly="world", list_any=[1, 2.000000, "three", object.Func@0x000103a7b910], 
list_list_int=[[1, 2, 3], [4, 5, 6]], dict_any_any={2.000000: 2, 4: object.Func@0x000103a7b910, 1: 1.000000, "three": "four"}, 
dict_str_any={"4": object.Func@0x000103a7b910, "1": 1.000000, "2.0": 2, "three": "four"}, 
dict_any_str={2.000000: "2", 4: "5", 1: "1.0", "three": "four"}, 
dict_str_list_int={"2": [4, 5, 6], "1": [1, 2, 3]}, opt_bool=True, opt_i64=-64, opt_f64=None, opt_raw_ptr=None, 
opt_dtype=None, opt_device=cuda:0, opt_func=None, opt_ulist=None, opt_udict=None, opt_str=None, opt_list_any=[1, 2.000000, "three", object.Func@0x000103a7b910], 
opt_list_list_int=[[1, 2, 3], [4, 5, 6]], opt_dict_any_any=None, opt_dict_str_any={"4": object.Func@0x000103a7b910, "1": 1.000000, "2.0": 2, "three": "four"}, 
opt_dict_any_str={2.000000: "2", 4: "5", 1: "1.0", "three": "four"}, opt_dict_str_list_int={"2": [4, 5, 6], "1": [1, 2, 3]})

This method can be explicitly invoked via mlc.dataclasses.stringify and should work for both c_class and py_class.

@potatomashed potatomashed marked this pull request as ready for review May 10, 2025 07:01
@potatomashed potatomashed force-pushed the cpp-str branch 2 times, most recently from e55a8ce to 5ae8a95 Compare May 10, 2025 16:25
@potatomashed potatomashed changed the title feat(dataclasses): Add dataclass style __str__ to c_class feat(dataclasses): Introduce mlc.dataclasses.stringify May 10, 2025
@potatomashed potatomashed merged commit 04be216 into mlc-ai:main May 10, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant