Skip to content

Commit

Permalink
test: update qa test
Browse files Browse the repository at this point in the history
Signed-off-by: Jos Verlinde <jos_verlinde@hotmail.com>
  • Loading branch information
Josverl committed Jan 19, 2024
1 parent 2b5b79b commit 99d9eae
Show file tree
Hide file tree
Showing 19 changed files with 242 additions and 91 deletions.
2 changes: 1 addition & 1 deletion publish/micropython-stdlib-stubs/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "micropython-stdlib-stubs"
version = "1.1.1a3"
version = "1.1.2a0"
description = "Micropython stdlib is a reduced and augmented copy of typeshed's stdlib for use by MicroPython stub packages"
authors = ["josverl <josverl@users.noreply.github.com>"]
license = "MIT"
Expand Down
155 changes: 155 additions & 0 deletions publish/micropython-stdlib-stubs/reduce_stdlib.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Remove top level module variables from the stdlib sys module.\n",
"This is based of the types detected on the sys module in a fimrware stub for esp32 micropython\n",
"\n",
"the approach is very simple, we just comment the top level variables from the sys module\n",
"this assumes that all defenitions are on a single line, which may or may not be true\n",
"\n",
"A more mature approach would use cstlib to parse the module and remove the variables\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"from typing import Set\n",
"import re\n",
"from pathlib import Path\n",
"\n",
"RE_VAR_TYPE = r\"[a-zA-Z0-9_]*\\s*: \"\n",
"RE_VAR_ASSIGN = r\"[a-zA-Z0-9_]*\\s*= \"\n",
"\n",
"\n",
"def read_stubfile_toplevel(stubfile: Path) -> Set[str]:\n",
" # read the stubfile into a list of lines\n",
" with open(stubfile, \"r\") as f:\n",
" lines = f.readlines()\n",
" # remove the newlines\n",
" lines = [line.rstrip(\"\\n\") for line in lines]\n",
"\n",
" # # find all lines that start with the regex \"def [a-zA-Z0-9_]*\\(\"\n",
" # deflines = []\n",
" # for line in lines:\n",
" # if re.match(r\"def [a-zA-Z0-9_]*\\s*\\(\", line):\n",
" # deflines.append(line)\n",
"\n",
" # # find all lines that start with the regex \"class [a-zA-Z0-9_]*\\(\"\n",
" # classlines = []\n",
" # for line in lines:\n",
" # if re.match(r\"class [a-zA-Z0-9_]*\\s*\\(\", line):\n",
" # classlines.append(line)\n",
"\n",
" # find all lines that start with the regex \"[a-zA-Z0-9_]*: \"\n",
" typelines = []\n",
" for line in lines:\n",
" if re.match(RE_VAR_TYPE, line):\n",
" typelines.append(line)\n",
"\n",
" # find all lines that start with the regex \"[a-zA-Z0-9_]* = \"\n",
" varlines = []\n",
" for line in lines:\n",
" if re.match(RE_VAR_ASSIGN, line):\n",
" varlines.append(line)\n",
"\n",
" toplevel_vars = set()\n",
" toplevel_vars = toplevel_vars.union([v.split(\":\")[0].strip() for v in typelines])\n",
" toplevel_vars = toplevel_vars.union([v.split(\"=\")[0].strip() for v in varlines])\n",
" return toplevel_vars"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"def patch_stdlib_toplevel(stubfile: Path, toplevel_vars: Set[str]) -> int:\n",
" # read the stubfile into a list of lines\n",
" with open(stubfile, \"r\") as f:\n",
" lines = f.readlines()\n",
" # remove the newlines\n",
" updated = 0\n",
" for i, line in enumerate(lines):\n",
" # comment out the lines that are not toplevel vars\n",
" if re.match(RE_VAR_TYPE, line):\n",
" varname = line.split(\":\")[0].strip()\n",
" if not varname in toplevel_vars and varname[0] != \"_\":\n",
" lines[i] = \"# \" + line\n",
" updated += 1\n",
" elif re.match(RE_VAR_ASSIGN, line):\n",
" varname = line.split(\"=\")[0].strip()\n",
" if not varname in toplevel_vars and varname[0] != \"_\":\n",
" lines[i] = \"# \" + line\n",
" updated += 1\n",
"\n",
" # write the stubfile back\n",
" with open(stubfile, \"w\") as f:\n",
" f.writelines(lines)\n",
"\n",
" return updated"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'stderr', 'argv', 'implementation', 'ps2', 'stdin', 'stdout', 'platform', 'maxsize', 'byteorder', 'ps1', 'modules', 'version', 'version_info', 'path'}\n"
]
},
{
"data": {
"text/plain": [
"27"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"stubfile = \"C:\\\\develop\\\\MyPython\\\\micropython-stubs\\\\publish\\\\micropython-latest-esp32-stubs\\\\sys.pyi\"\n",
"stdlib_stub = Path(\n",
" \"C:\\\\develop\\\\MyPython\\\\micropython-stubs\\\\publish\\\\micropython-stdlib-stubs\\\\stdlib\\\\sys\\\\__init__.pyi\"\n",
")\n",
"toplevel_vars = read_stubfile_toplevel(stubfile)\n",
"print(toplevel_vars)\n",
"\n",
"patch_stdlib_toplevel(stdlib_stub, toplevel_vars)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
54 changes: 27 additions & 27 deletions publish/micropython-stdlib-stubs/stdlib/sys/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -30,36 +30,36 @@ class _MetaPathFinder(Protocol):
if sys.platform != "win32":
abiflags: str
argv: list[str]
base_exec_prefix: str
base_prefix: str
# base_exec_prefix: str
# base_prefix: str
byteorder: Literal["little", "big"]
builtin_module_names: Sequence[str] # actually a tuple of strings
copyright: str
# builtin_module_names: Sequence[str] # actually a tuple of strings
# copyright: str
if sys.platform == "win32":
dllhandle: int
dont_write_bytecode: bool
displayhook: Callable[[object], Any]
excepthook: Callable[[type[BaseException], BaseException, TracebackType | None], Any]
exec_prefix: str
executable: str
float_repr_style: Literal["short", "legacy"]
hexversion: int
last_type: type[BaseException] | None
last_value: BaseException | None
last_traceback: TracebackType | None
# dont_write_bytecode: bool
# displayhook: Callable[[object], Any]
# excepthook: Callable[[type[BaseException], BaseException, TracebackType | None], Any]
# exec_prefix: str
# executable: str
# float_repr_style: Literal["short", "legacy"]
# hexversion: int
# last_type: type[BaseException] | None
# last_value: BaseException | None
# last_traceback: TracebackType | None
maxsize: int
maxunicode: int
meta_path: list[_MetaPathFinder]
# maxunicode: int
# meta_path: list[_MetaPathFinder]
modules: dict[str, ModuleType]
if sys.version_info >= (3, 10):
orig_argv: list[str]
path: list[str]
path_hooks: list[Callable[[str], PathEntryFinder]]
path_importer_cache: dict[str, PathEntryFinder | None]
# path_hooks: list[Callable[[str], PathEntryFinder]]
# path_importer_cache: dict[str, PathEntryFinder | None]
platform: str
if sys.version_info >= (3, 9):
platlibdir: str
prefix: str
# prefix: str
if sys.version_info >= (3, 8):
pycache_prefix: str | None
ps1: object
Expand All @@ -84,10 +84,10 @@ if sys.version_info >= (3, 10):
__stdin__: Final[TextIOWrapper] # Contains the original value of stdin
__stdout__: Final[TextIOWrapper] # Contains the original value of stdout
__stderr__: Final[TextIOWrapper] # Contains the original value of stderr
tracebacklimit: int
# tracebacklimit: int
version: str
api_version: int
warnoptions: Any
# api_version: int
# warnoptions: Any
# Each entry is a tuple of the form (action, message, category, module,
# lineno)
if sys.platform == "win32":
Expand All @@ -98,7 +98,7 @@ _xoptions: dict[Any, Any]
# This can't be represented in the type system, so we just use `structseq[Any]`
_UninstantiableStructseq: TypeAlias = structseq[Any]

flags: _flags
# flags: _flags

if sys.version_info >= (3, 10):
_FlagTuple: TypeAlias = tuple[int, int, int, int, int, int, int, int, int, int, int, int, int, bool, int, int]
Expand Down Expand Up @@ -144,7 +144,7 @@ class _flags(_UninstantiableStructseq, _FlagTuple):
@property
def safe_path(self) -> bool: ...

float_info: _float_info
# float_info: _float_info

@final
class _float_info(structseq[float], tuple[float, int, int, float, int, int, int, int, float, int, int]):
Expand All @@ -171,7 +171,7 @@ class _float_info(structseq[float], tuple[float, int, int, float, int, int, int,
@property
def rounds(self) -> int: ... # FLT_ROUNDS

hash_info: _hash_info
# hash_info: _hash_info

@final
class _hash_info(structseq[Any | int], tuple[int, int, int, int, int, str, int, int, int]):
Expand Down Expand Up @@ -206,7 +206,7 @@ class _implementation:
# > These non-standard attributes must start with an underscore, and are not described here.
def __getattr__(self, name: str) -> Any: ...

int_info: _int_info
# int_info: _int_info

@final
class _int_info(structseq[int], tuple[int, int, int, int]):
Expand All @@ -231,7 +231,7 @@ class _thread_info(_UninstantiableStructseq, tuple[_ThreadInfoName, _ThreadInfoL
@property
def version(self) -> str | None: ...

thread_info: _thread_info
# thread_info: _thread_info
_ReleaseLevel: TypeAlias = Literal["alpha", "beta", "candidate", "final"]

@final
Expand Down
6 changes: 3 additions & 3 deletions tests/quality_tests/check_esp32/check_onewire.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing_extensions import assert_type
import onewire
# OneWire driver
from machine import Pin
import onewire
from typing_extensions import assert_type

ow = onewire.OneWire(Pin(12)) # create a OneWire bus on GPIO12
ow.scan() # return a list of devices on the bus
Expand All @@ -12,7 +12,7 @@
ow.write(b"123") # write bytes on the bus
ow.select_rom(b"12345678") # select a specific device by its ROM code

assert_type(ow, onewire.OneWire)
# assert_type(ow, onewire.OneWire)

# there was no onewire documatation before 1.19.1
# assert_type(ow.write(b"123"), None)
Expand Down
19 changes: 9 additions & 10 deletions tests/quality_tests/check_esp32/check_webrepl.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
#type: ignore
# currently the webrepl module is excluded from creatstubs
# https://github.com/Josverl/micropython-stubber/blob/91394e70a168cfea954f0137faf38d769ae372e8/src/stubber/update_module_list.py#L91
# # https://github.com/Josverl/micropython-stubber/blob/91394e70a168cfea954f0137faf38d769ae372e8/src/stubber/update_module_list.py#L91

import webrepl
# https://docs.micropython.org/en/latest/esp32/quickref.html#webrepl-web-browser-interactive-prompt
#
import webrepl_setup
# import webrepl
# # https://docs.micropython.org/en/latest/esp32/quickref.html#webrepl-web-browser-interactive-prompt
# #
# import webrepl_setup

# and following on-screen instructions. After reboot, it will be available for connection. If you disabled automatic start-up on boot, you may run configured daemon on demand using:
# # and following on-screen instructions. After reboot, it will be available for connection. If you disabled automatic start-up on boot, you may run configured daemon on demand using:


webrepl.start()
# webrepl.start()

# or, start with a specific password
webrepl.start(password="mypass")
# # or, start with a specific password
# webrepl.start(password="mypass")
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def __init__(self, uart):
def _on_rx(self):
# Needed for ESP32.
if hasattr(os, "dupterm_notify"):
os.dupterm_notify(None) # type: ignore
os.dupterm_notify(None) # stubs-ignore: linter=="pyright"

def read(self, sz=None):
return self._uart.read(sz)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import asyncio

import aioespnow # type: ignore
import aioespnow # stubs-ignore: version<"1.22.0"
import network

# A WLAN interface must be active to send()/recv()
Expand Down
2 changes: 1 addition & 1 deletion tests/quality_tests/feat_micropython/check_functions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from socket import * # type: ignore
from socket import *

# socket.socket
# Create STREAM TCP socket
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

# put the device to deep sleep for 10 seconds

machine.reset() # type: ignore
machine.reset()
# detect that reset never returns
assert False, "reset never returns"
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def main(use_stream=False):
# MicroPython socket objects support stream (aka file) interface
# directly, but the line below is needed for CPython.
s = s.makefile("rwb", 0)
s.write(b"GET / HTTP/1.0\r\n\r\n")
print(s.read())
s.write(b"GET / HTTP/1.0\r\n\r\n") # stubs-ignore: linter == "mypy"
print(s.read()) # stubs-ignore: linter == "mypy"
else:
s.send(b"GET / HTTP/1.0\r\n\r\n")
print(s.recv(4096))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ def main(use_stream=True):
else:
# MicroPython SSLSocket objects implement only stream interface, not
# socket interface
s.send(b"GET / HTTP/1.0\r\n\r\n") # type: ignore # not supported by design of MicroPython
print(s.recv(4096)) # type: ignore # not supported by design of MicroPython

s.send(b"GET / HTTP/1.0\r\n\r\n") # stubs-ignore
print(s.recv(4096)) # stubs-ignore

s.close()

Expand Down
Loading

0 comments on commit 99d9eae

Please sign in to comment.