Skip to content

Remove dataclass emulation #2285

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 3 commits into from
Aug 19, 2023
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions integration_tests/bindc_08.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# file: main.py
from lpython import CPtr, i32, dataclass, c_p_pointer, Pointer, empty_c_void_p, p_c_pointer
from lpython import CPtr, i32, dataclass, c_p_pointer, Pointer, empty_c_void_p, p_c_pointer, Array

from numpy import empty, array

Expand All @@ -9,11 +9,11 @@ class Foo:
y: i32

def init(foos_ptr: CPtr) -> None:
foos: Pointer[Foo[:]] = c_p_pointer(foos_ptr, Foo[:], array([1]))
foos: Pointer[Array[Foo, :]] = c_p_pointer(foos_ptr, Array[Foo, :], array([1]))
foos[0] = Foo(3, 2)

def main() -> None:
foos: Foo[1] = empty(1, dtype=Foo)
foos: Array[Foo, 1] = empty(1, dtype=Foo)
foos_ptr: CPtr = empty_c_void_p()
foos[0] = Foo(0, 1)
p_c_pointer(foos, foos_ptr)
Expand Down
8 changes: 4 additions & 4 deletions integration_tests/structs_05.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from lpython import i32, f64, i64, i16, i8, f32, dataclass, InOut
from lpython import i32, f64, i64, i16, i8, f32, dataclass, InOut, Array
from numpy import empty

@dataclass
Expand All @@ -11,7 +11,7 @@ class A:
c: i8
d: bool

def verify(s: A[:], x1: i32, y1: f64, x2: i32, y2: f64):
def verify(s: Array[A, :], x1: i32, y1: f64, x2: i32, y2: f64):
eps: f64 = 1e-12
s0: A = s[0]
print(s0.x, s0.y, s0.z, s0.a, s0.b, s0.c, s0.d)
Expand Down Expand Up @@ -41,7 +41,7 @@ def update_1(s: InOut[A]):
s.b = i16(2)
s.c = i8(2)

def update_2(s: A[:]):
def update_2(s: Array[A, :]):
s[1].x = 3
s[1].y = 2.3
s[1].z = i64(3)
Expand All @@ -50,7 +50,7 @@ def update_2(s: A[:]):
s[1].c = i8(3)

def g():
y: A[2] = empty([2], dtype=A)
y: Array[A, 2] = empty([2], dtype=A)
y[0] = A(1.1, 1, i64(1), f32(1.1), i16(1), i8(1), True)
y[1] = A(2.2, 2, i64(2), f32(2.2), i16(2), i8(2), True)
verify(y, 1, 1.1, 2, 2.2)
Expand Down
6 changes: 3 additions & 3 deletions integration_tests/structs_25.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
from lpython import i32, dataclass
from lpython import i32, dataclass, Array
from numpy import empty

@dataclass
class Foo:
x: i32
y: i32

def init(foos: Foo[:]) -> None:
def init(foos: Array[Foo, :]) -> None:
foos[0] = Foo(5, 21)

def main0() -> None:
foos: Foo[1] = empty(1, dtype=Foo)
foos: Array[Foo, 1] = empty(1, dtype=Foo)
init(foos)
print("foos[0].x =", foos[0].x)

Expand Down
9 changes: 1 addition & 8 deletions src/runtime/lpython/lpython.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
import ctypes
import platform
from dataclasses import dataclass as py_dataclass, is_dataclass as py_is_dataclass, field
from dataclasses import dataclass, field, is_dataclass as py_is_dataclass
import functools


Expand Down Expand Up @@ -48,13 +48,6 @@ def __getitem__(self, params):
def __call__(self, arg):
return self._convert(arg)

def dataclass(arg):
def __class_getitem__(key):
return Array(arg, key)
arg.__class_getitem__ = __class_getitem__

return py_dataclass(arg)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beautiful! That's exactly what we need.

def is_ctypes_Structure(obj):
return (isclass(obj) and issubclass(obj, ctypes.Structure))

Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-structs_05-fa98307.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"basename": "asr-structs_05-fa98307",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/../integration_tests/structs_05.py",
"infile_hash": "5c587158fe09782d15aa8f5f9c24468c62795e9f537a9eb439d8e8a4",
"infile_hash": "ef1037b0936a63be679efd366920a94463900e80630a070ba440aa78",
"outfile": null,
"outfile_hash": null,
"stdout": "asr-structs_05-fa98307.stdout",
Expand Down