-
Notifications
You must be signed in to change notification settings - Fork 4
/
mypy_check.py
executable file
·48 lines (33 loc) · 1.57 KB
/
mypy_check.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import mypy.api as api
# you may use mock testing, similar to symbolic execution.
# you may ensure compatibility between non-mock based operation, or ensure all functions are mock calls.
# mock functions shall be lru_cached, to speedup overheads.
result = api.run(...) # commandline args.
import tensorflow as tf
from tensor_annotations import axes # by deepmind.
import tensor_annotations.tensorflow as ttf
from typing import NewType, List
from typing_extensions import Annotated
uint8 = ttf.uint8
Batch, Time = Annotated[axes.Batch, 3], Annotated[axes.Time, 5] # problem is, how to share this along with function calls?
MyAxis = NewType("MyAxis", axes.Axis)
# from pycontract import contract
# @contract
# def my_function(a, b):
# """ Function description.
# :type a: int,>0
# :type b: list[N],N>0
# :rtype: list[N]
# """
# ...
# def sample_batch() -> ttf.Tensor2[uint8, Time, Batch]:
# return tf.zeros((3, 5))
from typing_extensions import Annotated
from annotated_types import Gt, Len, Predicate
class MyClass:
age: Annotated[int, Gt(18)] # Valid: 19, 20, ...
# Invalid: 17, 18, "19", 19.0, ...
# factors: List[Annotated[int, Predicate(is_prime)]] # Valid: 2, 3, 5, 7, 11, ...
# # Invalid: 4, 8, -2, 5.0, "prime", ...
my_list: Annotated[List[int], Len(0, 10)] # Valid: [], [10, 20, 30, 40, 50]
# Invalid: (1, 2), ["abc"], [0] * 20