lcompilers / lpython

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

Using `list()` on a `list` variable does not work #2667

Open Shaikh-Ubaid opened 2 months ago

Shaikh-Ubaid commented 2 months ago
% cat examples/expr2.py   
from lpython import i32

a: list[i32]
a = [2, 3]
b: list[i32]
b = list(a)
print(b)
% python examples/expr2.py
[2, 3]
% lpython examples/expr2.py
semantic error: Type mismatch in assignment, the types must be compatible
 --> examples/expr2.py:6:1
  |
6 | b = list(a)
  | ^   ^^^^^^^ type mismatch ('list[i32]' and 'list[str]')

Note: Please report unclear or confusing messages as bugs at
https://github.com/lcompilers/lpython/issues.
kmr-srbh commented 2 months ago

I have a working fix ready for this. I wish to discuss some design decisions before pushing the changes.

We essentially need generics to fully achieve this. The error in the above example stated list[str] because that is the only implementation we currently have for list() inside lpython_builtin.py. I have written some implementations for list of integers and floats, but we cannot handle arbitrary nesting this way. For now, we may handle some nesting by hand, like list[list[i32]]. Although it is not a good idea, it will help us to test methods like dict.keys and dict.values more clearly. Currently tests for both these methods check for the length of the returned list.

@Shaikh-Ubaid @certik @czgdp1807 What is your take on this?