You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A solution is to add a minimalistic `typing.py` file to the project, and when your module or script is executed on the MCU, that will be used in place of the CPython typing module.
19
18
19
+
A solution is to add a minimalistic `typing.py` file to the project, and when your module or script is executed on the MCU, that will be used in place of the CPython typing module.
20
20
21
21
## Install the `typing` modules to your MCU
22
+
22
23
To have the least amount of runtime overhead on your MCU, you should use the cross compiled version of the modules to your MCU.
Installing github:josverl/micropython-stubs/mip/typing_mpy.json to /lib
33
36
Installing: /lib/typing.mpy
@@ -36,10 +39,12 @@ Installing __future__ (latest) from https://micropython.org/pi/v2 to /lib
36
39
Installing: /lib/__future__.mpy
37
40
Done
38
41
```
39
-
*Note:* The .mpy modules are cross compiled for MicroPython v1.25.0 ; mpy-cross emitting mpy v6.3
40
-
_Note that by default mip will install the modules in the `/lib` folder of the MCU._
42
+
43
+
*Note:* The .mpy modules are cross compiled for MicroPython v1.25.0 ; mpy-cross emitting mpy v6.3.
44
+
*Note that by default mip will install the modules in the `/lib` folder of the MCU.*
41
45
42
46
## Add to your project source
47
+
43
48
To avoid needing to `mip install` the modules on every MCU, you can add the modules to your project source.
44
49
mpremote mip does not have a method to retrieve and store modules locally to your project, but it is simple to copy them from your MCU to your local project.
For best portability across MicroPython ports you can use the `typing.py` and `typing_extensions.py` modules. These modules are identical to the `typing.mpy` and `typing_extensions.mpy` modules, but are in source form.
55
61
56
62
Use the same commands as above, but replace the `.mpy` with `.py` in the commands.
57
63
58
-
59
64
## example code
65
+
60
66
```python
61
67
"""Example typed Micropython module."""
62
68
from typing importTYPE_CHECKING, Tuple
@@ -72,17 +78,16 @@ print(f"{foo(1, 2)=}")
72
78
### Using the `@no_type_check` decorator with `@asm_xxx`code
73
79
74
80
**`@asm_pio` functions**
75
-
As RP2 ASM PIO code is not exactly valid Python code, type checkers will show multiple warnings for those code sections.
81
+
As RP2 ASM PIO code is not exactly valid Python code, type checkers will show multiple warnings for those code sections.
76
82
It is possible to disable these warnings for the specific sections of code by using the `@no_type_check` decorator.
77
83
78
-
The `@typing.no_type_check` decorator may be supported by type checkers
79
-
for functions and classes.
80
-
81
-
If a type checker supports the `no_type_check` decorator for functions, it
82
-
should suppress all type errors for the `def` statement and its body including
83
-
any nested functions or classes. It should also ignore all parameter
84
-
and return type annotations and treat the function as if it were unannotated.
84
+
The `@typing.no_type_check` decorator may be supported by type checkers
85
+
for functions and classes.
85
86
87
+
If a type checker supports the `no_type_check` decorator for functions, it
88
+
should suppress all type errors for the `def` statement and its body including
89
+
any nested functions or classes. It should also ignore all parameter
90
+
and return type annotations and treat the function as if it were unannotated.
86
91
87
92
```python
88
93
import typing
@@ -100,7 +105,7 @@ def blink_1hz():
100
105
# ...
101
106
```
102
107
103
-
Typechecking for rp2040 `@asm_pio` code, but has been integrated in the published type stubsfor rp2 (micropython-rp2-stubs.
108
+
Typechecking for rp2040 `@asm_pio` code, but has been integrated in the published type stubs for rp2 (`micropython-rp2-stubs`).
104
109
105
110
**The same can be used for `@micropython.asm_thumb` functions**
106
111
@@ -110,7 +115,7 @@ import micropython
110
115
111
116
@typing.no_type_check
112
117
@micropython.asm_thumb
113
-
defconvert2PWM(r0,r1,r2):
118
+
defconvert2PWM(r0,r1,r2):
114
119
#r3=32768
115
120
mov(r3,1)
116
121
mov(r4,15)
@@ -120,45 +125,47 @@ def convert2PWM(r0,r1,r2):
120
125
cmp(r2,10)
121
126
bne(PWM8BITS)
122
127
#...
123
-
```
124
-
128
+
```
125
129
126
130
## About the modules
127
131
128
132
### `typing.py`
133
+
129
134
A minimalistic `typing.py` module for MicroPython.
130
135
131
136
:::{note}
132
-
_When that PR is merged, or MicroPython itself can provide this functionality, I'll update the above links to point to micropython-lib._
137
+
*When that PR is merged, or MicroPython itself can provide this functionality, I'll update the above links to point to micropython-lib.*
133
138
:::
139
+
134
140
### `typing_extensions.py`
141
+
135
142
This module is provided to allow the use of older versions of Python (3.7+).
136
143
137
-
In CPython the `typing_extensions` module provide back-ported type hints from newer versions and enable use of new type system features on older Python versions.
138
-
For example, typing.TypeGuard is new in Python 3.10, but typing_extensions allows users on previous Python versions to use it too.
144
+
In CPython the `typing_extensions` module provide back-ported type hints from newer versions and enable use of new type system features on older Python versions.
145
+
For example, `typing.TypeGuard` is new in Python 3.10, but typing_extensions allows users on previous Python versions to use it too.
139
146
140
-
As MicroPython has no native typing implementation, the `typing_extensions.py` module provides identicalfunctionality to the `typing.py` module.
147
+
As MicroPython has no native typing implementation, the `typing_extensions.py` module provides identical functionality to the `typing.py` module.
141
148
142
149
## Cross Compiling
143
150
144
-
In order to create the smallest possible `.mpy` versions of the typing modules use
151
+
In order to create the smallest possible `.mpy` versions of the typing modules use:
145
152
146
153
```sh
147
154
cd mip
148
155
mpy-cross typing.py -O3
149
156
mpy-cross typing_extensions.py -O3
150
157
```
151
158
152
-
### Origin
153
-
The typing modules are the result of the collaboration of the MicroPython community around a PR to the micropython-lib repository.
0 commit comments