Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Support field in dataclass member initialization #2280

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
Aug 17, 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
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,7 @@ RUN(NAME structs_31 LABELS cpython llvm c)
RUN(NAME structs_32 LABELS cpython llvm c)
RUN(NAME structs_33 LABELS cpython llvm c)
RUN(NAME structs_34 LABELS cpython llvm c)
RUN(NAME structs_35 LABELS cpython llvm)

RUN(NAME symbolics_01 LABELS cpython_sym c_sym llvm_sym NOFAST)
RUN(NAME symbolics_02 LABELS cpython_sym c_sym llvm_sym NOFAST)
Expand Down
26 changes: 26 additions & 0 deletions integration_tests/structs_35.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from lpython import dataclass, field, i32
from numpy import array

@dataclass
class X:
a: i32 = 123
b: bool = True
c: list[i32] = field(default_factory=lambda: [1, 2, 3])
d: i32[3] = field(default_factory=lambda: array([4, 5, 6]))
e: i32 = field(default=-5)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this the same as:

Suggested change
e: i32 = field(default=-5)
e: i32 = -5

?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, I think they are equivalent.

Copy link
Contributor

Choose a reason for hiding this comment

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

If they are equivalent, then I would almost not support the duplicate way of doing it ("just one way of doing things").


def main0():
x: X = X()
print(x)
assert x.a == 123
assert x.b == True
assert x.c[0] == 1
assert x.d[1] == 5
assert x.e == -5
x.c[0] = 3
x.d[0] = 3
print(x)
assert x.c[0] == 3
assert x.d[0] == 3

main0()
29 changes: 29 additions & 0 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7802,6 +7802,35 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
tmp = ASR::make_SizeOfType_t(al, x.base.base.loc,
arg_type, size_type, nullptr);
return ;
} else if( call_name == "field" ) {
if (x.n_args != 0) {
throw SemanticError("'field' expects only keyword arguments", x.base.base.loc);
}

if (x.n_keywords != 1) {
throw SemanticError("'field' expects one keyword argument", x.base.base.loc);
}

args.reserve(al, 1);
visit_expr_list(x.m_args, x.n_args, args);

if( std::string(x.m_keywords[0].m_arg) != "default_factory" && std::string(x.m_keywords[0].m_arg) != "default" ) {
throw SemanticError("Unrecognised keyword argument, " +
std::string(x.m_keywords[0].m_arg), x.base.base.loc);
}

if ( std::string(x.m_keywords[0].m_arg) == "default_factory") {
if (!AST::is_a<AST::Lambda_t>(*x.m_keywords[0].m_value)) {
throw SemanticError("Only lambda functions currently supported as default_factory value", x.base.base.loc);
}

AST::Lambda_t* lambda_fn = AST::down_cast<AST::Lambda_t>(x.m_keywords[0].m_value);
this->visit_expr(*lambda_fn->m_body);
} else {
// field has default argument provided
this->visit_expr(*x.m_keywords[0].m_value);
}
return ;
} else if(
call_name == "f64" ||
call_name == "f32" ||
Expand Down
4 changes: 2 additions & 2 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
from dataclasses import dataclass as py_dataclass, is_dataclass as py_is_dataclass, field
import functools


Expand All @@ -11,7 +11,7 @@
"overload", "ccall", "TypeVar", "pointer", "c_p_pointer", "Pointer",
"p_c_pointer", "vectorize", "inline", "Union", "static",
"packed", "Const", "sizeof", "ccallable", "ccallback", "Callable",
"Allocatable", "In", "Out", "InOut", "dataclass", "S"]
"Allocatable", "In", "Out", "InOut", "dataclass", "field", "S"]

# data-types

Expand Down