Skip to content

Commit 171ad86

Browse files
committed
feat(dataclasses): Remove PyClass
1 parent 8147fbb commit 171ad86

31 files changed

+370
-268
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ MLC provides Pythonic dataclasses:
3636
import mlc.dataclasses as mlcd
3737

3838
@mlcd.py_class("demo.MyClass")
39-
class MyClass(mlcd.PyClass):
39+
class MyClass:
4040
a: int
4141
b: str
4242
c: float | None
@@ -117,7 +117,7 @@ By annotating IR definitions with `structure`, MLC supports structural equality
117117
import mlc.dataclasses as mlcd
118118

119119
@mlcd.py_class
120-
class Expr(mlcd.PyClass):
120+
class Expr:
121121
def __add__(self, other):
122122
return Add(a=self, b=other)
123123

cpp/registry.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
namespace mlc {
1919
namespace registry {
2020

21-
Any JSONLoads(AnyView json_str);
21+
Any JSONParse(AnyView json_str);
2222
Any JSONDeserialize(AnyView json_str, FuncObj *fn_opaque_deserialize);
2323
Str JSONSerialize(AnyView source, FuncObj *fn_opaque_serialize);
2424
bool StructuralEqual(AnyView lhs, AnyView rhs, bool bind_free_vars, bool assert_mode);
@@ -646,7 +646,7 @@ inline TypeTable *TypeTable::New() {
646646
self->SetFunc("mlc.base.DeviceTypeRegister",
647647
Func([self](const char *name) { return self->DeviceTypeRegister(name); }).get());
648648
self->SetFunc("mlc.core.Stringify", Func(::mlc::core::StringifyWithFields).get());
649-
self->SetFunc("mlc.core.JSONLoads", Func(::mlc::registry::JSONLoads).get());
649+
self->SetFunc("mlc.core.JSONParse", Func(::mlc::registry::JSONParse).get());
650650
self->SetFunc("mlc.core.JSONSerialize", Func(::mlc::registry::JSONSerialize).get());
651651
self->SetFunc("mlc.core.JSONDeserialize", Func(::mlc::registry::JSONDeserialize).get());
652652
self->SetFunc("mlc.core.StructuralEqual", Func(::mlc::registry::StructuralEqual).get());

cpp/structure.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ using mlc::core::VisitStructure;
2323

2424
/****************** JSON ******************/
2525

26-
inline Any JSONLoads(const char *json_str, int64_t json_str_len) {
26+
inline Any JSONParse(const char *json_str, int64_t json_str_len) {
2727
struct JSONParser {
2828
Any Parse() {
2929
SkipWhitespace();
@@ -1552,7 +1552,7 @@ inline Any Deserialize(const char *json_str, int64_t json_str_len, FuncObj *fn_o
15521552
int32_t json_type_index_tensor = -1;
15531553
int32_t json_type_index_opaque = -1;
15541554
// Step 0. Parse JSON string
1555-
UDict json_obj = JSONLoads(json_str, json_str_len);
1555+
UDict json_obj = JSONParse(json_str, json_str_len);
15561556
// Step 1. type_key => constructors
15571557
UList type_keys = json_obj->at("type_keys");
15581558
std::vector<FuncObj *> constructors;
@@ -1700,12 +1700,12 @@ Any CopyShallow(AnyView source) { return CopyShallowImpl(source); }
17001700
Any CopyDeep(AnyView source) { return CopyDeepImpl(source); }
17011701
void CopyReplace(int32_t num_args, const AnyView *args, Any *ret) { CopyReplaceImpl(num_args, args, ret); }
17021702

1703-
Any JSONLoads(AnyView json_str) {
1703+
Any JSONParse(AnyView json_str) {
17041704
if (json_str.type_index == kMLCRawStr) {
1705-
return ::mlc::JSONLoads(json_str.operator const char *(), -1);
1705+
return ::mlc::JSONParse(json_str.operator const char *(), -1);
17061706
} else {
17071707
StrObj *js = json_str.operator StrObj *();
1708-
return ::mlc::JSONLoads(js->data(), js->size());
1708+
return ::mlc::JSONParse(js->data(), js->size());
17091709
}
17101710
}
17111711

python/mlc/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,17 @@
1313
Tensor,
1414
build_info,
1515
dep_graph,
16+
eq_ptr,
17+
eq_s,
18+
eq_s_fail_reason,
19+
hash_s,
20+
json_dumps,
1621
json_loads,
22+
json_parse,
1723
typing,
1824
)
1925
from .core.dep_graph import DepGraph, DepNode
20-
from .dataclasses import PyClass, c_class, py_class
26+
from .dataclasses import c_class, py_class
2127

2228
try:
2329
from ._version import __version__, __version_tuple__ # type: ignore[import-not-found]

python/mlc/core/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
from .dict import Dict
44
from .dtype import DataType
55
from .error import Error
6-
from .func import Func, build_info, json_loads
6+
from .func import Func, build_info, json_parse
77
from .list import List
8-
from .object import Object
8+
from .object import Object, eq_ptr, eq_s, eq_s_fail_reason, hash_s, json_dumps, json_loads
99
from .object_path import ObjectPath
1010
from .opaque import Opaque
1111
from .tensor import Tensor

python/mlc/core/func.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ def decorator(func: _CallableType) -> _CallableType:
5151
return decorator
5252

5353

54-
def json_loads(s: str) -> Any:
55-
return _json_loads(s)
54+
def json_parse(s: str) -> Any:
55+
return _json_parse(s)
5656

5757

5858
def build_info() -> dict[str, Any]:
5959
return _build_info()
6060

6161

62-
_json_loads = Func.get("mlc.core.JSONLoads")
62+
_json_parse = Func.get("mlc.core.JSONParse")
6363
_build_info = Func.get("mlc.core.BuildInfo")

python/mlc/core/object.py

Lines changed: 115 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55

66
from mlc._cython import PyAny, TypeInfo, c_class_core
77

8+
try:
9+
from warnings import deprecated
10+
except ImportError:
11+
from typing_extensions import deprecated
12+
813

914
@c_class_core("object.Object")
1015
class Object(PyAny):
@@ -21,42 +26,6 @@ def id_(self) -> int:
2126
def is_(self, other: Object) -> bool:
2227
return isinstance(other, Object) and self._mlc_address == other._mlc_address
2328

24-
def json(
25-
self,
26-
fn_opaque_serialize: Callable[[list[typing.Any]], str] | None = None,
27-
) -> str:
28-
return super()._mlc_json(fn_opaque_serialize)
29-
30-
@staticmethod
31-
def from_json(
32-
json_str: str,
33-
fn_opaque_deserialize: Callable[[str], list[typing.Any]] | None = None,
34-
) -> Object:
35-
return PyAny._mlc_from_json(json_str, fn_opaque_deserialize) # type: ignore[attr-defined]
36-
37-
def eq_s(
38-
self,
39-
other: Object,
40-
*,
41-
bind_free_vars: bool = True,
42-
assert_mode: bool = False,
43-
) -> bool:
44-
return PyAny._mlc_eq_s(self, other, bind_free_vars, assert_mode) # type: ignore[attr-defined]
45-
46-
def eq_s_fail_reason(
47-
self,
48-
other: Object,
49-
*,
50-
bind_free_vars: bool = True,
51-
) -> tuple[bool, str]:
52-
return PyAny._mlc_eq_s_fail_reason(self, other, bind_free_vars)
53-
54-
def hash_s(self) -> int:
55-
return PyAny._mlc_hash_s(self) # type: ignore[attr-defined]
56-
57-
def eq_ptr(self, other: typing.Any) -> bool:
58-
return isinstance(other, Object) and self._mlc_address == other._mlc_address
59-
6029
def __copy__(self: Object) -> Object:
6130
return PyAny._mlc_copy_shallow(self) # type: ignore[attr-defined]
6231

@@ -74,7 +43,7 @@ def __hash__(self) -> int:
7443
return hash((type(self), self._mlc_address))
7544

7645
def __eq__(self, other: typing.Any) -> bool:
77-
return self.eq_ptr(other)
46+
return eq_ptr(self, other)
7847

7948
def __ne__(self, other: typing.Any) -> bool:
8049
return not self == other
@@ -103,3 +72,112 @@ def swap(self, other: typing.Any) -> None:
10372
self._mlc_swap(other)
10473
else:
10574
raise TypeError(f"Cannot different types: `{type(self)}` and `{type(other)}`")
75+
76+
@deprecated(
77+
"Method `.json` is deprecated. Use `mlc.json_dumps` instead.",
78+
stacklevel=2,
79+
)
80+
def json(
81+
self,
82+
fn_opaque_serialize: Callable[[list[typing.Any]], str] | None = None,
83+
) -> str:
84+
return json_dumps(self, fn_opaque_serialize)
85+
86+
@deprecated(
87+
"Method `.from_json` is deprecated. Use `mlc.json_loads` instead.",
88+
stacklevel=2,
89+
)
90+
@staticmethod
91+
def from_json(
92+
json_str: str,
93+
fn_opaque_deserialize: Callable[[str], list[typing.Any]] | None = None,
94+
) -> Object:
95+
return json_loads(json_str, fn_opaque_deserialize)
96+
97+
@deprecated(
98+
"Method `.eq_s` is deprecated. Use `mlc.eq_s` instead.",
99+
stacklevel=2,
100+
)
101+
def eq_s(
102+
self,
103+
other: Object,
104+
*,
105+
bind_free_vars: bool = True,
106+
assert_mode: bool = False,
107+
) -> bool:
108+
return eq_s(self, other, bind_free_vars=bind_free_vars, assert_mode=assert_mode)
109+
110+
@deprecated(
111+
"Method `.eq_s_fail_reason` is deprecated. Use `mlc.eq_s_fail_reason` instead.",
112+
stacklevel=2,
113+
)
114+
def eq_s_fail_reason(
115+
self,
116+
other: Object,
117+
*,
118+
bind_free_vars: bool = True,
119+
) -> tuple[bool, str]:
120+
return eq_s_fail_reason(self, other, bind_free_vars=bind_free_vars)
121+
122+
@deprecated(
123+
"Method `.hash_s` is deprecated. Use `mlc.hash_s` instead.",
124+
stacklevel=2,
125+
)
126+
def hash_s(self) -> int:
127+
return hash_s(self)
128+
129+
@deprecated(
130+
"Method `.eq_ptr` is deprecated. Use `mlc.eq_ptr` instead.",
131+
stacklevel=2,
132+
)
133+
def eq_ptr(self, other: typing.Any) -> bool:
134+
return eq_ptr(self, other)
135+
136+
137+
def json_dumps(
138+
object: typing.Any,
139+
fn_opaque_serialize: Callable[[list[typing.Any]], str] | None = None,
140+
) -> str:
141+
assert isinstance(object, Object), f"Expected `mlc.Object`, got `{type(object)}`"
142+
return object._mlc_json(fn_opaque_serialize) # type: ignore[attr-defined]
143+
144+
145+
def json_loads(
146+
json_str: str,
147+
fn_opaque_deserialize: Callable[[str], list[typing.Any]] | None = None,
148+
) -> Object:
149+
return PyAny._mlc_from_json(json_str, fn_opaque_deserialize) # type: ignore[attr-defined]
150+
151+
152+
def eq_s(
153+
lhs: typing.Any,
154+
rhs: typing.Any,
155+
*,
156+
bind_free_vars: bool = True,
157+
assert_mode: bool = False,
158+
) -> bool:
159+
assert isinstance(lhs, Object), f"Expected `mlc.Object`, got `{type(lhs)}`"
160+
assert isinstance(rhs, Object), f"Expected `mlc.Object`, got `{type(rhs)}`"
161+
return PyAny._mlc_eq_s(lhs, rhs, bind_free_vars, assert_mode) # type: ignore[attr-defined]
162+
163+
164+
def eq_s_fail_reason(
165+
lhs: typing.Any,
166+
rhs: typing.Any,
167+
*,
168+
bind_free_vars: bool = True,
169+
) -> tuple[bool, str]:
170+
assert isinstance(lhs, Object), f"Expected `mlc.Object`, got `{type(lhs)}`"
171+
assert isinstance(rhs, Object), f"Expected `mlc.Object`, got `{type(rhs)}`"
172+
return PyAny._mlc_eq_s_fail_reason(lhs, rhs, bind_free_vars)
173+
174+
175+
def hash_s(obj: typing.Any) -> int:
176+
assert isinstance(obj, Object), f"Expected `mlc.Object`, got `{type(obj)}`"
177+
return PyAny._mlc_hash_s(obj) # type: ignore[attr-defined]
178+
179+
180+
def eq_ptr(lhs: typing.Any, rhs: typing.Any) -> bool:
181+
assert isinstance(lhs, Object), f"Expected `mlc.Object`, got `{type(lhs)}`"
182+
assert isinstance(rhs, Object), f"Expected `mlc.Object`, got `{type(rhs)}`"
183+
return lhs._mlc_address == rhs._mlc_address

python/mlc/dataclasses/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
from mlc.core.object import Object as PyClass # for backward compatibility
2+
13
from .c_class import c_class
2-
from .py_class import PyClass, py_class
4+
from .py_class import py_class
35
from .utils import (
46
Structure,
57
add_vtable_method,

0 commit comments

Comments
 (0)