-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Typing module #190
Comments
The MicroPython compiler itself supports type annotations, so in principle the typing module will work. But there are no plans to port it. Feel free to try doing it yourself (you can start just by copying the CPython implementation, like some of the other modules here) and reporting about it here or on https://forum.micropython.org |
Thanks. Which Python version - if any - does Micropython track? |
As per the README in the top level of the main repo (https://github.com/micropython/micropython/blob/master/README.md) it's Python 3.4 with a bit of extra stuff (eg async/await keywords) from version 3.5. |
Ah, thanks! I only looked inside the So when porting, would it make sense to use the current 3.6 backport directly? I could probably give it a stab this week. Would be useful to be able to typecheck micropython projects with mypy. |
Yes, because that will likely work and will be more up to date (eg with bug fixes) than earlier versions. |
I just checked. The module makes a lot of use of the It would probably be best to write dummy classes for all types. That way the typechecking could not be done on Micropython itself, but mypy running on regular python would probably work, with proper stubs. |
Few comments:
|
In this case, typing was introduced in 3.5 and has evolved a lot so 3.6 is probably a better implementation. |
I saw the micropython-typing module. I can provide a hand. I'm also writing stub files for esp8266 port (1.9.2 currently). I'll release it as soon as I finish machine. Stub files I'm writing are using typing library. It'd be great if micropython would expose a fake typing module. Just to prevent import errors. Two cases are important |
I would really love to have a working typing module for Micropython. Sadly the CPython |
I had a closer look into CPythons typing module and think it would not fit the cause to make a complete port. It is rather complex and makes use of metaclasses which Micropython doesn't support. Also it would take a lot of unnecessary resources as static type-checkers like mypy only exist for CPython at the moment. I think the workflow for static type-checking Micropython code at the moment looks more like this:
I think for such use-cases a fake typing module as proposed by @Cediddi would do the job. It allows to import typing classes like Any, Union or the |
@stlehmann yes, I agree (as already suggested in my older comment). Stubs would be sufficient, you don't want to do typechecking on the target platform. |
I added the fake modules in #297. It works for simple types like |
I think a better way would be to avoid running the typing code, rather than shoe-horning a static annotations into the runtime. For now, simple |
I always use mpy-cross to compile py files before uploading to the board. Type annotations and stubs would help me develop my code faster, not run like a staticly typed program. Type annotations are ignored in runtime, they are just annotations. |
Yes, but other typing code (like |
I'm not sure about |
I also thought no code will get executed if no type-checking is done. But if you use a type-annotation like this:
in the interpreter you will get a SyntaxError on execution. |
Ok but with function annotations this works just fine:
This will execute without problems. Seems like type-annotations for simple objects is not supported by micropython. |
Yes, PEP 526 is not implemented in MicroPython. |
Alright, I didn' t know that. Is there a reason why it is not implemented? Actually it seems like a little thing to do if function-annotations are implemented already. |
@Cediddi A typing module with fake typing classes leads to working micropython code with type-annotations included. There would be no need to compile it to get rid of the annotations anymore. |
I wonder if it is possible to write a typescript like transpiler for micropython, so that an extra module (~2000 lines) isn't needed. |
G'day - reviving this dead issue 🧟
The dummy module provided by @stlehmann kind of gets the job done in significantly less code. It's not ideal, but I think a complete type-removing transpiler would be overkill. Anyway I'm commenting because @stlehmann 's pull request was only ever merged into pfalcon's Also, some of my code utilizes the |
Is it possible to at least have something like the |
How about something like this? try:
from typing import TYPE_CHECKING
except ImportError:
TYPE_CHECKING = False
if TYPE_CHECKING:
from typing import ... Or this? if getattr(globals(), '__annotations__'):
from typing import ... |
@dlech hmm |
A quick github search for |
It's actually very important to have as it allows you to import things to satisfy type hinting that would otherwise cause circular dependencies. It's defined True by the type checking tools, should be False in the library https://adamj.eu/tech/2021/05/13/python-type-hints-how-to-fix-circular-imports/ |
The #297 merge request looks to have useful code. So if anyone is interested, they can take that code and re-base it on the latest master, so we can get it included. |
Agreed! I was no aware of that merge request. Thanks :)
|
Hi all,
raises Shall we just conditionally import typing when developing with the python interpreter and then executing in uPy? |
My suggestions for today's versions are documented here Also there are PRs to add s typing module to either micropython, or micropython-lib |
Are there any plans to port the type annotations from the typing module to Micropython?
The text was updated successfully, but these errors were encountered: