Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/python-wasm
Path: blob/main/python/pylang/test/annotations.py
1396 views
1
# Annotations are completely disabled by default, so that it is possible to use mypy.
2
# This turns them back on:
3
from __python__ import annotations
4
5
def add(a: int, b: float):
6
return a + b
7
8
assrt.ok(add.__annotations__)
9
assrt.equal(add.__annotations__['a'], int)
10
assrt.equal(add.__annotations__['b'], float)
11
assrt.equal(add.__annotations__['return'], undefined)
12
13
def sum(ls: list) -> int:
14
pass
15
16
assrt.ok(not (sum.__annotations__ == undefined))
17
assrt.deepEqual(sum.__annotations__, {
18
'ls': list,
19
'return': int
20
})
21
22
def optional(a:int=10):
23
return a
24
25
assrt.ok(not (optional.__annotations__ == undefined))
26
assrt.equal(optional.__annotations__.a, int)
27
assrt.equal(optional.__defaults__.a, 10)
28
29
def otherexpr(a:3+4) -> [1, 2]:
30
pass
31
32
assrt.ok(not (otherexpr.__annotations__ == undefined))
33
assrt.equal(otherexpr.__annotations__['a'], 7)
34
assrt.deepEqual(otherexpr.__annotations__['return'], [1, 2])
35
36
def basic(x:float):
37
pass
38
39
assrt.deepEqual(basic.__annotations__, {
40
'x': float
41
})
42
43
def kwstarargs(*args:list, **kwargs:dict) -> int:
44
pass
45
46
assrt.equal(kwstarargs.__annotations__['return'], int)
47
48
def nothing():
49
pass
50
51
assrt.ok(nothing.__annotations__ == undefined)
52
assrt.throws(def():
53
nothing.__annotations__['return']
54
)
55
56
test = def(x: int):
57
pass
58
59
assrt.deepEqual(test.__annotations__, {
60
'x': int
61
})
62
63
anonreturn = def() -> 'test':
64
pass
65
66
assrt.equal(anonreturn.__annotations__['return'], 'test')
67
68
assrt.equal(def asexpr(a: int):
69
a
70
.__annotations__['a'], int)
71
72
assrt.deepEqual(def(a: int) -> float:
73
a + 10.0
74
.__annotations__, {
75
'a': int,
76
'return': float
77
})
78
79
class A:
80
81
def f(self, a : int, b: 'x') -> float:
82
pass
83
84
assrt.deepEqual(A.prototype.f.__annotations__, {'a':int, 'b':'x', 'return': float})
85
86