You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
CPython allows uninitialized variable, LPython does not ? #1991
(lp) ┌─(~/CLionProjects/lpython/lasr/LP-pycharm/Issue1990)───────────────────────────────────────────────────────────────────────────────────────────────(brian@Golf37:s014)─┐
└─(13:34:57 on brian-lasr ✖ ✭)──> PYTHONPATH='../../../src/runtime/lpython' python ./lasr_lexer.py ──(Wed,Jun21)─┘
STRINGIO TEST
READ-SEEK-TELL-TEST
LEXER TEST
REGEX TEST
LPython:
(lp) ┌─(~/CLionProjects/lpython/lasr/LP-pycharm/Issue1990)───────────────────────────────────────────────────────────────────────────────────────────────(brian@Golf37:s014)─┐
└─(13:36:44 on brian-lasr ✖ ✭)──> ../../../src/bin/lpython -I. ./lasr_lexer.py ──(Wed,Jun21)─┘
semantic error: `rr` must be initialized with an instance of ReRange
--> ./lasr_lexer.py:139:5
|
139 | rr : ReRange
| ^^^^^^^^^^^^
semantic error: `rr` must be initialized with an instance of ReRange --> a.py:10:5 |10 | rr : ReRange | ^^^^^^^^^^^^
The issue is that rr is used as a loop iterator, so it makes no sense to initialize it ahead of time.
This was done in #1808. I think we should relax this requirement. The reason we did (I think) is that it's relatively easy to enforce the struct to be initialized. But as we can see now, there is a use case when this is not a good idea. @Shaikh-Ubaid if you have time to have a look at this, that would be great.
$ cat examples/expr2.py
from lpython import dataclass, i32
@dataclass
class A:
n: i32
def main0():
a: A
a.n = 5
print(a)
main0()
$ python examples/expr2.py
Traceback (most recent call last):
File "/Users/ubaid/Desktop/OpenSource/lpython/examples/expr2.py", line 12, in<module>main0()
File "/Users/ubaid/Desktop/OpenSource/lpython/examples/expr2.py", line 9, in main0
a.n = 5
UnboundLocalError: local variable 'a' referenced before assignment
$ lpython examples/expr2.py
5
It seems CPython does not define a variable unless it is assigned. Since everything that compiles/runs with LPython should run with CPython, to tackle cases as above, we made it mandatory to initialize the variable if is a not a Simple Type. We need to see for a possible Robust fix/approach here.
because we can freely re-order the tests, than this:
deflexer_test() ->None: # lexer : InOut[LasrLexer]):# Issue #1981print('LEXER TEST')
# first testlexer : LasrLexer=LasrLexer(StringIO(' foo bar '))
__post_init__(lexer.fd)
eat_white(lexer)
t : str=peek_debug(lexer)
# Issue 1971 print({"t": t})assertt=='f', "t == 'f'"# second testlexer=LasrLexer(StringIO('(Integer 4 [])'))
__post_init__(lexer.fd)
t=get_non_white((lexer))
assertt=='(', "t = '('"
The latter is what we must do now, due to LPython limitations. If we reorder the tests, we must edit them to ensure that the first one has the initialization.
Activity
certik commentedon Jun 21, 2023
Hm, I just discussed this precise issue yesterday with our team, I thought it's ok, but I can now see it's not.
Here is the example:
It gives:
The issue is that
rris used as a loop iterator, so it makes no sense to initialize it ahead of time.This was done in #1808. I think we should relax this requirement. The reason we did (I think) is that it's relatively easy to enforce the struct to be initialized. But as we can see now, there is a use case when this is not a good idea. @Shaikh-Ubaid if you have time to have a look at this, that would be great.
ubaidsk commentedon Jun 22, 2023
$ cat examples/expr2.py from lpython import dataclass, i32 @dataclass class A: n: i32 def main0(): a: A a.n = 5 print(a) main0() $ python examples/expr2.py Traceback (most recent call last): File "/Users/ubaid/Desktop/OpenSource/lpython/examples/expr2.py", line 12, in <module> main0() File "/Users/ubaid/Desktop/OpenSource/lpython/examples/expr2.py", line 9, in main0 a.n = 5 UnboundLocalError: local variable 'a' referenced before assignment $ lpython examples/expr2.py 5It seems CPython does not define a variable unless it is assigned. Since everything that compiles/runs with LPython should run with CPython, to tackle cases as above, we made it mandatory to initialize the variable if is a not a Simple Type. We need to see for a possible Robust fix/approach here.
ubaidsk commentedon Jun 22, 2023
If anyone have any ideas, please feel free to share.
For the moment, I am assigning this to me.
certik commentedon Jun 22, 2023
I would fix this issue as follows. This should not be allowed:
Rather, either this:
or this must be required:
The PR #1808 enforces the third option. We should also allow the second option, but not the first. The second option should also allow:
Note that for loop over a list of structs is not supported yet, but eventually it will be supported.
rebcabin commentedon Jun 22, 2023
I'll give another use case for
declaringvariables with types without initializing them: imperative code where re-use of variables is intended.This is better:
because we can freely re-order the tests, than this:
The latter is what we must do now, due to LPython limitations. If we reorder the tests, we must edit them to ensure that the first one has the initialization.