Skip to content

Commit db4af20

Browse files
authored
Update mip/readme.md for markdown linting and typos. (#856)
(Not completely proofed, but this takes care of everything I quickly saw.)
1 parent 28911b6 commit db4af20

File tree

1 file changed

+36
-29
lines changed

1 file changed

+36
-29
lines changed

mip/readme.md

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Why you may need typing.[m]py or typing_extensions.[m]py.
55
When making use of static typing in Python or MicroPython, you often end up using types that are defined in the CPython typing module.
66
As Python static typing is 'optimized out' when the source is compiled to byte-code and then to machine-code, there is virtually no runtime overhead.
77

8-
However there is one remaining issue, and this is with the `import typing` or `from typing import ...`
8+
However there is one remaining issue, and this is with the `import typing` or `from typing import ...`.
99
If you try to run an fully typed MicroPython module or script on a MCU, you will get an error like this:
1010

1111
```python
@@ -15,19 +15,22 @@ Traceback (most recent call last):
1515
File "example.py", line 2, in <module>
1616
ImportError: no module named 'typing'
1717
```
18-
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.
1918

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.
2020

2121
## Install the `typing` modules to your MCU
22+
2223
To have the least amount of runtime overhead on your MCU, you should use the cross compiled version of the modules to your MCU.
2324

2425
```bash
2526
mpremote mip install github:josverl/micropython-stubs/mip/typing.json
26-
# or for the cross-compiled version:
27+
# or for the cross-compiled version:
2728
mpremote mip install github:josverl/micropython-stubs/mip/typing_mpy.json
2829
```
30+
2931
This will will output something like:
30-
```
32+
33+
```text
3134
Install github:josverl/micropython-stubs/mip/typing_mpy.json
3235
Installing github:josverl/micropython-stubs/mip/typing_mpy.json to /lib
3336
Installing: /lib/typing.mpy
@@ -36,10 +39,12 @@ Installing __future__ (latest) from https://micropython.org/pi/v2 to /lib
3639
Installing: /lib/__future__.mpy
3740
Done
3841
```
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.*
4145

4246
## Add to your project source
47+
4348
To avoid needing to `mip install` the modules on every MCU, you can add the modules to your project source.
4449
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.
4550

@@ -51,12 +56,13 @@ mpremote cp :lib/typing_extensions.mpy src/lib/typing_extensions.mpy
5156
```
5257

5358
## Best portability
59+
5460
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.
5561

5662
Use the same commands as above, but replace the `.mpy` with `.py` in the commands.
5763

58-
5964
## example code
65+
6066
```python
6167
"""Example typed Micropython module."""
6268
from typing import TYPE_CHECKING, Tuple
@@ -72,17 +78,16 @@ print(f"{foo(1, 2)=}")
7278
### Using the `@no_type_check` decorator with `@asm_xxx`code
7379

7480
**`@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.
7682
It is possible to disable these warnings for the specific sections of code by using the `@no_type_check` decorator.
7783

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.
8586

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.
8691

8792
```python
8893
import typing
@@ -100,7 +105,7 @@ def blink_1hz():
100105
# ...
101106
```
102107

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`).
104109

105110
**The same can be used for `@micropython.asm_thumb` functions**
106111

@@ -110,7 +115,7 @@ import micropython
110115

111116
@typing.no_type_check
112117
@micropython.asm_thumb
113-
def convert2PWM(r0,r1,r2):
118+
def convert2PWM(r0,r1,r2):
114119
#r3=32768
115120
mov(r3,1)
116121
mov(r4,15)
@@ -120,45 +125,47 @@ def convert2PWM(r0,r1,r2):
120125
cmp(r2,10)
121126
bne(PWM8BITS)
122127
#...
123-
```
124-
128+
```
125129

126130
## About the modules
127131

128132
### `typing.py`
133+
129134
A minimalistic `typing.py` module for MicroPython.
130135

131136
:::{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.*
133138
:::
139+
134140
### `typing_extensions.py`
141+
135142
This module is provided to allow the use of older versions of Python (3.7+).
136143

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.
139146

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.
141148

142149
## Cross Compiling
143150

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:
145152

146153
```sh
147154
cd mip
148155
mpy-cross typing.py -O3
149156
mpy-cross typing_extensions.py -O3
150157
```
151158

152-
### Origin
153-
The typing modules are the result of the collaboration of the MicroPython community around a PR to the micropython-lib repository.
159+
### Origin
154160

155-
PR: [micropython-lib:PR584](https://github.com/micropython/micropython-lib/pull/584)
156-
Authors:[stinos](https://github.com/stinos) & [Andrew Leech](https://github.com/andrewleech)
161+
The typing modules are the result of the collaboration of the MicroPython community around a PR to the micropython-lib repository.
157162

163+
PR: [micropython-lib:PR584](https://github.com/micropython/micropython-lib/pull/584)
164+
Authors: [stinos](https://github.com/stinos) & [Andrew Leech](https://github.com/andrewleech)
158165

159166
### References
160167

161168
- Python: [typing — Support for type hints](https://docs.python.org/3/library/typing.html)
162169
- PyPI: [typing_extensions — Type hints for Python](https://pypi.org/project/typing-extensions/)
163-
- MicroPython [`mpremote mip`](https://docs.micropython.org/en/latest/reference/packages.html#installing-packages-with-mpremote)
170+
- MicroPython: [`mpremote mip`](https://docs.micropython.org/en/latest/reference/packages.html#installing-packages-with-mpremote)
164171
- [MicroPython-lib](https://github.com/micropython/micropython-lib)

0 commit comments

Comments
 (0)