From c468ab77793c856024a966cf5230d4da019bd7a6 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Tue, 6 Sep 2022 10:45:23 -0500 Subject: [PATCH] Add typing information to micropython This requires converting it into a package so that it can have a "py.typed" file. After this, a package using micropython.const can be type checked --- setup.py | 3 ++- src/{micropython.py => micropython/__init__.py} | 12 ++++++++---- src/micropython/py.typed | 0 3 files changed, 10 insertions(+), 5 deletions(-) rename src/{micropython.py => micropython/__init__.py} (73%) create mode 100644 src/micropython/py.typed diff --git a/setup.py b/setup.py index 67ef7ee..4673591 100755 --- a/setup.py +++ b/setup.py @@ -74,7 +74,8 @@ setup( "adafruit_blinka.microcontroller.bcm283x.pulseio": [ "libgpiod_pulsein", "libgpiod_pulsein64", - ] + ], + "micropython": ["py.typed"], }, install_requires=[ "Adafruit-PlatformDetect>=3.13.0", diff --git a/src/micropython.py b/src/micropython/__init__.py similarity index 73% rename from src/micropython.py rename to src/micropython/__init__.py index 4f88543..5f6d73f 100755 --- a/src/micropython.py +++ b/src/micropython/__init__.py @@ -8,22 +8,26 @@ * Author(s): cefn """ +from typing import Callable, TypeVar, Any -def const(x): +Fun = TypeVar("Fun", bound=Callable[..., Any]) + + +def const(x: int) -> int: "Emulate making a constant" return x -def native(f): +def native(f: Fun) -> Fun: "Emulate making a native" return f -def viper(f): +def viper(f: Fun) -> None: "User is attempting to use a viper code emitter" raise SyntaxError("invalid micropython decorator") -def asm_thumb(f): +def asm_thumb(f: Fun) -> None: "User is attempting to use an inline assembler" raise SyntaxError("invalid micropython decorator") diff --git a/src/micropython/py.typed b/src/micropython/py.typed new file mode 100644 index 0000000..e69de29 -- 2.49.0