Skip to content

Commit

Permalink
Update typing* and abc module to improve runtime for typed files.
Browse files Browse the repository at this point in the history
related: micropython/micropython-lib#584
Signed-off-by: Jos Verlinde <Jos.Verlinde@microsoft.com>
  • Loading branch information
Josverl committed Aug 30, 2024
1 parent 73b9a8e commit 5e7b40f
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 244 deletions.
Binary file added mip/abc.mpy
Binary file not shown.
13 changes: 13 additions & 0 deletions mip/abc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""
abc.py - Micropython runtime Abstract Base Classes module
"""

from typing import _any_call # type: ignore


def abstractmethod(funcobj):
return funcobj


def __getattr__(attr):
return _any_call
Binary file modified mip/typing.mpy
Binary file not shown.
141 changes: 18 additions & 123 deletions mip/typing.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
"""
This module provides runtime support for type hints.
based on : https://github.com/micropython/micropython-lib/pull/584
"""


def cast(type, val):
return val

Expand All @@ -18,139 +24,28 @@ def overload(func):
return None


def NewType(name, type):
return type


class _AnyCall:
"""A class to ignore type hints in code."""

def __init__(*args, **kwargs):
pass

def __call__(*args, **kwargs):
pass

def __getitem__(self, arg):
return _anyCall

_anyCall = _AnyCall()


class _SubscriptableType:
def __getitem__(self, arg):
return _anyCall


_Subscriptable = _SubscriptableType()


def TypeVar(type, *types):
return None


def NewType(name, type):
return type


class Any:
pass


class BinaryIO:
pass


class ClassVar:
pass

return _any_call

class Final:
pass

_any_call = _AnyCall()

class Hashable:
pass


class IO:
pass


class NoReturn:
pass


class Sized:
pass


class SupportsInt:
pass


class SupportsFloat:
pass


class SupportsComplex:
pass


class SupportsBytes:
pass


class SupportsIndex:
pass


class SupportsAbs:
pass


class SupportsRound:
pass


class TextIO:
pass


AnyStr = str
Text = str
Pattern = str
Match = str
TypedDict = dict
TYPE_CHECKING = False

AbstractSet = _Subscriptable
AsyncContextManager = _Subscriptable
AsyncGenerator = _Subscriptable
AsyncIterable = _Subscriptable
AsyncIterator = _Subscriptable
Awaitable = _Subscriptable
Callable = _Subscriptable
ChainMap = _Subscriptable
Collection = _Subscriptable
Container = _Subscriptable
ContextManager = _Subscriptable
Coroutine = _Subscriptable
Counter = _Subscriptable
DefaultDict = _Subscriptable
Deque = _Subscriptable
Dict = _Subscriptable
FrozenSet = _Subscriptable
Generator = _Subscriptable
Generic = _Subscriptable
Iterable = _Subscriptable
Iterator = _Subscriptable
List = _Subscriptable
Literal = _Subscriptable
Mapping = _Subscriptable
MutableMapping = _Subscriptable
MutableSequence = _Subscriptable
MutableSet = _Subscriptable
NamedTuple = _Subscriptable
Optional = _Subscriptable
OrderedDict = _Subscriptable
Sequence = _Subscriptable
Set = _Subscriptable
Tuple = _Subscriptable
Type = _Subscriptable
Union = _Subscriptable

TYPE_CHECKING = False
# ref: https://github.com/micropython/micropython-lib/pull/584#issuecomment-2317690854
def __getattr__(attr):
return _any_call
Binary file modified mip/typing_extensions.mpy
Binary file not shown.
139 changes: 18 additions & 121 deletions mip/typing_extensions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
"""
This module provides runtime support for type hints.
based on : https://github.com/micropython/micropython-lib/pull/584
"""


def cast(type, val):
return val

Expand All @@ -18,137 +24,28 @@ def overload(func):
return None


def NewType(name, type):
return type


class _AnyCall:
"""A class to ignore type hints in code."""

def __init__(*args, **kwargs):
pass

def __call__(*args, **kwargs):
pass


_anyCall = _AnyCall()


class _SubscriptableType:
def __getitem__(self, arg):
return _anyCall


_Subscriptable = _SubscriptableType()


def TypeVar(type, *types):
return None


def NewType(name, type):
return type


class Any:
pass


class BinaryIO:
pass


class ClassVar:
pass

return _any_call

class Final:
pass

_any_call = _AnyCall()

class Hashable:
pass


class IO:
pass


class NoReturn:
pass


class Sized:
pass


class SupportsInt:
pass


class SupportsFloat:
pass


class SupportsComplex:
pass


class SupportsBytes:
pass


class SupportsIndex:
pass


class SupportsAbs:
pass


class SupportsRound:
pass


class TextIO:
pass


AnyStr = str
Text = str
Pattern = str
Match = str
TypedDict = dict
TYPE_CHECKING = False

AbstractSet = _Subscriptable
AsyncContextManager = _Subscriptable
AsyncGenerator = _Subscriptable
AsyncIterable = _Subscriptable
AsyncIterator = _Subscriptable
Awaitable = _Subscriptable
Callable = _Subscriptable
ChainMap = _Subscriptable
Collection = _Subscriptable
Container = _Subscriptable
ContextManager = _Subscriptable
Coroutine = _Subscriptable
Counter = _Subscriptable
DefaultDict = _Subscriptable
Deque = _Subscriptable
Dict = _Subscriptable
FrozenSet = _Subscriptable
Generator = _Subscriptable
Generic = _Subscriptable
Iterable = _Subscriptable
Iterator = _Subscriptable
List = _Subscriptable
Literal = _Subscriptable
Mapping = _Subscriptable
MutableMapping = _Subscriptable
MutableSequence = _Subscriptable
MutableSet = _Subscriptable
NamedTuple = _Subscriptable
Optional = _Subscriptable
OrderedDict = _Subscriptable
Sequence = _Subscriptable
Set = _Subscriptable
Tuple = _Subscriptable
Type = _Subscriptable
Union = _Subscriptable

TYPE_CHECKING = False
# ref: https://github.com/micropython/micropython-lib/pull/584#issuecomment-2317690854
def __getattr__(attr):
return _any_call

0 comments on commit 5e7b40f

Please sign in to comment.