python-stdlib: Add typing modules that can be frozen or mip installable.
This is a improved MicroPython alternative to
- the .c based minimal typing module proposed in https://github.com/micropython/micropython/pull/15911
- the .pyhon based https://github.com/micropython/micropython-lib/pull/584
- https://github.com/Josverl/micropython-stubs/tree/main/mip#micropythons-typingmpy-module
based on all the above and tuned for reduced firmware/frozen size, while keeping provable consistence with the relevant typing PEPs for features that are supported by MicroPython.
It consists of a number of modules that can be mip-installed or frozen as part of a firmware..
Note that the modules have been optimized for size - somewhat impacting readability - but where relevant the orignal code has been kept as comments.
Documentation
Documentation is not created yet.
- [ ] Document availability of the modules
- [ ] Document the use of the bundle-typing and it's options
Testing:
The same set of tests has been run against both implementations and while there are a few differences , both pass the majority of tests. The remaining differences to CPython should be documented as such, and then can be excluded from the verification tests.
The tests are created at part of MicroPython's tests suite , and are mot included in this PR. Currently they are part of https://github.com/micropython/micropython/pull/15911, but could be separated for testing ( may require a new variant for testing in this in the MicroPython repo)
The PR includes the bundle-typing to allow simple addition to a board defintition
# typing related modules
require(
"bundle-typing",
# extensions=False, # Set to True to include collections.abc
# opt_level=2,
)
This also defaults the mpy-cross opt level to 3, to reduce firmware size.
The impact to firmware size of the modtyping.c and the typing.py variant are surprisingly close,
although I think there may be room for additional optimisation for the modtyping.c alternative.
Comparison without collections.abc
Note: both PRs are changing , so the below is a point-in-time comparison ( 14/10/'25)
| method | modules included | size (bytes) | diff |
|---|---|---|---|
| standard | - | 841_295 | 0 |
| modtyping.c | no collections.abc | 843887 | 2592 |
| typing.py | no collections.abc | 843807 | 2.512 (-80) |
Test Results:
| Module | count | FIXME |
|---|---|---|
| typing.py | 3 | |
| - [ ] FIXME: from collections.abc import Callable | ||
| - [ ] FIXME: document cpy_diff - get_args(int) should be () | ||
| - [ ] FIXME: document cpydiff : Final cannot be used with container types | ||
| modtyping.c | 6 | |
| - [ ] FIXME: Difference or Crash - multiple bases have instance lay-out conflict: multiple bases have instance lay-out conflict | ||
| - [ ] FIXME: Difference or Crash - multiple bases have instance lay-out conflict: multiple bases have instance lay-out conflict | ||
| - [ ] FIXME: from collections.abc import Callable | ||
| - [ ] FIXME: document cpy_diff - get_origin(str) should be None | ||
| - [ ] FIXME: document cpy_diff - get_args(int) should be () | ||
| - [ ] FIXME: document cpydiff : Final cannot be used with container types |
Comparison including collections.abc
| method | modules included | size (bytes) | diff |
|---|---|---|---|
| standard | - | 841_295 | 0 |
| modtyping.c | no collections.abc | 843_887 | 2592 |
| typing.py | with collections.abc | 844_063 | 2668 (+76 bytes) |
Test results:
| Module | count | FIXME |
|---|---|---|
| typing.py | 2 | Size: 844063 |
| - [ ] FIXME: document cpy_diff - get_args(int) should be () | ||
| - [ ] FIXME: document cpydiff : Final cannot be used with container types | ||
| modtyping.c | 6 | Size: 843887 |
| - [ ] FIXME: Difference or Crash - multiple bases have instance lay-out conflict: multiple bases have instance lay-out conflict | ||
| - [ ] FIXME: Difference or Crash - multiple bases have instance lay-out conflict: multiple bases have instance lay-out conflict | ||
| - [ ] FIXME: from collections.abc import Callable | ||
| - [ ] FIXME: document cpy_diff - get_origin(str) should be None | ||
| - [ ] FIXME: document cpy_diff - get_args(int) should be () | ||
| - [ ] FIXME: document cpydiff : Final cannot be used with container types |
@stinos, could you please review ?
To make comparisons easier, could you share gcc/micropython version and a script which you used, or even just a set of commands, to get these results? Having rebased my code on master and comparing sizes and results, things seem different for me so I want to make sure we're doing roughly the same thing; for instance:
- a unix build with everything from this PR enabled is 3072 bytes larger than the standard build, compared to 2668 from your results. That seems substantial.
- in that same build,
typing.get_origin(str) is NoneisTruebut according to your results it should pass
In any case, I'm hitting something funny; I made a windows msvc build with your typing bundle, I can see frozen_content.c including the frozen typing code code, yet I get an ImportError trying to import any of the modules contained in the manifest. Probably I'm gonna try and sort that out first.
@stinos , please find the notebook and the supporting files for the 'jos' variant in the gist.
https://gist.github.com/Josverl/f953fb49a4cac63759bd2056f728a725#file-readme-md
I built locally on WSL Ubuntu 22.04.5 LTS with :
gcc (Ubuntu 11.4.0-1ubuntu1~22.04.2) 11.4.0
I get an ImportError trying to import any of the modules contained in the manifest
Oops, my MICROPYPATH didn't have a .frozen entry.
please find the notebook and the supporting files for the 'jos' variant in the gist.
Ok thanks. I was doing almost the same, however using multiple variants in different branches where I might have messed something up at the time I wrote the previous comment, and using gcc 9.4 or so. Will check again next week. Not sure if this is of any use, but here's a PS script for comparing typing variants: https://gist.github.com/stinos/2ee6138679a39a75628f85b5765a71b1