1+ from typing import List , Dict , Tuple , Any
2+ import numpy as np
3+ import datetime
4+
5+ # A list of floating point numbers
6+ v : List [float ] = [i * 1.23 for i in range (10 )]
7+
8+ # A list of mixed type values
9+ v : List [Any ] = ['apple' , 123 , 'banana' , None ]
10+
11+ # A dictionary of floats indexed by dates
12+ v : Dict [datetime .date , float ] = {
13+ datetime .date .today (): 123.456 ,
14+ datetime .date (2000 , 1 , 1 ): 234.567 ,
15+ }
16+
17+ # A dictionary of lists of strings indexed by tuples of integers
18+ v : Dict [Tuple [int , int ], List [str ]] = {
19+ (2 , 3 ): [
20+ 'apple' ,
21+ 'banana' ,
22+ ],
23+ (4 , 7 ): [
24+ 'orange' ,
25+ 'pineapple' ,
26+ ]
27+ }
28+
29+ # An incorrect type hint
30+ # Your compiler or IDE might complain about this
31+ v : List [str ] = [1 , 2 , 3 ]
32+
33+ # A possibly incorrect type hint
34+ # There is no concensus on whether or not this is correct
35+ v : List [float ] = [1 , None , 3 , None , 5 ]
36+
37+ # This is non-descript but correct
38+ v : List = [(1 ,2 ,'a' ), (4 ,5 ,'b' )]
39+
40+ # This is more descriptive
41+ v : List [Tuple [int , int , str ]] = [(1 ,2 ,'a' ), (4 ,5 ,'b' )]
42+
43+ # Custom types are supported
44+ from typing import NewType
45+ StockTicker = NewType ('StockTicker' , np .float64 )
46+ ticker : StockTicker = 'AAPL'
47+
48+ # Functions can define input and return types
49+ def convert_to_string (value : Any ) -> str :
50+ return str (value )
51+
0 commit comments