Skip to content

Commit 5d69a0b

Browse files
Josverlstinos
authored andcommitted
tests/extmod: Add tests to validate the typing module at runtime.
Signed-off-by: Jos Verlinde <jos_verlinde@hotmail.com>
1 parent 7bde4e6 commit 5d69a0b

File tree

3 files changed

+463
-0
lines changed

3 files changed

+463
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
categories: Modules,typing
3+
description: ``TypedDict`` does not behave according to spec.
4+
cause: Micropython does not implement all typing features
5+
workaround: None
6+
"""
7+
# Specification: https://typing.readthedocs.io/en/latest/spec/typeddict.html
8+
9+
from typing import TypedDict
10+
11+
12+
class Movie(TypedDict):
13+
title: str
14+
year: int
15+
16+
17+
movie = Movie(title="Life of Brian", year=1979)
18+
print("Type is: ", type(movie))
19+
20+
try:
21+
if isinstance(movie, Movie): # type: ignore
22+
pass
23+
print("TypedDict class not allowed for instance or class checks")
24+
25+
except TypeError as e:
26+
print("Handled according to spec")

tests/extmod/collections_abc.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
print("Testing runtime aspects of collections.abc module")
2+
3+
try:
4+
import typing
5+
except ImportError:
6+
print("SKIP")
7+
raise SystemExit
8+
9+
10+
print("Testing : collections.abc.Mapping, Sequence")
11+
12+
# FIXME: from collections.abc import Mapping, Sequence
13+
from typing import Mapping, Sequence
14+
15+
16+
class Employee:
17+
...
18+
19+
20+
def notify_by_email(employees: Sequence[Employee], overrides: Mapping[str, str]) -> None:
21+
pass
22+
23+
24+
notify_by_email([], {})
25+
26+
27+
print("Testing : collections.abc.Callable, Awaitable")
28+
29+
# from collections.abc import Callable, Awaitable
30+
from typing import Callable, Awaitable
31+
32+
33+
def feeder(get_next_item: Callable[[], str]) -> None:
34+
... # Body
35+
36+
37+
def async_query(
38+
on_success: Callable[[int], None], on_error: Callable[[int, Exception], None]
39+
) -> None:
40+
... # Body
41+
42+
43+
async def on_update(value: str) -> None:
44+
... # Body
45+
46+
47+
callback: Callable[[str], Awaitable[None]] = on_update
48+
49+
# ...
50+
51+
52+
def concat(x: str, y: str) -> str:
53+
return x + y
54+
55+
56+
x: Callable[..., str]
57+
x = str # OK
58+
x = concat # Also OK
59+
60+
61+
print("Testing : collections.abc.Iterable")
62+
63+
# FIXME: from collections.abc import Iterable
64+
from typing import Iterable
65+
from typing import Protocol
66+
67+
68+
class Combiner(Protocol):
69+
def __call__(self, *vals: bytes, maxlen: int | None = None) -> list[bytes]:
70+
...
71+
72+
73+
def batch_proc(data: Iterable[bytes], cb_results: Combiner) -> bytes:
74+
for item in data:
75+
pass
76+
return b"".join(cb_results(*data))
77+
78+
79+
def good_cb(*vals: bytes, maxlen: int | None = None) -> list[bytes]:
80+
return [val[:maxlen] for val in vals if maxlen is not None]
81+
82+
83+
batch_proc([], good_cb) # OK
84+
85+
86+
print("Testing : collections.abc.")
87+
print("Testing : collections.abc.")
88+
print("Testing : collections.abc.")

0 commit comments

Comments
 (0)