Skip to content

Commit e772ee1

Browse files
Josverlstinos
authored andcommitted
test/cpydiff: Add tests to document typing differences.
These are constructs used in static typing that are different between MicroPython and CPython, and should be documented as such. Signed-off-by: Jos Verlinde <jos_verlinde@hotmail.com>
1 parent 132c28e commit e772ee1

9 files changed

+119
-24
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
categories: Core,Classes
3+
description: Defining a class with a metaclass is not possible.
4+
cause: Unknown
5+
workaround: Unknown
6+
"""
7+
8+
from abc import ABCMeta
9+
10+
11+
class MyABC(metaclass=ABCMeta):
12+
pass
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
categories: Modules,typing
3+
description: User Defined Generics
4+
cause: Micropython does not implement User Defined Generics
5+
workaround: None
6+
"""
7+
8+
from typing import Dict, Generic, TypeVar
9+
10+
T = TypeVar("T")
11+
12+
13+
class Registry(Generic[T]):
14+
def __init__(self) -> None:
15+
self._store: Dict[str, T] = {}
16+
17+
def set_item(self, k: str, v: T) -> None:
18+
self._store[k] = v
19+
20+
def get_item(self, k: str) -> T:
21+
return self._store[k]
22+
23+
24+
family_name_reg = Registry[str]()
25+
family_age_reg = Registry[int]()
26+
27+
family_name_reg.set_item("husband", "steve")
28+
family_name_reg.set_item("dad", "john")
29+
30+
family_age_reg.set_item("steve", 30)
31+
32+
print(repr(family_name_reg.__dict__))
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
categories: Modules,typing
3+
description: ``get_args()`` function not fully implemented.
4+
cause: Micropython does not implement all typing features
5+
workaround: None
6+
"""
7+
8+
from typing import get_args
9+
10+
# partial implementation of get_args
11+
x = get_args(int)
12+
assert x == (), f"expected () but got {x}"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""
2+
categories: Modules,typing
3+
description: ``get_origin()`` function not fully implemented.
4+
cause: Micropython does not implement all typing features from Python 3.8+
5+
workaround: None
6+
"""
7+
# https://docs.python.org/3/library/typing.html#typing.get_origin
8+
9+
from typing import Dict, get_origin
10+
11+
assert get_origin(Dict[str, int]) is dict, "origin Dict cannot be detected"
12+
13+
assert get_origin(str) is None, "origin str should be None"
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
categories: Modules,typing
3+
description: ``reveal_type()`` is not implemented.
4+
cause: Micropython does not implement all typing features
5+
workaround: None
6+
"""
7+
8+
from typing import Self, reveal_type
9+
10+
11+
class Foo:
12+
def return_self(self) -> Self:
13+
...
14+
15+
16+
class SubclassOfFoo(Foo):
17+
pass
18+
19+
20+
foo = Foo()
21+
sub = SubclassOfFoo()
22+
23+
reveal_type(foo)
24+
reveal_type(sub)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""
2+
categories: Modules,typing
3+
description: ``runtime_checkable()`` is not implemented.
4+
cause: Micropython does not implement all typing features from Python 3.8+
5+
workaround: None
6+
"""
7+
8+
from typing import runtime_checkable, Protocol
9+
10+
11+
@runtime_checkable
12+
class SwallowLaden(Protocol):
13+
def __iter__(self):
14+
...
15+
16+
17+
assert isinstance([1, 2, 3], SwallowLaden)

tests/cpydiff/modules_typing_typeddict.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
"""
22
categories: Modules,typing
3-
description: ``TypedDict`` does not behave according to spec.
3+
description: ``TypedDict`` class not allowed for instance or class checks.
44
cause: Micropython does not implement all typing features
55
workaround: None
66
"""
7-
# Specification: https://typing.readthedocs.io/en/latest/spec/typeddict.html
87

9-
from typing import TypedDict
8+
from typing import TypeVar, TypedDict
109

1110

1211
class Movie(TypedDict):
13-
title: str
12+
name: str
1413
year: int
1514

1615

17-
movie = Movie(title="Life of Brian", year=1979)
18-
print("Type is: ", type(movie))
16+
movie: Movie = {"name": "Blade Runner", "year": 1982}
1917

2018
try:
2119
if isinstance(movie, Movie): # type: ignore

tests/extmod/typing_runtime.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def hash_b(item: Any) -> int:
138138
print(hash_b(42))
139139
print(hash_b("foo"))
140140

141-
print("Testing : typing.AnyString")
141+
print("Testing : typing.AnyStr")
142142

143143
from typing import AnyStr
144144

@@ -151,8 +151,8 @@ def concat(a: AnyStr, b: AnyStr) -> AnyStr:
151151
concat(b"foo", b"bar") # OK, output has type 'bytes'
152152
try:
153153
concat("foo", b"bar") # Error, cannot mix str and bytes
154-
except TypeError as e:
155-
print("OK, expected:", e)
154+
except TypeError:
155+
print("TypeError is expected")
156156

157157

158158
print("Testing : typing.LiteralString")
@@ -201,11 +201,11 @@ def bar(x):
201201
print(bar(42))
202202

203203

204-
print("Testing : typing.")
204+
print("Testing : typing.Required, NotRequired in TypedDict")
205205

206206
# Specification: https://typing.readthedocs.io/en/latest/spec/typeddict.html#required-and-notrequired
207207

208-
from typing import Annotated, NotRequired, Required, TypedDict
208+
from typing import NotRequired, Required, TypedDict
209209

210210

211211
class Movie(TypedDict):
@@ -215,7 +215,6 @@ class Movie(TypedDict):
215215

216216

217217
m = Movie(title="Life of Brian", year=1979)
218-
print(type(m)) # <class 'dict'>
219218

220219

221220
print("Testing : typing.TypeVar")

tests/extmod/typing_syntax.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757

5858
MyAlias = str
5959
Vector: typing.List[float]
60-
Nested = Iterable[Tuple[MyAlias, ...]]
6160
UserId = NewType("UserId", int)
6261
T = TypeVar("T", int, float, complex)
6362

@@ -66,14 +65,3 @@
6665

6766
def func_with_hints(c: int, b: MyAlias, a: Union[int, None], lst: List[float] = [0.0]) -> Any:
6867
pass
69-
70-
71-
class ClassWithHints(Generic[T]):
72-
a: int = 0
73-
74-
def foo(self, other: int) -> None:
75-
self.typed_thing: List[T] = []
76-
77-
78-
class Bar(ClassWithHints[Any]):
79-
pass

0 commit comments

Comments
 (0)