MrYsLab / pseudo-microbit

A pseudo implementation of the micro:bit micropython API.
Other
16 stars 4 forks source link

Circular dependencies in the version 1.12 #11

Closed astynax closed 2 weeks ago

astynax commented 2 weeks ago

Hi! For now the __init__.py imports the display.py but then the display.py tries to do from . import Image and the whole import process fails with

>>> from microbit import *
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/.../microbit/__init__.py", line 31, in <module>
    from . import (display as display, uart as uart, spi as spi, i2c as i2c,
  File "/.../microbit/display.py", line 12, in <module>
    from . import Image
ImportError: cannot import name 'Image' from partially initialized module 'microbit' (most likely due to a circular import) (/.../microbit/__init__.py)
MrYsLab commented 2 weeks ago

I do not see the issue when creating a new PyCharm project. image

Could you please supply the steps for me to recreate the issue?

Thanks.

astynax commented 2 weeks ago

PyCharm doesn't interpret user's code, it just analyses all the code statically. That's why cyclic imports aren't appearing during the code editing. But if you try to import microbit in Python's REPL, you'll get the "cyclic import" error.

I see the test file in the repo. Is that code suitable to run with "big" Python or it only was meant to be running on the Microbit? If you'll try to run the test file on the computer you will get this:

$ env PYTHONPATH=$(pwd) python3 tests/imports.py 
Traceback (most recent call last):
  File "/.../pseudo-microbit/tests/imports.py", line 1, in <module>
    from microbit import *
  File "/.../pseudo-microbit/microbit/__init__.py", line 31, in <module>
    from . import (display as display, uart as uart, spi as spi, i2c as i2c,
    ...<2 lines>...
                   speaker as speaker, audio as audio)
  File "/.../pseudo-microbit/microbit/display.py", line 12, in <module>
    from . import Image
ImportError: cannot import name 'Image' from partially initialized module 'microbit' (most likely due to a circular import) (/.../pseudo-microbit/microbit/__init__.py)
MrYsLab commented 2 weeks ago

Now I understand. The purpose of pseudo-microbit is to provide micro-bit aware editing within PyCharm. It was not intended to be run from within a standard Python REPL. I apologize if that is not clear from the documentation. The README.md states: This library is a pseudo implementation of the micro:bit micropython API used in conjunction with PyCharm to check for syntax errors and to provide type hints.

The user guide has a similar statement.

astynax commented 2 weeks ago

No-no, I completely understand the purpose of the package, that side is very clear! Yet, your code looks like a proper python package even it works as a bunch of NoOps. But for now it is broken because of circular imports.

Personally, I'll be glad to be able to import any "faked" stuff into the real Python for example for the sake of tests. I may have a project with Python on the server and on the Microbit. In that case I may want to test a "common code" even if any Microbit's peripherals wouldn't work - they only should be importable. Yes, I can factor out any "platform independent" Python code into the separate module and unittest it separately, but it will be a little bit less fun :)

MrYsLab commented 2 weeks ago

I agree it would be a nice feature, but I am not willing to tackle it. If one does not need PyCharm's integrated GitHub interface, I now believe that the micro:bit Python editor is the better way to go. It has a very nice integrated simulator and links to the API. I am going to close this issue. Thanks for your input.