Open rebcabin opened 1 year ago
The following code can be used as a workaround:
from lpython import (dataclass, i32)
@dataclass
class Foo:
string : str
@dataclass
class Bar :
il : list[i32]
foo : Foo = Foo('')
bar : Bar = Bar([1, 2, 3], Foo('bar'))
Do you think the above code violates your program design? (if so, we need to try other workaround or fix the actual bug)
I'm not so much worried about workarounds as I am about fixing bugs. My program and my work is all about finding bugs in LPython so that we can ship LPython to customers. I am trying to break LPython. I am trying to pretend to be a naive customer doing "reasonable" things and running into problems. The more bugs we fix by end of July, the happier customers will be.
The workaround was good, and I'm not blocked :) Still, this is a usability bug that will confuse customers (as it confused me :)
There are basically two issues here:
from lpython import (dataclass,)
@dataclass class Foo: string : str
bar : Foo = Foo()
This example will fail in CPython, but passes in LPython -- I think I know how to fix it here.
2. The second bug is what @rebcabin has reported in the very first example. CPython acts okay here with its policy of having initial value of all the structs members just because it has ignored the type annotations, and everything is fine at runtime.
```py
# CPython works even with this as it ignores type annotations/checking
from lpython import (dataclass, i32)
@dataclass
class Foo:
string : str
@dataclass
class Bar :
foo : Foo
il : list[i32]
bar : Bar = Bar(32, [1, 2, 3])
print(bar.foo, bar.il)
LPython deals here a bit differently. I discussed the same with @certik some time ago, and the reason why LPython wants structs to be initialized is to avoid accidentally using unallocated structs (or their members).
There is a fundamental inconsistency between LPython and CPython. I do not know how to make a single Python file with the following features that passes both. This issue is a minimization and clarification of Issue #2031, but not exactly a duplicate.
Consider
This passes CPython, but not LPython:
ok, so we'll add that initializer. Now it fails CPython but not LPython:
run:
Ok, so we'll add the initializer. Now it still fails CPython, and there seems to be no fix because lpython does not actually export
field
anddefault_factory
:run: