Skip to content

Commit 20405dd

Browse files
committed
Parse dict[str, i32] style annotation
1 parent 8491912 commit 20405dd

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,20 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
204204
throw SemanticError("Only Name in Subscript supported for now in `set`"
205205
" annotation", loc);
206206
}
207+
} else if (var_annotation == "dict") {
208+
if (AST::is_a<AST::Tuple_t>(*s->m_slice)) {
209+
AST::Tuple_t *t = AST::down_cast<AST::Tuple_t>(s->m_slice);
210+
if (t->n_elts != 2) {
211+
throw SemanticError("`dict` annotation must have 2 elements: types"
212+
" of both keys and values", loc);
213+
}
214+
ASR::ttype_t *key_type = ast_expr_to_asr_type(loc, *t->m_elts[0]);
215+
ASR::ttype_t *value_type = ast_expr_to_asr_type(loc, *t->m_elts[1]);
216+
return ASRUtils::TYPE(ASR::make_Dict_t(al, loc, key_type, value_type));
217+
} else {
218+
throw SemanticError("`dict` annotation must have 2 elements: types of"
219+
" both keys and values", loc);
220+
}
207221
} else {
208222
ASR::dimension_t dim;
209223
dim.loc = loc;

0 commit comments

Comments
 (0)