modularml / mojo

The Mojo Programming Language
https://docs.modular.com/mojo/manual/
Other
22.92k stars 2.58k forks source link

[BUG]: Can't pass memory only types in array to PythonObject #1440

Open hoxha-saber opened 9 months ago

hoxha-saber commented 9 months ago

Bug description

The following causes an error

fn main() raises:
     let d = String("abc")
     let n : PythonObject = [d, "apple"]
     print(n)

The error is:

    let n : PythonObject = [d, "apple"]

                           ^

/Users/ec2-user/actions-runner/_work/modular/modular/Kernels/mojo/python/object.mojo:195:5: note:             no viable expansions found

/Users/ec2-user/actions-runner/_work/modular/modular/Kernels/mojo/python/object.mojo:236:33: note:               call expansion failed - no concrete specializations

/Users/ec2-user/actions-runner/_work/modular/modular/Kernels/mojo/algorithm/functional.mojo:56:1: note:                 no viable expansions found

/Users/ec2-user/actions-runner/_work/modular/modular/Kernels/mojo/algorithm/functional.mojo:67:33: note:                   call expansion failed - no concrete specializations

/Users/ec2-user/actions-runner/_work/modular/modular/Kernels/mojo/algorithm/functional.mojo:71:1: note:                     no viable expansions found

/Users/ec2-user/actions-runner/_work/modular/modular/Kernels/mojo/algorithm/functional.mojo:78:18: note:                       call expansion failed - no concrete specializations

/Users/ec2-user/actions-runner/_work/modular/modular/Kernels/mojo/python/object.mojo:210:9: note:                         no viable expansions found

/Users/ec2-user/actions-runner/_work/modular/modular/Kernels/mojo/python/object.mojo:232:18: note:                           constraint failed: cannot convert nested list element to object

mojo: error: failed to run the pass manager

I didn't see any indication from the documents that Python objects containing heterogenous types weren't supported. A common use case could be in python dictionaries where the key and values are different types. Are there any plans to make this work in the future? Thanks

Steps to reproduce

Run the above program

System information

- What OS did you do install Mojo on ?
Apple M1 Max
- Provide version information for Mojo by pasting the output of `mojo -v`
mojo 0.6.0 (d55c0025)
- Provide Modular CLI version by pasting the output of `modular -v`
modular 0.2.2 (95d42445)
jackos commented 8 months ago

Hi @hoxha-saber I believe we should be able to get this working with traits once lifetimes are introduced.

Note your code isn't working because String is memory only (it's allocated to the heap), you can still do it with register passable types:

fn main() raises:
     let n : PythonObject = [5, "apple"]
     print(n)