-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathtyping.py
More file actions
111 lines (87 loc) · 3.02 KB
/
typing.py
File metadata and controls
111 lines (87 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
"""
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__(*keys, **values):
# May need some guardrails here
pass
def __getitem__(self, key):
# May need some guardrails here
return __ignore
def __getattr__(self, key):
return self.__dict__.get(key, __ignore)
__ignore = __Ignore()
# -----------------
# typing essentials
# -----------------
TYPE_CHECKING = False
def reveal_type(value):
return value
override = final = reveal_type # saves bytes, and is semantically similar
# def overload(arg): # ( 27 bytes)
# # ignore functions signatures with @overload decorator
# return None
overload = __ignore # saves bytes, and is semantically similar
def NewType(_, value): # (21 bytes)
# https://docs.python.org/3/library/typing.html#newtype
# MicroPython: just use the original type.
return value
def TypeVar(key, *types, bound = None, covariant=False, contravariant=False, infer_variance=False):
return key
# ---------------
# useful methods
# ---------------
# is semantically similar
TypeVarTuple = final
# 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 used 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
# The get_origin behaviour is # already implemented by the __getattr__
# method of __Ignore, which passes the current test suite.
# 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(_): # ( 22 bytes)
# https://docs.python.org/3/library/typing.html#typing.get_args
# Python 3.8+ only
return ()
# https://typing.python.org/en/latest/spec/typeddict.html
# make TypedDict dict-like at runtime.
TypedDict = dict
class IO:
pass
class TextIO:
pass
class BinaryIO:
pass
AnyStr=str
# ref: https://github.com/micropython/micropython-lib/pull/584#issuecomment-2317690854
def __getattr__(key):
return __ignore
# snarky way to alias typing_extensions to typing ( saving 59 bytes)
import sys
sys.modules["typing_extensions"] = sys.modules["typing"]
del sys