-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtype_inference.py
More file actions
31 lines (20 loc) · 791 Bytes
/
type_inference.py
File metadata and controls
31 lines (20 loc) · 791 Bytes
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
"""
Mypy will infer the first assignment as the variable's type
"""
from typing import List
def name() -> str:
return "Toupie"
s = name() # Mypy decides here that `s` is a str
print(s)
s = 123 # error: Incompatible types in assignment (expression has type "int", variable has type "str")
n = 123
n = name() # error: Incompatible types in assignment (expression has type "str", variable has type "int")
print(n)
# -----------------------------------------------------------------------------
# This may look strange, but is due to type invariance.
# See README.md -- About covariance section.
# See also `collections_covariance_buit_ins.py`
arr = [] # error: Need type annotation for 'arr' (hint: "arr: List[<type>] = ...")
arr = [1, 2, 3]
arr2: List = []
arr2 = [1, 2, 3]