Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add snippet tests using mypy #445

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions snippets/feat_micropython/check_machine/check_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@

from machine import Pin, I2C

i2c = I2C(0)
i2c = I2C(1, scl=Pin(5), sda=Pin(4), freq=400000)
i2c_hw = I2C(0)
i2c_hw = I2C(1, scl=Pin(5), sda=Pin(4), freq=400000)



Expand Down
13 changes: 11 additions & 2 deletions snippets/feat_micropython/check_micropython/check_viper.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import micropython
from typing_extensions import TYPE_CHECKING

# https://docs.micropython.org/en/v1.20.0/reference/speed_python.html?highlight=viper#the-viper-code-emitter
# Casting operators are currently: int, bool, uint, ptr, ptr8, ptr16 and ptr32.
# todo: micropython.viper - add support for casting operators

# todo: micropython.viper - add support for casting operators in the stubs
if TYPE_CHECKING:
from array import array
def ptr(buf: bytes) -> bytearray: ...
def ptr8(buf: bytes) -> bytearray: ...
def ptr16(buf: bytes) -> array: ... #Points to a 16 bit half-word.
def ptr32(buf: bytes) -> array: ... #Points to a 32 bit word.


@micropython.viper
def foo(self, arg: int) -> int:
buf = ptr8(self.linebuf) # self.linebuf is a bytearray or bytes object # type: ignore #
## self.linebuf is a bytearray or bytes object
buf = ptr8(self.linebuf)
for x in range(20, 30):
bar = buf[x] # Access a data item through the pointer
# code omitted
Expand Down
2 changes: 1 addition & 1 deletion snippets/feat_stdlib/check_os/check_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

def listdir(path=".", sub=False, JSON=True, gethash=False):
# Lists the file information of a folder
li :List[dict]= [] # type: List[dict]
li :List[dict]= []
if path == ".": # Get current folder name
path = os.getcwd()
files = os.listdir(path)
Expand Down
2 changes: 1 addition & 1 deletion snippets/feat_stdlib/check_sys/check_implementation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def is_tuple(imp: Tuple):

# indirect check if sys implementation has a base class of a tuple
# there must be a cleaner way to do this but I can't find it
is_tuple(impl)
is_tuple(impl) # stubs-ignore: linter in ["mypy"]


assert_type(impl, NamedTuple) # type: ignore # TODO sys.implementation is not a tuple
Expand Down
2 changes: 1 addition & 1 deletion snippets/feat_stdlib/check_sys/check_print_exception.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

# ref: https://github.com/Josverl/micropython-stubber/issues/336
from sys import print_exception
from sys import print_exception # stubs-ignore: linter in ["mypy"]
2 changes: 1 addition & 1 deletion snippets/feat_stdlib/check_sys/check_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

# Micropython Extensions
exc = Exception
sys.print_exception(exc)
sys.print_exception(exc) # stubs-ignore: linter in ["mypy"]

port = sys.platform
if port in ["unix", "windows"]:
Expand Down
19 changes: 19 additions & 0 deletions snippets/feat_stdlib/mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[mypy]
platform = linux
mypy_path = typings
files = *.py
exclude = .*typings.*.pyi
custom_typing_module = typings/stdlib/typing.pyi
; custom_typeshed_dir = typings/stdlib/_typeshed ; not a valid typeshed dir

follow_imports = silent
follow_imports_for_stubs = True
no_site_packages = True

check_untyped_defs=True

; show_error_context = True
disable_error_code = no-redef

; warn_return_any = True
; warn_unused_configs = True
Loading