Skip to content

feat(dataclass): Support frozen=True in py_class #54

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 21, 2025

Conversation

potatomashed
Copy link
Collaborator

This PR brings support for frozen=True so that one could create dataclass instances with immutability. For example,

import mlc.dataclasses as mlcd

@mlcd.py_class(frozen=True)
class Frozen(mlcd.PyClass):
    a: int
    b: str

x = Frozen(a=1, b="b")

>>> x.a = 2  # not allowed to set a field
AttributeError: property 'a' of 'Frozen' object has no setter

And if one has to forcefully set attributes, there's a private API:

>>> x._mlc_setattr("a", 3)
>>> x.a
3

This design is consistent with python's native dataclass, where it uses object.__setattr__ to force-set fields

@potatomashed
Copy link
Collaborator Author

Should fix #50

@potatomashed potatomashed merged commit ba39078 into mlc-ai:main Apr 21, 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