-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathtyping.py
More file actions
97 lines (73 loc) · 2.66 KB
/
typing.py
File metadata and controls
97 lines (73 loc) · 2.66 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
"""
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"]