vasigorc
vasigorc

Reputation: 962

Mojo not recognizing Python types

Looking at this document, I realize that this feature may not be supported yet, but I will give it a try. Here is my dilemma. I have a tiny mojo file (magic intiated inside a Python project) which imports a Python library. It looks like this:

from python import Python

def main():
    datasets = Python.import_module("sklearn.datasets")
    iris = datasets.load_iris(as_frame=True)
    print(iris.data.head(3))

This works:

✗ mojo main.mojo
   sepal length (cm)  sepal width (cm)  petal length (cm)  petal width (cm)
0                5.1               3.5                1.4               0.2
1                4.9               3.0                1.4               0.2
2                4.7               3.2                1.3               0.2

If I try to re-organize it just a little bit, it stops working:

from python import Python

def load_iris_dataset(): 
    datasets = Python.import_module("sklearn.datasets")
    iris = datasets.load_iris(as_frame=True)
    return iris

def main():
    iris = load_iris_dataset()
    print(iris.data.head(3))

This is the shell output when running it:

<REDACTED>/main.mojo:6:12: error: ambiguous call to '__init__', each candidate requires 1 implicit conversion, disambiguate with an explicit cast
    return iris
           ^~~~
<REDACTED>/main.mojo:1:1: note: candidate declared here
from python import Python
^
<REDACTED>/main.mojo:1:1: note: candidate declared here
from python import Python
^
mojo: error: failed to parse the provided Mojo source module

But I also get this error from VSCode's Errors:

cannot implicitly convert 'PythonObject' value to 'object'

Could someone please explain, why does this behavior kick-in only when trying to pass PythonObjects between methods? What am I missing? Is Mojo just not ready to work with Python's dynamic types in an organized manner?

Upvotes: 0

Views: 40

Answers (0)

Related Questions