From 1ed771f1cca5f23273e93a509ef003e363810985 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Tue, 6 Sep 2022 11:57:30 -0500 Subject: [PATCH 1/2] Provide stubs for micropython module This allows use of micropython.const to typecheck without making any changes to the .py files themselves. --- setup.py | 6 ++++-- src/micropython-stubs/micropython.pyi | 29 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 src/micropython-stubs/micropython.pyi diff --git a/setup.py b/setup.py index 67ef7ee4..51df7911 100755 --- a/setup.py +++ b/setup.py @@ -52,7 +52,7 @@ python_requires=">=3.7.0", url="https://github.com/adafruit/Adafruit_Blinka", package_dir={"": "src"}, - packages=find_packages("src"), + packages=find_packages("src") + ["micropython-stubs"], # py_modules lists top-level single file packages to include. # find_packages only finds packages in directories with __init__.py files. py_modules=[ @@ -74,8 +74,10 @@ "adafruit_blinka.microcontroller.bcm283x.pulseio": [ "libgpiod_pulsein", "libgpiod_pulsein64", - ] + ], + "micropython-stubs": ["*.pyi"], }, + include_package_data=True, install_requires=[ "Adafruit-PlatformDetect>=3.13.0", "Adafruit-PureIO>=1.1.7", diff --git a/src/micropython-stubs/micropython.pyi b/src/micropython-stubs/micropython.pyi new file mode 100644 index 00000000..7ae9330a --- /dev/null +++ b/src/micropython-stubs/micropython.pyi @@ -0,0 +1,29 @@ +# SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries +# +# SPDX-License-Identifier: MIT +""" +`micropython` - MicroPython Specific Decorator Functions +======================================================== + +* Author(s): cefn +""" + +from typing import Callable, TypeVar, Any, NoReturn + +Fun = TypeVar("Fun", bound=Callable[..., Any]) + +def const(x: int) -> int: + "Emulate making a constant" + return x + +def native(f: Fun) -> Fun: + "Emulate making a native" + return f + +def viper(f: Fun) -> NoReturn: + "User is attempting to use a viper code emitter" + raise SyntaxError("invalid micropython decorator") + +def asm_thumb(f: Fun) -> NoReturn: + "User is attempting to use an inline assembler" + raise SyntaxError("invalid micropython decorator") From 22b67a2080d2aadc52afd8fa71dbbeb2b7fe0d53 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Tue, 6 Sep 2022 18:20:46 -0500 Subject: [PATCH 2/2] remove unnecessary function bodies --- src/micropython-stubs/micropython.pyi | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/micropython-stubs/micropython.pyi b/src/micropython-stubs/micropython.pyi index 7ae9330a..33dd6a98 100644 --- a/src/micropython-stubs/micropython.pyi +++ b/src/micropython-stubs/micropython.pyi @@ -14,16 +14,12 @@ Fun = TypeVar("Fun", bound=Callable[..., Any]) def const(x: int) -> int: "Emulate making a constant" - return x def native(f: Fun) -> Fun: "Emulate making a native" - return f def viper(f: Fun) -> NoReturn: "User is attempting to use a viper code emitter" - raise SyntaxError("invalid micropython decorator") def asm_thumb(f: Fun) -> NoReturn: "User is attempting to use an inline assembler" - raise SyntaxError("invalid micropython decorator")