7
from __future__ import annotations
13
from typing import Dict, List, TypeVar, Optional, Generic, Tuple
17
from typing import Set as _SET_
18
from typing import ClassVar
25
fvar: cython.float = 1.2
26
some_number: cython.int
27
some_list: List[cython.int] = []
28
another_list: list[cython.int] = []
29
t: Tuple[cython.int, ...] = (1, 2, 3)
30
t2: tuple[cython.int, ...]
31
body: Optional[List[str]]
32
descr_only : "descriptions are allowed but ignored"
42
(2, 1.5, [], (1, 2, 3))
46
fvar: cython.float = 1.5
47
some_number: cython.int
48
some_list: List[cython.int] = []
49
t: Tuple[cython.int, ...] = (1, 2, 3)
50
body: Optional[List[str]]
51
descr_only: "descriptions are allowed but ignored"
53
return var, fvar, some_list, t
56
class BasicStarship(object):
58
>>> bs = BasicStarship(5)
65
>>> BasicStarship.stats
68
captain: str = 'Picard'
70
stats: ClassVar[Dict[str, cython.int]] = {}
71
descr_only: "descriptions are allowed but ignored"
73
def __init__(self, damage):
78
class BasicStarshipExt(object):
80
>>> bs = BasicStarshipExt(5)
84
captain: str = 'Picard'
86
stats: ClassVar[Dict[str, cython.int]] = {}
87
descr_only: "descriptions are allowed but ignored"
89
def __init__(self, damage):
93
return self.damage, self.captain, self.stats
123
@cython.test_assert_path_exists(
125
"//WhileStatNode//DictIterationNextNode",
127
def iter_declared_dict(d):
129
>>> d = {1.1: 2.5, 3.3: 4.5}
130
>>> iter_declared_dict(d)
133
# specialized "compiled" test in module-level __doc__
135
typed_dict : Dict[cython.float, cython.float] = d
137
for key in typed_dict:
142
@cython.test_assert_path_exists(
144
"//WhileStatNode//DictIterationNextNode",
146
def iter_declared_dict_arg(d : Dict[cython.float, cython.float]):
148
>>> d = {1.1: 2.5, 3.3: 4.5}
149
>>> iter_declared_dict_arg(d)
152
# module level "compiled" test in __doc__ below
160
def literal_list_ptr():
162
>>> literal_list_ptr()
165
a : cython.p_int = [1, 2, 3, 4, 5]
169
def test_subscripted_types():
171
>>> test_subscripted_types()
179
a1: typing.Dict[cython.int, cython.float] = {}
180
a2: dict[cython.int, cython.float] = {}
181
b1: List[cython.int] = []
182
b2: list[cython.int] = []
184
c: _SET_[object] = set()
186
print(cython.typeof(a1) + (" object" if not cython.compiled else ""))
187
print(cython.typeof(a2) + (" object" if not cython.compiled else ""))
188
print(cython.typeof(b1) + (" object" if not cython.compiled else ""))
189
print(cython.typeof(b2) + (" object" if not cython.compiled else ""))
190
print(cython.typeof(b3) + (" object" if not cython.compiled else ""))
191
print(cython.typeof(c) + (" object" if not cython.compiled else ""))
194
def test_tuple(a: typing.Tuple[cython.int, cython.float], b: typing.Tuple[cython.int, ...],
195
c: Tuple[cython.int, object]
198
>>> test_tuple((1, 1.0), (1, 1.0), (1, 1.0))
209
x: typing.Tuple[int, float] = (a[0], a[1])
210
y: Tuple[cython.int, ...] = (1,2.)
211
plain_tuple: Tuple = ()
215
print(cython.typeof(z))
216
print("int" if cython.compiled and cython.typeof(x[0]) == "Python object" else cython.typeof(x[0]))
218
print(cython.typeof(p))
220
print('float' if cython.typeof(p) == 'float' else cython.typeof(p))
221
print(cython.typeof(x[1]) if cython.compiled or cython.typeof(p) != 'float' else "Python object")
222
print(cython.typeof(a) if cython.compiled or cython.typeof(a) != 'tuple' else "(int, float)")
223
print(cython.typeof(x) + (" object" if not cython.compiled else ""))
224
print(cython.typeof(y) + (" object" if not cython.compiled else ""))
225
print(cython.typeof(c) + (" object" if not cython.compiled else ""))
226
print(cython.typeof(plain_tuple) + (" object" if not cython.compiled else ""))
230
def test_tuple_without_typing(a: tuple[cython.int, cython.float], b: tuple[cython.int, ...],
231
c: tuple[cython.int, object]
234
>>> test_tuple_without_typing((1, 1.0), (1, 1.0), (1, 1.0))
245
x: tuple[int, float] = (a[0], a[1])
246
y: tuple[cython.int, ...] = (1,2.)
247
plain_tuple: tuple = ()
251
print(cython.typeof(z))
252
print("int" if cython.compiled and cython.typeof(x[0]) == "Python object" else cython.typeof(x[0]))
253
print(cython.typeof(p) if cython.compiled or cython.typeof(p) != 'float' else "float")
254
print(cython.typeof(x[1]) if cython.compiled or cython.typeof(p) != 'float' else "Python object")
255
print(cython.typeof(a) if cython.compiled or cython.typeof(a) != 'tuple' else "(int, float)")
256
print(cython.typeof(x) + (" object" if not cython.compiled else ""))
257
print(cython.typeof(y) + (" object" if not cython.compiled else ""))
258
print(cython.typeof(c) + (" object" if not cython.compiled else ""))
259
print(cython.typeof(plain_tuple) + (" object" if not cython.compiled else ""))
262
def test_use_typing_attributes_as_non_annotations():
264
>>> test_use_typing_attributes_as_non_annotations()
265
typing.Tuple typing.Tuple[int]
270
x2 = typing.Tuple[int]
274
y2 = typing.Optional[typing.FrozenSet]
279
allowed_optional_frozenset_strings = [
280
"typing.Union[typing.FrozenSet, NoneType]",
281
"typing.Optional[typing.FrozenSet]"
283
allowed_optional_dict_strings = [
284
"typing.Union[typing.Dict, NoneType]",
285
"typing.Optional[typing.Dict]"
288
print(y1, str(y2) in allowed_optional_frozenset_strings or str(y2))
289
print(z1, str(z2) in allowed_optional_dict_strings or str(z2))
291
def test_optional_ctuple(x: typing.Optional[tuple[float]]):
293
Should not be a C-tuple (because these can't be optional)
294
>>> test_optional_ctuple((1.0,))
297
print(cython.typeof(x) + (" object" if not cython.compiled else ""))
301
import numpy.typing as npt
309
def list_float_to_numpy(z: List[float]) -> List[npt.NDArray[np.float64]]:
311
assert cython.typeof(z) == 'list'
316
# passing non-dicts to variables declared as dict now fails
318
... def __getitem__(self, x): return 2
319
... def __iter__(self): return iter([1, 2, 3])
320
>>> iter_declared_dict(D()) # doctest:+IGNORE_EXCEPTION_DETAIL
321
Traceback (most recent call last):
323
TypeError: Expected dict, got D
324
>>> iter_declared_dict_arg(D()) # doctest:+IGNORE_EXCEPTION_DETAIL
325
Traceback (most recent call last):
327
TypeError: Expected dict, got D