[Feature request] Provide a optional pure-python mode like Cython does #922
Replies: 4 comments
-
This seems like a smart and proven approach. |
Beta Was this translation helpful? Give feedback.
-
While I can sympathize with the idea of compatibility with Python, we might have future Mojo constructs which will not be representable in Python at all. For example, if Mojo finds a way to have type annotations in lambda, it would be hard to do that in Python as AFAIK Python syntax does not allow it. Also it will add constraints to Mojo as in future the developers would expect that new syntax can be represented in Python compatibility mode also. Another risk is that the Mojo codebase getting split into "clean" and "compatible " codebases... Overall I am not so enthusiastic about this... |
Beta Was this translation helpful? Give feedback.
-
To me, pure-python mode is |
Beta Was this translation helpful? Give feedback.
-
Another way to do this is to transform python AST into mojo AST using some predefined rules. https://github.com/py2many/py2many/blob/main/tests/cases/classes.py |
Beta Was this translation helpful? Give feedback.
-
Hi all, I speak as a library maintainer who likes this new language a lot!
I like the mentality of "Mojo meets developers where they are". And Python devs are making libraries and distributing them with Pypi. It would be awesome if Mojo could allow them to continue this workflow without another Pypi-like distribution or another codebase (maintaining two versions of the same library would be very sad!). Cython is very similar to mojo, has already been there and has a solution for this, the optional pure-python mode. You can see the doc here: https://cython.readthedocs.io/en/latest/src/tutorial/pure.html
example:
Long story short, it would look like this:
Python users not working with mojo can still install this package if the package has the dependency
mojo-compatibility
(similary to cython being an importable module in pure python). Users can install it withpip install mojo-compatibility
.The Python compatiblity implementation
If one looks into the python definition of the
mojo_compatibility
module, they will see something very similar to cython:That would allow mojo code to be easily distributed through Pypi and library maintainers to support both languages. Of course it would be a best-effort since not all the Mojo syntax can have a pure Python equivalent syntaxe.
Also note that this is purely optional and will likely be used by only a small portion of Mojo users, specifically the library maintainers.
Bonus
Cython has been handling this for quite a while, and they have a switch for this:
If we could also have that in Mojo it would allow library maintainers to call libraries like numpy if executed with the python interpreter, and if executed with mojo, would be able to run the super fast mojo code directly (thus making numpy an optional dependency for example.
I really encourage people to take a look at Cython for the Python compatility, they do it very well :)
Conclusion
I believe this would jumpstart the distribution of mojo code since the infrastructure (pypi, wheels, pip, etc) is already there. Yes this optional syntax is combersome and not every feature of mojo can be supported through it, but library maintainers are usually advanced users who don't mind a few more lines here and there if it can help with maintainability and distribution.
Beta Was this translation helpful? Give feedback.
All reactions