Skip to content

Commit 336168b

Browse files
authored
Merge pull request #1997 from Shaikh-Ubaid/pass_in_dataclass
Support pass in dataclass
2 parents b46a93d + 304107f commit 336168b

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,7 @@ RUN(NAME structs_24 LABELS cpython llvm c)
578578
RUN(NAME structs_25 LABELS cpython llvm c)
579579
RUN(NAME structs_26 LABELS cpython llvm c)
580580
RUN(NAME structs_27 LABELS cpython llvm c)
581+
RUN(NAME structs_28 LABELS cpython llvm c)
581582

582583
RUN(NAME symbolics_01 LABELS cpython_sym c_sym)
583584
RUN(NAME symbolics_02 LABELS cpython_sym c_sym)

integration_tests/structs_28.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from lpython import dataclass, i32
2+
3+
@dataclass
4+
class Pattern:
5+
_foo : str
6+
pass
7+
_n: i32
8+
9+
def main0():
10+
p: Pattern = Pattern("some string", 5)
11+
assert p._foo == "some string"
12+
assert p._n == 5
13+
print(p)
14+
15+
main0()

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2874,20 +2874,22 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
28742874
if (AST::is_a<AST::ConstantStr_t>(*expr->m_value)) {
28752875
// It is a doc string. Skip doc strings for now.
28762876
continue;
2877-
} else {
2878-
throw SemanticError("Only doc strings allowed as expressions inside class", expr->base.base.loc);
28792877
}
2880-
}
2881-
if( AST::is_a<AST::ClassDef_t>(*x.m_body[i]) ) {
2878+
throw SemanticError("Only doc strings allowed as expressions inside class", expr->base.base.loc);
2879+
} else if( AST::is_a<AST::ClassDef_t>(*x.m_body[i]) ) {
28822880
visit_ClassDef(*AST::down_cast<AST::ClassDef_t>(x.m_body[i]));
28832881
continue;
2884-
}
2885-
if ( AST::is_a<AST::FunctionDef_t>(*x.m_body[i]) ) {
2882+
} else if ( AST::is_a<AST::FunctionDef_t>(*x.m_body[i]) ) {
28862883
throw SemanticError("Struct member functions are not supported", x.m_body[i]->base.loc);
2884+
} else if (AST::is_a<AST::Pass_t>(*x.m_body[i])) {
2885+
continue;
2886+
} else if (!AST::is_a<AST::AnnAssign_t>(*x.m_body[i])) {
2887+
throw SemanticError("AnnAssign expected inside struct", x.m_body[i]->base.loc);
28872888
}
2888-
LCOMPILERS_ASSERT(AST::is_a<AST::AnnAssign_t>(*x.m_body[i]));
28892889
AST::AnnAssign_t* ann_assign = AST::down_cast<AST::AnnAssign_t>(x.m_body[i]);
2890-
LCOMPILERS_ASSERT(AST::is_a<AST::Name_t>(*ann_assign->m_target));
2890+
if (!AST::is_a<AST::Name_t>(*ann_assign->m_target)) {
2891+
throw SemanticError("Only Name supported as target in AnnAssign inside struct", x.m_body[i]->base.loc);
2892+
}
28912893
AST::Name_t *n = AST::down_cast<AST::Name_t>(ann_assign->m_target);
28922894
std::string var_name = n->m_id;
28932895
ASR::expr_t* init_expr = nullptr;

0 commit comments

Comments
 (0)