Skip to content

Commit a74d529

Browse files
Add support for dict methods with Const (#2567)
* Implement attributes for `Const dict` * Remove duplicate changes * Improve checking for `Const` types * Simplify type checking for `Const dict`. * Add tests * Update test * Update fetching attribute name logic Co-authored-by: Thirumalai Shaktivel <74826228+Thirumalai-Shaktivel@users.noreply.github.com> * Update test references * Update fetching `dict_type` Co-authored-by: Thirumalai Shaktivel <74826228+Thirumalai-Shaktivel@users.noreply.github.com> * Formatting changes * Update test references * Update error test references * Tests: Update test references * Tests: Add runtime tests and update test references * Remove checks on the absent `Const` node * Remove call to `get_contained_type()` * Tests: Add tests and update references * Style changes * Tests: Update tests and add to CMakeLists * Delete tests/reference/asr-test_const_dict-151acad.json * Delete tests/reference/asr-test_const_dict-151acad.stdout * Delete tests/reference/asr-test_const_dict-59445d7.json * Delete tests/reference/asr-test_dict_const-69479e2.json * Delete tests/reference/asr-test_dict_const-69479e2.stderr * Delete tests/reference/asr-test_dict_const-69479e2.stdout * Delete tests/reference/runtime-test_dict_const-62054df.json * Delete tests/reference/runtime-test_dict_const-62054df.stderr * Delete tests/reference/asr-test_const_dict-59445d7.stderr * Tests: Update error references * Undo formatting changes * Remove extra newline --------- Co-authored-by: Thirumalai Shaktivel <74826228+Thirumalai-Shaktivel@users.noreply.github.com>
1 parent 8451ad2 commit a74d529

File tree

7 files changed

+59
-0
lines changed

7 files changed

+59
-0
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,7 @@ RUN(NAME test_tuple_03 LABELS cpython llvm llvm_jit c)
562562
RUN(NAME test_tuple_04 LABELS cpython llvm llvm_jit c)
563563
RUN(NAME test_tuple_concat LABELS cpython llvm llvm_jit)
564564
RUN(NAME test_tuple_nested LABELS cpython llvm llvm_jit)
565+
RUN(NAME test_const_dict LABELS cpython llvm llvm_jit)
565566
RUN(NAME test_dict_01 LABELS cpython llvm llvm_jit c)
566567
RUN(NAME test_dict_02 LABELS cpython llvm llvm_jit c NOFAST)
567568
RUN(NAME test_dict_03 LABELS cpython llvm llvm_jit NOFAST)

integration_tests/test_const_dict.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from lpython import i32, f64, Const
2+
3+
CONST_DICTIONARY_INTEGR: Const[dict[str, i32]] = {"a": 1, "b": 2, "c": 3}
4+
5+
print(CONST_DICTIONARY_INTEGR.get("a"))
6+
assert CONST_DICTIONARY_INTEGR.get("a") == 1
7+
8+
print(CONST_DICTIONARY_INTEGR.keys())
9+
assert len(CONST_DICTIONARY_INTEGR.keys()) == 3
10+
11+
print(CONST_DICTIONARY_INTEGR.values())
12+
assert len(CONST_DICTIONARY_INTEGR.values()) == 3
13+
14+
CONST_DICTIONARY_FLOAT: Const[dict[str, f64]] = {"a": 1.0, "b": 2.0, "c": 3.0}
15+
16+
print(CONST_DICTIONARY_FLOAT.get("a"))
17+
assert CONST_DICTIONARY_FLOAT.get("a") == 1.0
18+
19+
print(CONST_DICTIONARY_FLOAT.keys())
20+
assert len(CONST_DICTIONARY_FLOAT.keys()) == 3
21+
22+
print(CONST_DICTIONARY_FLOAT.values())
23+
assert len(CONST_DICTIONARY_FLOAT.values()) == 3
24+

src/lpython/semantics/python_attribute_eval.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,9 @@ struct AttributeHandler {
395395

396396
static ASR::asr_t* eval_dict_pop(ASR::expr_t *s, Allocator &al, const Location &loc,
397397
Vec<ASR::expr_t*> &args, diag::Diagnostics &diag) {
398+
if (ASRUtils::is_const(s)) {
399+
throw SemanticError("cannot pop elements from a const dict", loc);
400+
}
398401
if (args.size() != 1) {
399402
throw SemanticError("'pop' takes only one argument for now", loc);
400403
}

tests/errors/test_const_dict.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from lpython import i32, f64, dict, Const
2+
3+
4+
def test_const_dict():
5+
CONST_DICTIONARY: Const[dict[str, i32]] = {"a": 1, "b": 2, "c": 3}
6+
print(CONST_DICTIONARY.pop("a"))
7+
8+
9+
test_const_dict()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"basename": "asr-test_const_dict-59445d7",
3+
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
4+
"infile": "tests/errors/test_const_dict.py",
5+
"infile_hash": "51130e98c759eb3cdbd50848e59879e4689d241c7a8674aa06a5b3c7",
6+
"outfile": null,
7+
"outfile_hash": null,
8+
"stdout": null,
9+
"stdout_hash": null,
10+
"stderr": "asr-test_const_dict-59445d7.stderr",
11+
"stderr_hash": "1d3729d80a7895dd01baaf0905c6cc9ebadd7f7ce623f4ae5970e2b8",
12+
"returncode": 2
13+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
semantic error: cannot pop elements from a const dict
2+
--> tests/errors/test_const_dict.py:6:11
3+
|
4+
6 | print(CONST_DICTIONARY.pop("a"))
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^

tests/tests.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,10 @@ run = true
11091109
filename = "errors/test_dict16.py"
11101110
run = true
11111111

1112+
[[test]]
1113+
filename = "errors/test_const_dict.py"
1114+
asr = true
1115+
11121116
[[test]]
11131117
filename = "errors/test_zero_division.py"
11141118
asr = true

0 commit comments

Comments
 (0)