Open vietanhdev opened 11 months ago
This program has the same error:
from python import Python
def main2():
var str_a: String = "abcd"
var x: (Int, Int, String) = (1, 2, str_a)
var a = Python.dict()
a[(1, 2, False)] = x
print(a[(1, 2, False)][0].to_float64().to_int())
a.__setitem__((1, 3, False), 12345)
print(a.__getitem__((1, 3, False)).to_float64().to_int())
print("hello")
fn main():
try:
_ = main2()
except:
pass
While this does not:
from python import Python
def main2():
var str_a: StringLiteral = "abcd"
var x: (Int, Int, StringLiteral) = (1, 2, str_a)
var a = Python.dict()
a[(1, 2, False)] = x
print(a[(1, 2, False)][0].to_float64().to_int())
a.__setitem__((1, 3, False), 12345)
print(a.__getitem__((1, 3, False)).to_float64().to_int())
print("hello")
fn main():
try:
_ = main2()
except:
pass
Maybe because String
cannot be used as a key? Do you have any workaround?
I fixed this bug by flattening all the arrays. However, I suppose that it's a bug in Mojo.
Even the following fails. So does not seem to be issue of Tuple in key, but a tuple with one element being String.
from python import Python
def main():
let s: String = "abcd"
let x: (Int, String) = (1, s)
let a = Python.dict()
a["akey"] = x
The following also does not compile.
from python import Python
def main():
let s: String = "abcd"
let x: (String,) = (s,)
let a = Python.dict()
a["akey"] = x
and the following compiles:
from python import Python
def main():
let s: String = "abcd"
let x: (Int, Int) = (1, 2)
let a = Python.dict()
a["akey"] = x
Seems only register_passable elements are accepted. The following fails:
from python import Python
@value
struct S:
var i: Int
def main():
let s: S = 1
let x: (S,) = (s,)
let a = Python.dict()
a["akey"] = x
The following passes:
from python import Python
@value
@register_passable
struct S:
var i: Int
def main():
let s: S = 1
let x: (S,) = (s,)
let a = Python.dict()
a["akey"] = x
This is a limitation of the builtin Tuple
type as it only works with register passable types. Can potentially be resolved with traits @modularml/stdlib
This is a limitation of the builtin
Tuple
type as it only works with register passable types. Can potentially be resolved with traits @modularml/stdlib
Yep, this is something on my radar as we look to lift the reg-passable restriction on a lot of types in the stdlib now that we have traits. Thanks for tagging me.
Tuple now works with non-register-passable values. The problem with this code:
def main():
var s: S = 1
var x: (S,) = (s,)
var a = Python.dict()
a["akey"] = x
Is that we're allowing an arbitrary conversion from any tuple of CollectionElement
members to a PythonObject.
This isn't safe at all. We need to constrain this conversion to only "Pythonable" (or whatever) types that know how to convert themselves to a PythonObject.
Bug description
I am developing a chess engine with Mojo here: https://github.com/vietanhdev/chess.mojo/blob/96caf7bf3cebe12c626fc049cd4733d095462579/engine.mojo. My program had a problem using a Tuple as Python dictionary key.