-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #730 from Josverl/stdlib_rename_cpython_stubs
Improve verity of micropython-stdlib-stubs
- Loading branch information
Showing
38 changed files
with
395 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
This is a short description of the steps taken to create the stubs for the micropython-stdlib-stubs distribution. | ||
There may be some steps and details missing as I have not documented the process as I went along. | ||
|
||
1. copy typeshed/stdlib (from pyright fallback) | ||
( I used a different version than listed here, but the process should be the same ) | ||
- ...\.vscode\extensions\ms-python.vscode-pylance-2023.12.1\dist\typeshed-fallback\stdlib | ||
- to ...\micropython-stdlib-stubs\stdlib | ||
|
||
2. Keep only the following **sub folders** / packages | ||
- [ ] `_typeshed` | ||
- [ ] `asyncio` | ||
- [ ] `collections` | ||
- [ ] `os` | ||
|
||
- remove all other folders (28-ish folders) | ||
|
||
3. Rename the stubs that are used internally, but are not part of micropython | ||
with two leading underscores ( __ ) | ||
NOTE: rename them in vscode to that the files that reference them get automagically refactored | ||
- [ ] `codecs.pyi` --> `__codecs.pyi` | ||
- [ ] `contextlib.pyi` --> `__contextlib.pyi` | ||
- [ ] `contextvars.pyi` --> `__contextvars.pyi` | ||
- [ ] `dataclasses.pyi` --> `__dataclasses.pyi` | ||
- [ ] `decimal.pyi` --> `__decimal.pyi` | ||
- [ ] `enum.pyi` --> `__enum.pyi` | ||
- [ ] `fractions.pyi` --> `__fractions.pyi` | ||
- [ ] `functools.pyi` --> `__functools.pyi` | ||
- [ ] `numbers.pyi` --> `__numbers.pyi` | ||
- [ ] `queue.pyi` --> `__queue.pyi` | ||
- [ ] `selectors.pyi` --> `__selectors.pyi` | ||
- [ ] `sre_compile.pyi` --> `__sre_compile.pyi` | ||
- [ ] `sre_constants.pyi` --> `__sre_constants.pyi` | ||
- [ ] `sre_parse.pyi` --> `__sre_parse.pyi` | ||
|
||
4. Remove all stubs and stub-only packages that are not in micropython | ||
Keep only the following: | ||
- [ ] All `__<package>.pyi` from step 3 | ||
- [ ] `__future__.pyi` | ||
- [ ] `_ast.pyi` | ||
- [ ] `_codecs.pyi` | ||
- [ ] `_collections_abc.pyi` | ||
- [ ] `_decimal.pyi` | ||
- [ ] `abc.pyi` | ||
- [ ] `builtins.pyi` | ||
- [ ] `io.pyi` | ||
- [ ] `re.pyi` | ||
- [ ] `socket.pyi` | ||
- [ ] `sys.pyi` | ||
- [ ] `types.pyi` | ||
- [ ] `typing_extensions.pyi` | ||
- [ ] `typing.pyi` | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "micropython-stdlib-stubs" | ||
version = "1.0.0" | ||
version = "1.0.1a1" | ||
description = "Micropython stdlib is a reduced and augmented copy of typeshed's stdlib for use by MicroPython stub packages" | ||
authors = ["josverl <[email protected]>"] | ||
license = "MIT" | ||
|
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
micropython-stubber | ||
|
||
# for quality reporting | ||
pyright | ||
pyright==1.1.339 | ||
pytest | ||
fasteners | ||
python-dotenv | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,6 @@ | ||
{ | ||
"python.analysis.diagnosticMode": "workspace", | ||
"python.analysis.typeCheckingMode": "basic", | ||
"python.testing.pytestArgs": [ | ||
"." | ||
], | ||
"python.testing.unittestEnabled": false, | ||
"python.testing.pytestEnabled": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
PING_MSG = b"ping" | ||
channel = 5 | ||
|
||
check = PING_MSG + b"x" | ||
check = PING_MSG + bytes([channel]) # type: ignore #TODO Operator "+" not supported for types "Literal[b"ping"]" and "bytes" |
12 changes: 12 additions & 0 deletions
12
tests/quality_tests/feat_stdlib_only/check_collections/check_namedtuple.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from collections import namedtuple | ||
|
||
MyTuple = namedtuple("MyTuple", ("id", "name")) | ||
|
||
t1 = MyTuple(1, "foo") # type: ignore # TODO: collections.namedtuple is not callable | ||
t2 = MyTuple(2, "bar") # type: ignore | ||
print(t1.name) | ||
assert t2.name == t2[1] | ||
|
||
print(type(MyTuple)) | ||
print(type(t1)) | ||
print(type(t2)) |
12 changes: 12 additions & 0 deletions
12
tests/quality_tests/feat_stdlib_only/check_collections/check_ordereddict.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from collections import OrderedDict | ||
|
||
# To make benefit of ordered keys, OrderedDict should be initialized | ||
# from sequence of (key, value) pairs. | ||
d = OrderedDict([("z", 1), ("a", 2)]) | ||
# More items can be added as usual | ||
|
||
#TODO add to ordered dict : https://github.com/Josverl/micropython-stubber/issues/333 | ||
d["w"] = 5 # type: ignore | ||
d["b"] = 3 # type: ignore | ||
for k, v in d.items(): | ||
print(k, v) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
"""Validate that a type is hashable. | ||
ref: https://github.com/Josverl/micropython-stubs/issues/723 | ||
""" | ||
|
||
i = 0 | ||
d = {i: "a"} | ||
|
||
type_text = "int" | ||
if type_text in {"int", "float", "str", "bool", "tuple", "list", "dict"}: | ||
order = 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import io | ||
from typing import IO, Any, Optional | ||
|
||
alloc_size = 512 | ||
|
||
|
||
buffer_1 = io.StringIO("hello world") | ||
buffer_2 = io.BytesIO(b"some initial binary data: \x00\x01") # type: ignore # TODO | ||
|
||
stream = open("file") | ||
|
||
buffer_3 = io.BufferedWriter(stream, 8) # type: ignore # TODO stdlib.io "TextIOWrapper" is incompatible with "RawIOBase" | ||
print(buffer_3.write(bytearray(16))) # type: ignore # TODO stdlib.io "bytearray" is incompatible with protocol "ReadableBuffer" | ||
|
||
|
||
stream.close() |
60 changes: 60 additions & 0 deletions
60
tests/quality_tests/feat_stdlib_only/check_os/check_files.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# runs on MicroPython (and Python) | ||
# get file and folder information and return this as JSON | ||
# params : folder , traverse subdirectory , output format, gethash | ||
# intended to allow simple processing of files | ||
# [email protected] | ||
import json | ||
import logging | ||
import os | ||
from typing import Any, Dict, List, Optional, Tuple, Union | ||
|
||
import ubinascii # type: ignore | ||
import uhashlib # type: ignore | ||
|
||
logging.basicConfig(level=logging.DEBUG) | ||
log = logging.getLogger(__name__) | ||
|
||
|
||
def listdir(path=".", sub=False, JSON=True, gethash=False): | ||
# Lists the file information of a folder | ||
li :List[dict]= [] # type: List[dict] | ||
if path == ".": # Get current folder name | ||
path = os.getcwd() | ||
files = os.listdir(path) | ||
for file in files: | ||
# get size of each file | ||
info = {"Path": path, "Name": file, "Size": 0} | ||
if path[-1] == "/": | ||
full = "%s%s" % (path, file) | ||
else: | ||
full = "%s/%s" % (path, file) | ||
log.debug("os.stat({})".format(full)) | ||
subdir = [] | ||
try: | ||
stat = os.stat(full) # type: ignore | ||
if stat[0] & 0x4000: # stat.S_IFDIR | ||
info["Type"] = "dir" | ||
# recurse folder(s) | ||
if sub == True: | ||
log.debug("Folder :{}".format(full)) | ||
subdir = listdir(path=full, sub=True, JSON=False, gethash=gethash) | ||
else: | ||
info["Size"] = stat[6] | ||
info["Type"] = "file" | ||
if gethash: | ||
with open(full, "rb") as f: | ||
h = uhashlib.sha256(f.read()) | ||
info["Hash"] = ubinascii.hexlify(h.digest()) | ||
except OSError as e: | ||
log.error("error:{} processing file:{}".format(e, full)) | ||
info["OSError"] = e.args[0] | ||
info["Type"] = "OSError" | ||
info["Fullname"] = full | ||
li.append(info) | ||
# recurse folder(s) | ||
if sub == True: | ||
li = li + subdir | ||
if JSON == True: | ||
return json.dumps(li) | ||
else: | ||
return li |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from typing_extensions import assert_type | ||
|
||
import re | ||
Substring ='.*Python' | ||
String1 = "MicroPython" | ||
m =re.match(Substring, String1) | ||
|
||
assert m is not None | ||
assert_type(m, re.Match[str]) |
15 changes: 15 additions & 0 deletions
15
tests/quality_tests/feat_stdlib_only/check_re/check_compile.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import re | ||
from typing import Any, List, Union | ||
|
||
from typing_extensions import assert_type | ||
|
||
# As re doesn't support escapes itself, use of r"" strings is not | ||
# recommended. | ||
regex = re.compile("[\r\n]") | ||
|
||
result = regex.split("line1\rline2\nline3\r\n") | ||
|
||
assert_type(result, List[Union[str, Any]]) | ||
|
||
# Result: | ||
# ['line1', 'line2', 'line3', '', ''] |
Oops, something went wrong.