Skip to content

Commit 40bcd66

Browse files
committed
python-stdlib: Add modules to support typing.
typing, typing_extensions and collections.abc Signed-off-by: Jos Verlinde <jos_verlinde@hotmail.com>
1 parent 3eaf027 commit 40bcd66

File tree

6 files changed

+132
-0
lines changed

6 files changed

+132
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# collections.abc
2+
# minimal support for runtime typing
3+
# type: ignore
4+
try:
5+
from typing import __Ignore as ABC, __getattr__ as __getattr__
6+
except:
7+
pass
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
metadata(version="1.26.1")
2+
3+
# require("collections")
4+
require("typing")
5+
package("collections")

python-stdlib/typing/manifest.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# type: ignore
2+
3+
metadata(version="1.26.1", description="Typing module for MicroPython.")
4+
5+
# default to opt_level 3 for minimal firmware size
6+
options.defaults(opt_level=3)
7+
8+
module("typing.py", opt=options.opt_level)

python-stdlib/typing/typing.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
"""
2+
This module provides runtime support for type hints.
3+
based on :
4+
- https://github.com/micropython/micropython-lib/pull/584
5+
- https://github.com/Josverl/micropython-stubs/tree/main/mip
6+
- https://github.com/Josverl/rt_typing
7+
8+
"""
9+
10+
# -------------------------------------
11+
# code reduction by Ignoring type hints
12+
# -------------------------------------
13+
class __Ignore:
14+
"""A class to ignore type hints in code."""
15+
16+
def __call__(*args, **kwargs):
17+
# May need some guardrails here
18+
pass
19+
20+
def __getitem__(self, arg):
21+
# May need some guardrails here
22+
return __ignore
23+
24+
def __getattr__(self, attr):
25+
if attr in self.__dict__:
26+
return self.__dict__[attr]
27+
return __ignore
28+
29+
30+
__ignore = __Ignore()
31+
# -----------------
32+
# typing essentials
33+
# -----------------
34+
TYPE_CHECKING = False
35+
36+
def final(arg): # ( 6 + bytes)
37+
# decorator to indicate that a method should not be overridden
38+
# https://docs.python.org/3/library/typing.html#typing.final
39+
return arg
40+
41+
42+
# def overload(arg): # ( 27 bytes)
43+
# # ignore functions signatures with @overload decorator
44+
# return None
45+
overload = __ignore # saves bytes, and is semantically similar
46+
47+
def NewType(_, arg): # (21 bytes)
48+
# https://docs.python.org/3/library/typing.html#newtype
49+
# MicroPython: just use the original type.
50+
return arg
51+
52+
# ---------------
53+
# useful methods
54+
# ---------------
55+
56+
# https://docs.python.org/3/library/typing.html#typing.cast
57+
# def cast(type, arg): # ( 23 bytes)
58+
# return arg
59+
cast = NewType # saves bytes, and is semantically similar
60+
61+
# https://docs.python.org/3/library/typing.html#typing.no_type_check
62+
# def no_type_check(arg): # ( 26 bytes)
63+
# # decorator to disable type checking on a function or method
64+
# return arg
65+
no_type_check = final # saves bytes, and is semantically similar
66+
67+
# -------------------
68+
# less useful methods
69+
# -------------------
70+
71+
# def reveal_type(x): # ( 38 bytes)
72+
# # # https://docs.python.org/3/library/typing.html#typing.reveal_type
73+
# return x
74+
# or for smaller size:
75+
reveal_type = final # saves bytes, and is semantically similar
76+
77+
78+
# def get_origin(type): # ( 23 bytes)
79+
# # https://docs.python.org/3/library/typing.html#typing.get_origin
80+
# # Return None for all unsupported objects.
81+
# return None
82+
83+
84+
# def get_args(arg): # ( 22 bytes)
85+
# # https://docs.python.org/3/library/typing.html#typing.get_args
86+
# # Python 3.8+ only
87+
# return ()
88+
89+
90+
# ref: https://github.com/micropython/micropython-lib/pull/584#issuecomment-2317690854
91+
92+
def __getattr__(x):
93+
return __ignore
94+
95+
# snarky way to alias typing_extensions to typing ( saving 59 bytes)
96+
import sys
97+
sys.modules["typing_extensions"] = sys.modules["typing"]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
metadata(version="1.26.1")
2+
3+
4+
# default to opt_level 3 for minimal firmware size
5+
options.defaults(opt_level=3)
6+
7+
module("typing_extensions.py", opt=options.opt_level)
8+
9+
require("typing")
10+
package("typing_extensions")
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# typing_extensions.py
2+
# type: ignore
3+
4+
from typing import *
5+
from typing import __getattr__

0 commit comments

Comments
 (0)