Add support for dict methods with Const#2567
Conversation
Const dictdict methods with Const
|
@kmr-srbh What issue does this PR fix? |
I did not create a separate issue for this as I had worked out a fix. The main problem was that accessing values inside In the testing process I found that the dictionary methods were still failing. This PR adds support for using dictionary methods on a |
Why don't you create an issue first and list all the bugs related to That would help highlight the contributions made and also make it simpler to review. Thanks! |
|
Also, you do not need to share the output as an image. Instead, you can simply share it in markdown as: (lp) ubaid@ubaids-MacBook-Pro lpython % cat examples/expr2.py
from lpython import i32
def main0():
x: i32
x = (2+3)*5
print(x)
if __name__ == "__main__":
main0()(lp) ubaid@ubaids-MacBook-Pro lpython % lpython examples/expr2.py
25Sharing in text format as above is helpful as you can also search for some keyword in your code later. The markdown code for the above code snippet is: ```python
(lp) ubaid@ubaids-MacBook-Pro lpython % cat examples/expr2.py
from lpython import i32
def main0():
x: i32
x = (2+3)*5
print(x)
if __name__ == "__main__":
main0()
```
```console
(lp) ubaid@ubaids-MacBook-Pro lpython % lpython examples/expr2.py
25
```
|
|
Please mark this PR ready for review once it is ready. |
|
@Shaikh-Ubaid @Thirumalai-Shaktivel This PR is now ready. |
Co-authored-by: Thirumalai Shaktivel <[email protected]>
Co-authored-by: Thirumalai Shaktivel <[email protected]>
|
|
I will the share the output. I think this should get back to working with a few small changes. The changes we had previously made to handle const list (merged) works. I think we will have to replace the Thanks a lot for looking into this @Shaikh-Ubaid! |
|
@Shaikh-Ubaid the PR is ready. Here is an example that works: CONST_DICT: Const[dict[str, i32]] = {"a": 1, "b": 2, "c": 3}
print(CONST_DICT.get("a"))
print(CONST_DICT.keys())
print(CONST_DICT.values())
# print(CONST_DICT.pop("a"))(base) saurabh-kumar@Awadh:~/Projects/System/lpython$ ./src/bin/lpython ./examples/example.py
1
['a', 'b', 'c']
[1, 2, 3]Uncommenting the last line throws the required error: (base) saurabh-kumar@Awadh:~/Projects/System/lpython$ ./src/bin/lpython ./examples/example.py
semantic error: cannot pop elements from a const dict
--> ./examples/example.py:6:7
|
6 | print(CONST_DICT.pop("a"))
| ^^^^^^^^^^^^^^^^^^^
Note: Please report unclear or confusing messages as bugs at
https://github.com/lcompilers/lpython/issues. |
ubaidsk
left a comment
There was a problem hiding this comment.
Thanks for this! I shared some comments. I will review again after these are resolved.
|
@Shaikh-Ubaid I have made the necessary changes. We tests Here is an example that fails: def f():
CONST_DICT: Const[dict[str, i32]] = {"a": 1, "b": 2, "c": 3}
print(CONST_DICT.get("a"))
print(CONST_DICT.keys())
print(CONST_DICT.values())
f() |


Fixes #2585
Support for following attributes was implemented:
dict.get- fetches the required valuedict.pop- throws aSemanticErrorstating that changes cannot be made to aconst dictdict.keys- fetches the list of keysdict.values- fetches the list of valuesget()
pop()
keys()
values()