-
Notifications
You must be signed in to change notification settings - Fork 162
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
Add support for dict
methods with Const
#2567
Conversation
Const dict
dict
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
25 Sharing 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 <74826228+Thirumalai-Shaktivel@users.noreply.github.com>
Co-authored-by: Thirumalai Shaktivel <74826228+Thirumalai-Shaktivel@users.noreply.github.com>
|
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks good. Thanks.
Fixes #2585
Support for following attributes was implemented:
dict.get
- fetches the required valuedict.pop
- throws aSemanticError
stating that changes cannot be made to aconst dict
dict.keys
- fetches the list of keysdict.values
- fetches the list of valuesget()
pop()
keys()
values()