Skip to content

Commit b878fc6

Browse files
Josverlstinos
authored andcommitted
tests/extmod/typing_runtime: Add tests and improve formatting.
Signed-off-by: Jos Verlinde <[email protected]>
1 parent fbdca67 commit b878fc6

File tree

1 file changed

+138
-30
lines changed

1 file changed

+138
-30
lines changed

tests/extmod/typing_runtime.py

Lines changed: 138 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
print("Testing runtime aspects of typing module")
22

3+
34
try:
45
from typing import TYPE_CHECKING
56
except ImportError:
67
print("SKIP")
78
raise SystemExit
89

10+
print("# Python 3.5+")
11+
print("### Miscellaneous")
12+
913

10-
print("Testing : typing.TYPE_CHECKING - Python 3.5.2")
14+
print("typing.TYPE_CHECKING")
1115
if TYPE_CHECKING:
1216
from typing_extensions import TypeGuard
1317

14-
print("Testing : typing parameter annotations")
15-
from typing import Any, Dict, List
18+
print("typing parameter annotations")
19+
from typing import Any, Dict, List, Union
1620

1721

1822
def add_numbers(a: int, b: int) -> int:
@@ -38,7 +42,7 @@ def process_data(data: Dict[str, Any]) -> None:
3842
process_data({"key": "value", "number": 42})
3943

4044

41-
print("Testing : typing.Self - Python 3.11")
45+
print("typing.Self - Python 3.11")
4246
from typing import Callable, Self
4347

4448

@@ -55,7 +59,7 @@ def cb(x):
5559
base.register(cb)
5660

5761

58-
print("Testing : typing@no_type_check decorator")
62+
print("typing@no_type_check decorator")
5963
from typing import no_type_check
6064

6165

@@ -66,7 +70,7 @@ def quad(r0):
6670

6771
print(quad(1))
6872

69-
print("Testing : typing.Protocol")
73+
print("typing.Protocol")
7074

7175
from typing import Protocol
7276

@@ -93,7 +97,7 @@ def add(adder: Adder) -> None:
9397
add(IntAdder())
9498
add(FloatAdder())
9599

96-
print("Testing : typing.NewType")
100+
print("typing.NewType")
97101

98102
from typing import NewType
99103

@@ -105,7 +109,7 @@ def add(adder: Adder) -> None:
105109
assert isinstance(some_id, int), "NewType should be instance of the original type"
106110

107111

108-
print("Testing : typing.Any")
112+
print("typing.Any")
109113
from typing import Any
110114

111115
a: Any = None
@@ -138,7 +142,7 @@ def hash_b(item: Any) -> int:
138142
print(hash_b(42))
139143
print(hash_b("foo"))
140144

141-
print("Testing : typing.AnyStr")
145+
print("typing.AnyStr")
142146

143147
from typing import AnyStr
144148

@@ -150,12 +154,12 @@ def concat(a: AnyStr, b: AnyStr) -> AnyStr:
150154
concat("foo", "bar") # OK, output has type 'str'
151155
concat(b"foo", b"bar") # OK, output has type 'bytes'
152156
try:
153-
concat("foo", b"bar") # Error, cannot mix str and bytes
157+
concat("foo", b"bar") # Error, cannot mix str and bytes # type: ignore
154158
except TypeError:
155159
print("TypeError is expected")
156160

157161

158-
print("Testing : typing.LiteralString")
162+
print("typing.LiteralString")
159163
from typing import LiteralString
160164

161165

@@ -179,7 +183,7 @@ def caller(arbitrary_string: str, literal_string: LiteralString) -> None:
179183

180184
caller(some_str, literal_str)
181185

182-
print("Testing : typing.overload")
186+
print("typing.overload")
183187

184188
from typing import overload
185189

@@ -201,9 +205,8 @@ def bar(x):
201205
print(bar(42))
202206

203207

204-
print("Testing : typing.Required, NotRequired in TypedDict")
205-
206-
# Specification: https://typing.readthedocs.io/en/latest/spec/typeddict.html#required-and-notrequired
208+
print("typing.Required, NotRequired in TypedDict")
209+
# https://typing.readthedocs.io/en/latest/spec/typeddict.html#required-and-notrequired
207210

208211
from typing import NotRequired, Required, TypedDict
209212

@@ -217,7 +220,7 @@ class Movie(TypedDict):
217220
m = Movie(title="Life of Brian", year=1979)
218221

219222

220-
print("Testing : typing.TypeVar")
223+
print("typing.TypeVar")
221224

222225
from typing import List, TypeVar
223226

@@ -235,7 +238,7 @@ def first(container: List[T]) -> T:
235238
print(first(list_two))
236239

237240

238-
print("Testing : typing.Generator")
241+
print("typing.Generator")
239242

240243
from typing import Generator
241244

@@ -250,7 +253,7 @@ def echo(a: float) -> Generator[int, float, str]:
250253
print(v)
251254

252255

253-
print("Testing : typing.NoReturn")
256+
print("typing.NoReturn")
254257

255258
from typing import NoReturn
256259

@@ -261,7 +264,7 @@ def stop() -> NoReturn:
261264

262265
#
263266

264-
print("Testing : typing.Final")
267+
print("typing.Final")
265268

266269
from typing import Final
267270

@@ -271,7 +274,7 @@ def stop() -> NoReturn:
271274
print(CONST)
272275

273276

274-
print("Testing : typing.final")
277+
print("typing.final")
275278

276279
from typing import final
277280

@@ -299,21 +302,25 @@ class Other(Leaf): # type: ignore # Error reported by type checker
299302
other = Other()
300303

301304

302-
print("Testing : typing.TypeVarTuple and typing.Unpack")
305+
print("typing.TypeVarTuple and typing.Unpack")
303306

304307
from typing import TypeVarTuple, Unpack
305308

306309
Ts = TypeVarTuple("Ts")
307-
tup: tuple[Unpack[Ts]] # Semantically equivalent, and backwards-compatible
310+
tup: tuple[Unpack[Ts]] # Semantically equivalent, and backwards-compatible # type: ignore
308311

309312

310-
print("Testing : typing.Callable, ParamSpec")
313+
print("typing.Callable, ParamSpec")
311314

312315
# ParamSpec, 3.11 notation
313316
# https://docs.python.org/3/library/typing.html#typing.ParamSpec
314317

315-
# FIXME: from collections.abc import Callable
316-
from typing import Callable
318+
try:
319+
from collections.abc import Callable
320+
except ImportError:
321+
print("- [ ] FIXME: from collections.abc import Callable")
322+
323+
from typing import Callable # Workaround for test
317324
from typing import TypeVar, ParamSpec
318325

319326
T = TypeVar("T")
@@ -341,8 +348,109 @@ def add_two(x: float, y: float) -> float:
341348
assert x == 3, "add_two(1, 2) == 3"
342349

343350

344-
print("Testing : typing.")
345-
print("Testing : typing.")
346-
print("Testing : typing.")
347-
print("Testing : typing.")
348-
print("Testing : typing.")
351+
print("typing.get_origin()")
352+
# https://docs.python.org/3/library/typing.html#typing.get_origin
353+
354+
from typing import get_origin
355+
356+
# FIXME: - cpy_diff - get_origin() unsupported, or always returns None
357+
if not get_origin(str) is None:
358+
print("- [ ] FIXME: cpy_diff - get_origin(str) should be None")
359+
# assert get_origin(Dict[str, int]) is dict
360+
# assert get_origin(Union[int, str]) is Union
361+
362+
print("typing.get_args()")
363+
# https://docs.python.org/3/library/typing.html#typing.get_args
364+
from typing import get_args, Dict, Union
365+
366+
# FIXME: - cpy_diff - get_args() unsupported, or always returns ()
367+
if not get_args(int) == ():
368+
print("- [ ] FIXME: cpy_diff - get_args(int) should be ()")
369+
370+
# assert get_args(Dict[int, str]) == (int, str), "get_args(Dict[int, str]) should be (int, str)"
371+
# assert get_args(Union[int, str]) == (int, str), "get_args(Union[int, str]) should be (int, str)"
372+
373+
374+
print("Subscriptables")
375+
376+
from typing import (
377+
AbstractSet,
378+
AsyncContextManager,
379+
AsyncGenerator,
380+
AsyncIterable,
381+
AsyncIterator,
382+
Awaitable,
383+
)
384+
from typing import (
385+
Callable,
386+
ChainMap,
387+
Collection,
388+
Container,
389+
ContextManager,
390+
Coroutine,
391+
Counter,
392+
DefaultDict,
393+
)
394+
from typing import (
395+
Deque,
396+
Dict,
397+
FrozenSet,
398+
Generator,
399+
Generic,
400+
Iterable,
401+
Iterator,
402+
List,
403+
Literal,
404+
Mapping,
405+
)
406+
from typing import (
407+
MutableMapping,
408+
MutableSequence,
409+
MutableSet,
410+
NamedTuple,
411+
Optional,
412+
OrderedDict,
413+
Self,
414+
)
415+
from typing import Sequence, Set, Tuple, Type, Union
416+
417+
418+
t_01: AbstractSet[Any]
419+
t_02: AsyncContextManager[Any]
420+
t_03: AsyncGenerator[Any]
421+
t_04: AsyncIterable[Any]
422+
t_05: AsyncIterator[Any]
423+
t_06: Awaitable[Any]
424+
t_07: Callable[[], Any]
425+
t_08: ChainMap[Any, Any]
426+
t_09: Collection[Any]
427+
t_10: Container[Any]
428+
t_11: ContextManager[Any]
429+
t_12: Coroutine[Any, Any, Any]
430+
t_13: Counter[Any]
431+
t_14: DefaultDict[Any, Any]
432+
t_15: Deque[Any]
433+
t_16: Dict[Any, Any]
434+
t_17: FrozenSet[Any]
435+
t_18: Generator[Any]
436+
# t_19: Generic[Any]
437+
t_20: Iterable[Any]
438+
t_21: Iterator[Any]
439+
t_22: List[Any]
440+
t_23: Literal[1, 2, 3, "a", b"b", True, None]
441+
t_24: Mapping[Any, Any]
442+
t_25: MutableMapping[Any, Any]
443+
t_26: MutableSequence[Any]
444+
t_27: MutableSet[Any]
445+
t_28: NamedTuple
446+
t_29: Optional[Any]
447+
t_30: OrderedDict[Any, Any]
448+
# t_31: Self[Any]
449+
t_32: Sequence[Any]
450+
t_33: Set[Any]
451+
t_34: Tuple[Any]
452+
t_35: Type[Any]
453+
t_36: Union[Any, Any]
454+
455+
456+
print("-----")

0 commit comments

Comments
 (0)