Skip to content

Commit ac595b9

Browse files
committed
tests/extmod: Add/enable more typing tests.
Note this is just to enable more printing of FIXMEs to make clear what the typing implementations support, it's not clear yet how the final tests should be structured. Signed-off-by: stijn <[email protected]>
1 parent 1325017 commit ac595b9

File tree

10 files changed

+308
-236
lines changed

10 files changed

+308
-236
lines changed

tests/extmod/collections_abc.py

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

tests/extmod/typing_pep_0484.py

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -75,24 +75,23 @@ def concat(x: AnyStr, y: AnyStr) -> AnyStr:
7575
from typing import Generic, TypeVar
7676

7777
T = TypeVar("T")
78-
# FIXME: Crash - inheriting from typing.Generic[T] unsupported at runtime
79-
# try:
80-
#
81-
# class LoggedVar(Generic[T]):
82-
# pass
83-
#
84-
# def __init__(self, value: T, name: str) -> None:
85-
# self.name = name
86-
# self.value = value
87-
#
88-
# def set(self, new: T) -> None:
89-
# self.value = new
90-
#
91-
# def get(self) -> T:
92-
# return self.value
93-
#
94-
# except Exception as e:
95-
# print("-[ ] FIXME: Difference - Generic[T] base class unsupported:", e)
78+
try:
79+
80+
class LoggedVar(Generic[T]):
81+
def __init__(self, value: T, name: str) -> None:
82+
self.name = name
83+
self.value = value
84+
85+
def set(self, new: T) -> None:
86+
self.value = new
87+
88+
def get(self) -> T:
89+
return self.value
90+
91+
print(LoggedVar('val', 'name').name)
92+
93+
except Exception as e:
94+
print("- [ ] FIXME: Difference - Generic[T] base class unsupported:", e)
9695

9796

9897
# Union/Optional examples
@@ -119,7 +118,7 @@ def use_map(m: dict) -> None:
119118
v = UserId(5)
120119
print("NewType UserId runtime:", v, type(v))
121120
except Exception as e:
122-
print("-[ ] FIXME: Difference or Crash - NewType runtime issue:", e)
121+
print("- [ ] FIXME: Difference or Crash - NewType runtime issue:", e)
123122

124123
print("TYPE_CHECKING guard")
125124

@@ -128,8 +127,7 @@ def use_map(m: dict) -> None:
128127
# TYPE_CHECKING guard
129128
if TYPE_CHECKING:
130129
# This block is for type checkers only
131-
pass
132-
print("typing.TYPE_CHECKING is True at runtime. ERROR")
130+
print("- [ ] FIXME: typing.TYPE_CHECKING is True at runtime. ERROR")
133131
else:
134132
print("typing.TYPE_CHECKING is False at runtime as expected")
135133

@@ -164,13 +162,11 @@ def stop() -> NoReturn:
164162

165163

166164
@overload
167-
def func(x: int) -> int:
168-
...
165+
def func(x: int) -> int: ...
169166

170167

171168
@overload
172-
def func(x: str) -> str:
173-
...
169+
def func(x: str) -> str: ...
174170

175171

176172
def func(x):
@@ -182,6 +178,9 @@ def func(x):
182178
# Cast example: at runtime cast returns the value
183179
from typing import cast
184180

185-
print("cast runtime identity:", cast(str, 123))
181+
if cast(str, 123) == 123:
182+
print("cast runtime works as identity function")
183+
else:
184+
print("- [ ] FIXME: Difference - cast runtime does not work as identity function")
186185

187186
print("-----")

tests/extmod/typing_pep_0526.py

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,6 @@ def hit(self):
104104
Starship.stats = {} # This is OK
105105

106106

107-
# FIXME: - cpy_diff - User Defined Generic Classes unsupported
108-
# from typing import Generic, TypeVar
109-
# T = TypeVar("T")
110-
# class Box(Generic[T]):
111-
# def __init__(self, content):
112-
# self.content: T = content
113-
114-
115107
print("Annotating expressions")
116108

117109

@@ -141,24 +133,17 @@ def f():
141133
x: NonexistentName # No RUNTIME error. # type: ignore
142134

143135

144-
# FIXME: cpy_diff - MicroPython does not raise NameError at runtime
145-
# try:
146-
# x: NonexistentName # Error!
147-
# print("-[ ] FIXME: Expected NameError")
148-
# except NameError:
149-
# print("Expected NameError:")
150-
151-
# try:
152-
153-
# class X:
154-
# var: NonexistentName # Error!
155-
# except NameError:
156-
# print("Expected NameError:")
136+
try:
137+
x: NonexistentName # Error!
138+
print("- [ ] FIXME: cpy_diff - MicroPython does not raise NameError at runtime")
139+
except NameError:
140+
print("Expected NameError")
157141

158142

159-
# FIXME: cpy_diff - MicroPython does not provide the ``__annotations__`` dict at runtime
160-
# print(__annotations__)
161-
# __annotations__["s"] = str
143+
try:
144+
print(__annotations__)
145+
except NameError:
146+
print("- [ ] FIXME: cpy_diff - MicroPython does not provide the ``__annotations__`` dict at runtime")
162147

163148

164149
alice: "well done" = "A+" # type: ignore

tests/extmod/typing_pep_0544.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ class SizedAndClosable_1(Sized, Protocol):
155155
def close(self) -> None:
156156
...
157157
except Exception as e:
158-
print("-[ ] FIXME: Difference or Crash - multiple bases have instance lay-out conflict:", e)
158+
print("- [ ] FIXME: Difference or Crash - multiple bases have instance lay-out conflict:", e)
159159

160160

161161
class SupportsClose_2(Protocol):
@@ -169,7 +169,7 @@ def close(self) -> None:
169169
class SizedAndClosable_2(Sized, SupportsClose_2, Protocol):
170170
pass
171171
except Exception as e:
172-
print("-[ ] FIXME: Difference or Crash - multiple bases have instance lay-out conflict:", e)
172+
print("- [ ] FIXME: Difference or Crash - multiple bases have instance lay-out conflict:", e)
173173

174174
print("Generic protocols")
175175
# https://peps.python.org/pep-0544/#generic-protocols
@@ -203,7 +203,7 @@ def leaves(self) -> List["SimpleTree"]:
203203
root: Traversable = SimpleTree() # OK
204204

205205

206-
# FIXME: CPY_DIFF : Micropython does not support User Defined Generic Classes
206+
# - [ ] FIXME: cpy_diff : Micropython does not support User Defined Generic Classes
207207
# TypeError: 'type' object isn't subscriptable
208208
# class Tree(Generic[T]):
209209
# def leaves(self) -> List["Tree[T]"]: ...
@@ -330,7 +330,7 @@ def fun(cls: Type[Proto]) -> int:
330330
# FIXME: Should Throw: Can't instantiate protocol with abstract methods -
331331
# try:
332332
# fun(Proto) # Error # type: ignore
333-
# print("-[ ] FIXME: Should Throw: Can't instantiate protocol with abstract methods")
333+
# print("- [ ] FIXME: Should Throw: Can't instantiate protocol with abstract methods")
334334
# except Exception:
335335
# print("Expected: Can't instantiate protocol with abstract methods")
336336

@@ -374,7 +374,7 @@ class Id(Protocol):
374374

375375
from typing import TypeVar, Reversible, Iterable, Sized
376376

377-
# FIXME: cpy_diff : User Defined Generic Classes unsupported
377+
# - [ ] FIXME: cpy_diff : User Defined Generic Classes unsupported
378378
# TypeError: 'type' object isn't subscriptable
379379

380380
# T = TypeVar("T")
@@ -387,7 +387,7 @@ class Id(Protocol):
387387

388388
from typing import runtime_checkable, Protocol
389389

390-
# FIXME: cpy_diff : NotImplementedError: @runtime_checkable decorator unsupported
390+
# - [ ] FIXME: cpy_diff : NotImplementedError: @runtime_checkable decorator unsupported
391391
# @runtime_checkable
392392
# class SupportsClose(Protocol):
393393
# def close(self): ...

0 commit comments

Comments
 (0)