Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typing: Add typing module #584

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions python-stdlib/typing/manifest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
metadata(version="0.1")

module("typing.py")
24 changes: 24 additions & 0 deletions python-stdlib/typing/test_typing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# This doesn't quite test everything but just serves to verify that basic syntax works,
# which for MicroPython means everything typing-related should be ignored.

from typing import *

MyAlias = str
Vector = List[float]
Nested = Iterable[Tuple[int, ...]]
UserId = NewType("UserId", int)
T = TypeVar("T", int, float, complex)

hintedGlobal: Any = None


def func_with_hints(c: int, b: MyAlias, a: Union[int, None], lst: List[float] = [0.0]) -> Any:
pass


class ClassWithHints(Generic[T]):

a: int = 0

def foo(self, other: int) -> None:
pass
154 changes: 154 additions & 0 deletions python-stdlib/typing/typing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
def cast(type, val):
return val


def get_origin(type):
return None


def get_args(type):
return ()


def no_type_check(arg):
return arg


def overload(func):
return None


class _AnyCall:
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


class Final:
pass


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

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
Comment on lines +1 to +154
Copy link
Sponsor Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def cast(type, val):
return val
def get_origin(type):
return None
def get_args(type):
return ()
def no_type_check(arg):
return arg
def overload(func):
return None
class _AnyCall:
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
class Final:
pass
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
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
from micropython import const
TYPE_CHECKING = const(False)
if TYPE_CHECKING:
# Type Checkers on PC will "override" this dynamically to turn on TYPE_CHECKING and include below
def TypeVar(type, *types):
return None
def NewType(name, type):
return type
def cast(type, val):
return val
def get_origin(type):
return None
def get_args(type):
return ()
def no_type_check(arg):
return arg
def overload(func):
return None
class _AnyCall:
def __init__(*args, **kwargs):
pass
def __call__(*args, **kwargs):
pass
_anyCall = _AnyCall()
class _SubscriptableType:
def __getitem__(self, arg):
return _anyCall
_Subscriptable = _SubscriptableType()
class Any:
pass
class BinaryIO:
pass
class ClassVar:
pass
class Final:
pass
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
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
else:
# On-device when compiled to mpy the above should be stripped out leaving just this section.
class _TypeIgnore:
def __init__(*args, **kwargs):
pass
def __call__(self, *args, **kwargs):
return self
def __getitem__(self, attr):
return self
_type_ignore = _TypeIgnore()
def __getattr__(attr):
return _type_ignore

Copy link
Sponsor Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how well it works suggesting this inline, but this is my attempt at providing an on-device simplified runtime stub, but keeping the "full" details for PC based type checking. mpy-cross does strip out the bulk of it, compiled to mpy beforehand was about 1.7KB, with this change it brings it down to 289 bytes for me.