|
| 1 | +""" |
| 2 | +This module provides runtime support for type hints. |
| 3 | +based on : |
| 4 | +- https://github.com/micropython/micropython-lib/pull/584 |
| 5 | +- https://github.com/Josverl/micropython-stubs/tree/main/mip |
| 6 | +- https://github.com/Josverl/rt_typing |
| 7 | + |
| 8 | +""" |
| 9 | + |
| 10 | +# ------------------------------------- |
| 11 | +# code reduction by Ignoring type hints |
| 12 | +# ------------------------------------- |
| 13 | +class __Ignore: |
| 14 | + """A class to ignore type hints in code.""" |
| 15 | + |
| 16 | + def __call__(*args, **kwargs): |
| 17 | + # May need some guardrails here |
| 18 | + pass |
| 19 | + |
| 20 | + def __getitem__(self, arg): |
| 21 | + # May need some guardrails here |
| 22 | + return __ignore |
| 23 | + |
| 24 | + def __getattr__(self, attr): |
| 25 | + if attr in self.__dict__: |
| 26 | + return self.__dict__[attr] |
| 27 | + return __ignore |
| 28 | + |
| 29 | + |
| 30 | +__ignore = __Ignore() |
| 31 | +# ----------------- |
| 32 | +# typing essentials |
| 33 | +# ----------------- |
| 34 | +TYPE_CHECKING = False |
| 35 | + |
| 36 | +def final(arg): # ( 6 + bytes) |
| 37 | + # decorator to indicate that a method should not be overridden |
| 38 | + # https://docs.python.org/3/library/typing.html#typing.final |
| 39 | + return arg |
| 40 | + |
| 41 | + |
| 42 | +# def overload(arg): # ( 27 bytes) |
| 43 | +# # ignore functions signatures with @overload decorator |
| 44 | +# return None |
| 45 | +overload = __ignore # saves bytes, and is semantically similar |
| 46 | + |
| 47 | +def NewType(_, arg): # (21 bytes) |
| 48 | + # https://docs.python.org/3/library/typing.html#newtype |
| 49 | + # MicroPython: just use the original type. |
| 50 | + return arg |
| 51 | + |
| 52 | +# --------------- |
| 53 | +# useful methods |
| 54 | +# --------------- |
| 55 | + |
| 56 | +# https://docs.python.org/3/library/typing.html#typing.cast |
| 57 | +# def cast(type, arg): # ( 23 bytes) |
| 58 | +# return arg |
| 59 | +cast = NewType # saves bytes, and is semantically similar |
| 60 | + |
| 61 | +# https://docs.python.org/3/library/typing.html#typing.no_type_check |
| 62 | +# def no_type_check(arg): # ( 26 bytes) |
| 63 | +# # decorator to disable type checking on a function or method |
| 64 | +# return arg |
| 65 | +no_type_check = final # saves bytes, and is semantically similar |
| 66 | + |
| 67 | +# ------------------- |
| 68 | +# less useful methods |
| 69 | +# ------------------- |
| 70 | + |
| 71 | +# def reveal_type(x): # ( 38 bytes) |
| 72 | +# # # https://docs.python.org/3/library/typing.html#typing.reveal_type |
| 73 | +# return x |
| 74 | +# or for smaller size: |
| 75 | +reveal_type = final # saves bytes, and is semantically similar |
| 76 | + |
| 77 | + |
| 78 | +# def get_origin(type): # ( 23 bytes) |
| 79 | +# # https://docs.python.org/3/library/typing.html#typing.get_origin |
| 80 | +# # Return None for all unsupported objects. |
| 81 | +# return None |
| 82 | + |
| 83 | + |
| 84 | +# def get_args(arg): # ( 22 bytes) |
| 85 | +# # https://docs.python.org/3/library/typing.html#typing.get_args |
| 86 | +# # Python 3.8+ only |
| 87 | +# return () |
| 88 | + |
| 89 | + |
| 90 | +# ref: https://github.com/micropython/micropython-lib/pull/584#issuecomment-2317690854 |
| 91 | + |
| 92 | +def __getattr__(x): |
| 93 | + return __ignore |
| 94 | + |
| 95 | +# snarky way to alias typing_extensions to typing ( saving 59 bytes) |
| 96 | +import sys |
| 97 | +sys.modules["typing_extensions"] = sys.modules["typing"] |
0 commit comments