lcompilers / lpython

Python compiler
https://lpython.org/
Other
1.37k stars 156 forks source link

Add compile-time support for `dict.values` #2661

Closed kmr-srbh closed 2 months ago

kmr-srbh commented 2 months ago

Working

from lpython import i32

print({1: "a"}.values())
print({"a": 1, "b": 2, "c": 3}.values())
print({1: [1, 2, 3], 2: [4, 5, 6], 3: [7, 8, 9]}.values())
print({(1, 2): "a", (3, 4): "b", (5, 6): "c"}.values())

k_1: list[list[i32]] = {
    "list1": [1, 2, 3],
    "list2": [4, 5, 6],
    "list3": [7, 8, 9],
}.values()
print(k_1)

k_2: list[str] = {(1, 2): "a", (3, 4): "b", (5, 6): "c"}.values()
print(k_2)

CPython

(base) saurabh-kumar@Awadh:~/Projects/System/lpython$ python ./examples/example.py
dict_values(['a'])
dict_values([1, 2, 3])
dict_values([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
dict_values(['a', 'b', 'c'])
dict_values([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
dict_values(['a', 'b', 'c'])

LPython

(base) saurabh-kumar@Awadh:~/Projects/System/lpython$ ./src/bin/lpython ./examples/example.py
['a']
[1, 2, 3]
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
['a', 'b', 'c']
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
['a', 'b', 'c']
Shaikh-Ubaid commented 2 months ago

@kmr-srbh whenever something works with lpython, we also need it to work with cpython and produce a similar output. Can you post the output for the example in the PR description for cpython? Can you update your other PRs to show output for cpython? Thanks!

kmr-srbh commented 2 months ago

Can you update your other PRs to show output for cpython?

I am posting the output. :+1:

Shaikh-Ubaid commented 2 months ago
% cat examples/expr2.py    
print({1: "a"}.values())
% python examples/expr2.py 
dict_values(['a'])
% lpython examples/expr2.py
['a']
% cat examples/expr2.py   
print({1: "a"}.values()[0])
% python examples/expr2.py 
Traceback (most recent call last):
  File "/Users/ubaid/Desktop/OpenSource/lpython/examples/expr2.py", line 1, in <module>
    print({1: "a"}.values()[0])
TypeError: 'dict_values' object is not subscriptable
% lpython examples/expr2.py
a

It seems technically a list and a dict_values are not the same.

Shaikh-Ubaid commented 2 months ago

Please mark as "Ready for review" when ready.

Shaikh-Ubaid commented 2 months ago

@certik do you have views on https://github.com/lcompilers/lpython/pull/2661#issuecomment-2078563958? This PR returns dict.values() as list. Similarly other PR https://github.com/lcompilers/lpython/pull/2660 returns dict.keys() as list. In cpython dict.keys() and dict.values() return as object dict_values and object dict_keys.

kmr-srbh commented 2 months ago

@Shaikh-Ubaid I return a list for dict.keys and dict.values following https://github.com/lcompilers/lpython/issues/1881#issuecomment-1605977472

Shaikh-Ubaid commented 2 months ago

I return a list for dict.keys and dict.values following https://github.com/lcompilers/lpython/issues/1881#issuecomment-1605977472

Thanks for sharing. It makes sense.

Shaikh-Ubaid commented 2 months ago

After https://github.com/lcompilers/lpython/pull/2660 gets merged, there can be conflicts in this PR. Please resolve them after https://github.com/lcompilers/lpython/pull/2660 is merged. I will give it a final review then.

kmr-srbh commented 2 months ago

@Shaikh-Ubaid the conflicts have been resolved.