Skip to content

Support pass in dataclass #1997

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 4 commits into from
Jun 22, 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 @@ -578,6 +578,7 @@ RUN(NAME structs_24 LABELS cpython llvm c)
RUN(NAME structs_25 LABELS cpython llvm c)
RUN(NAME structs_26 LABELS cpython llvm c)
RUN(NAME structs_27 LABELS cpython llvm c)
RUN(NAME structs_28 LABELS cpython llvm c)

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

@dataclass
class Pattern:
_foo : str
pass
_n: i32

def main0():
p: Pattern = Pattern("some string", 5)
assert p._foo == "some string"
assert p._n == 5
print(p)

main0()
18 changes: 10 additions & 8 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2874,20 +2874,22 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
if (AST::is_a<AST::ConstantStr_t>(*expr->m_value)) {
// It is a doc string. Skip doc strings for now.
continue;
} else {
throw SemanticError("Only doc strings allowed as expressions inside class", expr->base.base.loc);
}
}
if( AST::is_a<AST::ClassDef_t>(*x.m_body[i]) ) {
throw SemanticError("Only doc strings allowed as expressions inside class", expr->base.base.loc);
} else if( AST::is_a<AST::ClassDef_t>(*x.m_body[i]) ) {
visit_ClassDef(*AST::down_cast<AST::ClassDef_t>(x.m_body[i]));
continue;
}
if ( AST::is_a<AST::FunctionDef_t>(*x.m_body[i]) ) {
} else if ( AST::is_a<AST::FunctionDef_t>(*x.m_body[i]) ) {
throw SemanticError("Struct member functions are not supported", x.m_body[i]->base.loc);
} else if (AST::is_a<AST::Pass_t>(*x.m_body[i])) {
continue;
} else if (!AST::is_a<AST::AnnAssign_t>(*x.m_body[i])) {
throw SemanticError("AnnAssign expected inside struct", x.m_body[i]->base.loc);
}
LCOMPILERS_ASSERT(AST::is_a<AST::AnnAssign_t>(*x.m_body[i]));
AST::AnnAssign_t* ann_assign = AST::down_cast<AST::AnnAssign_t>(x.m_body[i]);
LCOMPILERS_ASSERT(AST::is_a<AST::Name_t>(*ann_assign->m_target));
if (!AST::is_a<AST::Name_t>(*ann_assign->m_target)) {
throw SemanticError("Only Name supported as target in AnnAssign inside struct", x.m_body[i]->base.loc);
}
AST::Name_t *n = AST::down_cast<AST::Name_t>(ann_assign->m_target);
std::string var_name = n->m_id;
ASR::expr_t* init_expr = nullptr;
Expand Down