Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions micropython/bundles/bundle-typing/manifest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# type: ignore[all]

metadata(
version="1.26.1",
description="Limited runtime typing support for MicroPython.",
)

options.defaults(opt_level=3, extensions=False)

# Primary typing related modules
require("__future__", opt_level=options.opt_level)
require("typing", opt_level=options.opt_level)
require("abc", opt_level=options.opt_level)

# # Optional typing modules
if options.extensions:
require("typing_extensions", opt_level=options.opt_level)
require("collections-abc", opt_level=options.opt_level)
6 changes: 4 additions & 2 deletions python-stdlib/__future__/manifest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
metadata(version="0.1.0")
metadata(version="1.26.1")

module("__future__.py")
options.defaults(opt_level=3)

module("__future__.py", opt=options.opt_level)
12 changes: 10 additions & 2 deletions python-stdlib/abc/abc.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# type: ignore
class ABC:
pass


def abstractmethod(f):
return f
def abstractmethod(arg):
return arg

try:
# add functionality if typing module is available
from typing import __getattr__ as __getattr__

except: # naked except saves 4 bytes
pass
6 changes: 4 additions & 2 deletions python-stdlib/abc/manifest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
metadata(version="0.1.0")
metadata(version="0.2.0")

module("abc.py")
options.defaults(opt_level=3)

module("abc.py", opt=options.opt_level)
7 changes: 7 additions & 0 deletions python-stdlib/collections-abc/collections/abc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# collections.abc
# minimal support for runtime typing
# type: ignore
try:
from typing import __Ignore as ABC, __getattr__ as __getattr__
except:
pass
5 changes: 5 additions & 0 deletions python-stdlib/collections-abc/manifest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
metadata(version="1.26.1")

# require("collections")
require("typing")
package("collections")
8 changes: 8 additions & 0 deletions python-stdlib/collections/collections/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
from .defaultdict import defaultdict
except ImportError:
pass
# optional collections.abc typing dummy module
try:
# cannot use relative import here
import collections.abc as abc
import sys
sys.modules['collections.abc'] = abc
except ImportError:
pass


class MutableMapping:
Expand Down
8 changes: 8 additions & 0 deletions python-stdlib/typing/manifest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# type: ignore

metadata(version="1.26.1", description="Typing module for MicroPython.")

# default to opt_level 3 for minimal firmware size
options.defaults(opt_level=3)

module("typing.py", opt=options.opt_level)
97 changes: 97 additions & 0 deletions python-stdlib/typing/typing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
"""
This module provides runtime support for type hints.
based on :
- https://github.com/micropython/micropython-lib/pull/584
- https://github.com/Josverl/micropython-stubs/tree/main/mip
- https://github.com/Josverl/rt_typing

"""

# -------------------------------------
# code reduction by Ignoring type hints
# -------------------------------------
class __Ignore:
"""A class to ignore type hints in code."""

def __call__(*args, **kwargs):
# May need some guardrails here
pass

def __getitem__(self, arg):
# May need some guardrails here
return __ignore

def __getattr__(self, attr):
if attr in self.__dict__:
return self.__dict__[attr]
return __ignore


__ignore = __Ignore()
# -----------------
# typing essentials
# -----------------
TYPE_CHECKING = False

def final(arg): # ( 6 + bytes)
# decorator to indicate that a method should not be overridden
# https://docs.python.org/3/library/typing.html#typing.final
return arg


# def overload(arg): # ( 27 bytes)
# # ignore functions signatures with @overload decorator
# return None
overload = __ignore # saves bytes, and is semantically similar

def NewType(_, arg): # (21 bytes)
# https://docs.python.org/3/library/typing.html#newtype
# MicroPython: just use the original type.
return arg

# ---------------
# useful methods
# ---------------

# https://docs.python.org/3/library/typing.html#typing.cast
# def cast(type, arg): # ( 23 bytes)
# return arg
cast = NewType # saves bytes, and is semantically similar

# https://docs.python.org/3/library/typing.html#typing.no_type_check
# def no_type_check(arg): # ( 26 bytes)
# # decorator to disable type checking on a function or method
# return arg
no_type_check = final # saves bytes, and is semantically similar

# -------------------
# less useful methods
# -------------------

# def reveal_type(x): # ( 38 bytes)
# # # https://docs.python.org/3/library/typing.html#typing.reveal_type
# return x
# or for smaller size:
reveal_type = final # saves bytes, and is semantically similar


# def get_origin(type): # ( 23 bytes)
# # https://docs.python.org/3/library/typing.html#typing.get_origin
# # Return None for all unsupported objects.
# return None


# def get_args(arg): # ( 22 bytes)
# # https://docs.python.org/3/library/typing.html#typing.get_args
# # Python 3.8+ only
# return ()


# ref: https://github.com/micropython/micropython-lib/pull/584#issuecomment-2317690854

def __getattr__(x):
return __ignore

# snarky way to alias typing_extensions to typing ( saving 59 bytes)
import sys
sys.modules["typing_extensions"] = sys.modules["typing"]
10 changes: 10 additions & 0 deletions python-stdlib/typing_extensions/manifest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
metadata(version="1.26.1")


# default to opt_level 3 for minimal firmware size
options.defaults(opt_level=3)

module("typing_extensions.py", opt=options.opt_level)

require("typing")
package("typing_extensions")
5 changes: 5 additions & 0 deletions python-stdlib/typing_extensions/typing_extensions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# typing_extensions.py
# type: ignore

from typing import *
from typing import __getattr__
Loading