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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
a = 3 # type: str
#? str()
a
b = 3 # type: str but I write more
#? int()
b
c = 3 # type: str # I comment more
#? str()
c
d = "It should not read comments from the next line"
# type: int
#? str()
d
# type: int
e = "It should not read comments from the previous line"
#? str()
e
class BB: pass
def test(a, b):
a = a # type: BB
c = a # type: str
d = a
# type: str
e = a # type: str # Should ignore long whitespace
#? BB()
a
#? str()
c
#? BB()
d
#? str()
e
class AA:
class BB:
pass
def test(a):
# type: (AA.BB) -> None
#? AA.BB()
a
def test(a):
# type: (AA.BB,) -> None
#? AA.BB()
a
a,b = 1, 2 # type: str, float
#? str()
a
#? float()
b
class Employee:
pass
from typing import List, Tuple
x = [] # type: List[Employee]
#? Employee()
x[1]
x, y, z = [], [], [] # type: List[int], List[int], List[str]
#? int()
y[2]
x, y, z = [], [], [] # type: (List[float], List[float], List[BB])
for zi in z:
#? BB()
zi
x = [
1,
2,
] # type: List[str]
#? str()
x[1]
for bar in foo(): # type: str
#? str()
bar
for bar, baz in foo(): # type: int, float
#? int()
bar
#? float()
baz
for bar, baz in foo():
# type: str, str
""" type hinting on next line should not work """
#?
bar
#?
baz
with foo(): # type: int
...
with foo() as f: # type: str
#? str()
f
with foo() as f:
# type: str
""" type hinting on next line should not work """
#?
f
aaa = some_extremely_long_function_name_that_doesnt_leave_room_for_hints() \
# type: float # We should be able to put hints on the next line with a \
#? float()
aaa
# Test instance methods
class Dog:
def __init__(self, age, friends, name):
# type: (int, List[Tuple[str, Dog]], str) -> None
#? int()
self.age = age
self.friends = friends
#? Dog()
friends[0][1]
#? str()
self.name = name
def friend_for_name(self, name):
# type: (str) -> Dog
for (friend_name, friend) in self.friends:
if friend_name == name:
return friend
raise ValueError()
def bark(self):
pass
buddy = Dog(UNKNOWN_NAME1, UNKNOWN_NAME2, UNKNOWN_NAME3)
friend = buddy.friend_for_name('buster')
# type of friend is determined by function return type
#! 9 ['def bark']
friend.bark()
friend = buddy.friends[0][1]
# type of friend is determined by function parameter type
#! 9 ['def bark']
friend.bark()
# type is determined by function parameter type following nested generics
#? str()
friend.name
# Mypy comment describing function return type.
def annot():
# type: () -> str
pass
#? str()
annot()
# Mypy variable type annotation.
x = UNKNOWN_NAME2 # type: str
#? str()
x
class Cat(object):
def __init__(self, age, friends, name):
# type: (int, List[Dog], str) -> None
self.age = age
self.friends = friends
self.name = name
cat = Cat(UNKNOWN_NAME4, UNKNOWN_NAME5, UNKNOWN_NAME6)
#? str()
cat.name
# Check potential errors
def x(a, b):
# type: ([) -> a
#?
a
def x(a, b):
# type: (1) -> a
#?
a
def x(a, b, c):
# type: (str) -> a
#?
b
#?
c
|