-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Expand file tree
/
Copy pathcheck-newtype.test
More file actions
387 lines (293 loc) · 10.1 KB
/
check-newtype.test
File metadata and controls
387 lines (293 loc) · 10.1 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
-- Checks NewType(...)
-- Checks for basic functionality
[case testNewTypePEP484Example1]
from typing import NewType
UserId = NewType('UserId', int)
def name_by_id(user_id: UserId) -> str:
return "foo"
UserId('user') # E: Argument 1 to "UserId" has incompatible type "str"; expected "int"
name_by_id(42) # E: Argument 1 to "name_by_id" has incompatible type "int"; expected "UserId"
name_by_id(UserId(42))
id = UserId(5)
num = id + 1
reveal_type(id) # N: Revealed type is "__main__.UserId"
reveal_type(num) # N: Revealed type is "builtins.int"
[targets __main__, __main__.UserId.__init__, __main__.name_by_id]
[case testNewTypePEP484Example2]
from typing import NewType
class PacketId:
def __init__(self, major: int, minor: int) -> None:
self._major = major
self._minor = minor
TcpPacketId = NewType('TcpPacketId', PacketId)
packet = PacketId(100, 100)
tcp_packet = TcpPacketId(packet)
tcp_packet = TcpPacketId(127, 0)
[out]
main:12: error: Too many arguments for "TcpPacketId"
main:12: error: Argument 1 to "TcpPacketId" has incompatible type "int"; expected "PacketId"
[case testNewTypeWithTuples]
from typing import NewType, Tuple
TwoTuple = NewType('TwoTuple', Tuple[int, str])
a = TwoTuple((3, "a"))
b = TwoTuple(("a", 3)) # E: Argument 1 to "TwoTuple" has incompatible type "tuple[str, int]"; expected "tuple[int, str]"
reveal_type(a[0]) # N: Revealed type is "builtins.int"
reveal_type(a[1]) # N: Revealed type is "builtins.str"
[builtins fixtures/tuple.pyi]
[out]
[case testNewTypeWithLists]
from typing import NewType, List
UserId = NewType('UserId', int)
IdList = NewType('IdList', List[UserId])
bad1 = IdList([1]) # E: List item 0 has incompatible type "int"; expected "UserId"
foo = IdList([])
foo.append(3) # E: Argument 1 to "append" of "list" has incompatible type "int"; expected "UserId"
foo.append(UserId(3))
foo.extend([UserId(1), UserId(2), UserId(3)])
foo.extend(IdList([UserId(1), UserId(2), UserId(3)]))
bar = IdList([UserId(2)])
baz = foo + bar
reveal_type(foo) # N: Revealed type is "__main__.IdList"
reveal_type(bar) # N: Revealed type is "__main__.IdList"
reveal_type(baz) # N: Revealed type is "builtins.list[__main__.UserId]"
[builtins fixtures/list.pyi]
[out]
[case testNewTypeWithGenerics]
from typing import TypeVar, Generic, NewType, Any
T = TypeVar('T')
class Base(Generic[T]):
def __init__(self, item: T) -> None:
self.item = item
def getter(self) -> T:
return self.item
Derived1 = NewType('Derived1', Base[str])
Derived2 = NewType('Derived2', Base) # Implicit 'Any'
Derived3 = NewType('Derived3', Base[Any]) # Explicit 'Any'
Derived1(Base(1)) # E: Argument 1 to "Base" has incompatible type "int"; expected "str"
Derived1(Base('a'))
Derived2(Base(1))
Derived2(Base('a'))
Derived3(Base(1))
Derived3(Base('a'))
reveal_type(Derived1(Base('a')).getter()) # N: Revealed type is "builtins.str"
reveal_type(Derived3(Base('a')).getter()) # N: Revealed type is "Any"
[out]
[case testNewTypeWithNamedTuple]
from collections import namedtuple
from typing import NewType, NamedTuple
Vector1 = namedtuple('Vector1', ['x', 'y'])
Point1 = NewType('Point1', Vector1)
p1 = Point1(Vector1(1, 2))
reveal_type(p1.x) # N: Revealed type is "Any"
reveal_type(p1.y) # N: Revealed type is "Any"
Vector2 = NamedTuple('Vector2', [('x', int), ('y', int)])
Point2 = NewType('Point2', Vector2)
p2 = Point2(Vector2(1, 2))
reveal_type(p2.x) # N: Revealed type is "builtins.int"
reveal_type(p2.y) # N: Revealed type is "builtins.int"
class Vector3:
def __init__(self, x: int, y: int) -> None:
self.x = x
self.y = y
Point3 = NewType('Point3', Vector3)
p3 = Point3(Vector3(1, 3))
reveal_type(p3.x) # N: Revealed type is "builtins.int"
reveal_type(p3.y) # N: Revealed type is "builtins.int"
[builtins fixtures/list.pyi]
[out]
[case testNewTypeWithCasts]
from typing import NewType, cast
UserId = NewType('UserId', int)
foo = UserId(3)
foo = cast(UserId, 3)
foo = cast(UserId, "foo")
foo = cast(UserId, UserId(4))
[out]
[case testNewTypeWithTypeAliases]
from typing import NewType
Foo = int
Bar = NewType('Bar', Foo)
Bar2 = Bar
def func1(x: Foo) -> Bar:
return Bar(x)
def func2(x: int) -> Bar:
return Bar(x)
def func3(x: Bar2) -> Bar:
return x
x = Bar(42)
y = Bar2(42)
y = func3(x)
[out]
[case testNewTypeWithNewType]
from typing import NewType
A = NewType('A', int)
B = NewType('B', A)
C = A
D = C
E = NewType('E', D)
a = A(1)
b = B(a)
e = E(a)
def funca(a: A) -> None: ...
def funcb(b: B) -> None: ...
funca(a)
funca(b)
funca(e)
funcb(a) # E: Argument 1 to "funcb" has incompatible type "A"; expected "B"
funcb(b)
funcb(e) # E: Argument 1 to "funcb" has incompatible type "E"; expected "B"
[out]
-- Make sure NewType works as expected in a variety of different scopes/across files
[case testNewTypeInLocalScope]
from typing import NewType
A = NewType('A', int)
a = A(3)
def func() -> None:
A = NewType('A', str)
B = NewType('B', str)
a = A(3) # E: Argument 1 to "A@6" has incompatible type "int"; expected "str"
a = A('xyz')
b = B('xyz')
class MyClass:
C = NewType('C', float)
def foo(self) -> 'MyClass.C':
return MyClass.C(3.2)
b = A(3)
c = MyClass.C(3.5)
[out]
[case testNewTypeInMultipleFiles]
import a
import b
list1 = [a.UserId(1), a.UserId(2)]
list1.append(b.UserId(3)) # E: Argument 1 to "append" of "list" has incompatible type "b.UserId"; expected "a.UserId"
[file a.py]
from typing import NewType
UserId = NewType('UserId', int)
[file b.py]
from typing import NewType
UserId = NewType('UserId', int)
[builtins fixtures/list.pyi]
[out]
[case testNewTypeWithIncremental]
import m
[file m.py]
from typing import NewType
UserId = NewType('UserId', int)
def name_by_id(user_id: UserId) -> str:
return "foo"
name_by_id(UserId(42))
id = UserId(5)
num = id + 1
[file m.py.2]
from typing import NewType
UserId = NewType('UserId', int)
def name_by_id(user_id: UserId) -> str:
return "foo"
name_by_id(UserId(42))
id = UserId(5)
num = id + 1
reveal_type(id)
reveal_type(num)
[rechecked m]
[stale]
[out1]
[out2]
tmp/m.py:13: note: Revealed type is "m.UserId"
tmp/m.py:14: note: Revealed type is "builtins.int"
-- Check misuses of NewType fail
[case testNewTypeBadInitializationFails]
from typing import NewType
a = NewType('b', int) # E: String argument 1 "b" to NewType(...) does not match variable name "a"
b = NewType('b', 3) # E: Argument 2 to NewType(...) must be a valid type
c = NewType(2, int) # E: Argument 1 to NewType(...) must be a string literal
d = NewType(b'f', int) # E: Argument 1 to NewType(...) must be a string literal
foo = "d"
e = NewType(foo, int) # E: Argument 1 to NewType(...) must be a string literal
f = NewType(name='e', tp=int) # E: NewType(...) expects exactly two positional arguments
g = NewType('f', tp=int) # E: NewType(...) expects exactly two positional arguments
[out]
[case testNewTypeWithAnyFails]
from typing import NewType, Any
A = NewType('A', Any) # E: Argument 2 to NewType(...) must be subclassable (got "Any")
[out]
[case testNewTypeWithUnionsFails]
from typing import NewType, Union
Foo = NewType('Foo', Union[int, float]) # E: Argument 2 to NewType(...) must be subclassable (got "int | float")
[out]
[case testNewTypeWithTypeTypeFails]
from typing import NewType, Type
Foo = NewType('Foo', Type[int]) # E: Argument 2 to NewType(...) must be subclassable (got "type[int]")
a = Foo(type(3))
[builtins fixtures/args.pyi]
[out]
[case testNewTypeWithTypeVarsFails]
from typing import NewType, TypeVar, List
T = TypeVar('T')
A = NewType('A', T)
B = NewType('B', List[T])
[builtins fixtures/list.pyi]
[out]
main:4: error: Argument 2 to NewType(...) must be subclassable (got T?)
main:4: error: Type variable "__main__.T" is unbound
main:4: note: (Hint: Use "Generic[T]" or "Protocol[T]" base class to bind "T" inside a class)
main:4: note: (Hint: Use "T" in function signature to bind "T" inside a function)
main:5: error: Type variable "__main__.T" is unbound
main:5: note: (Hint: Use "Generic[T]" or "Protocol[T]" base class to bind "T" inside a class)
main:5: note: (Hint: Use "T" in function signature to bind "T" inside a function)
[case testNewTypeRedefiningVariablesFails]
from typing import NewType
a = 3
def f(): a
a = NewType('a', int) # E: Cannot redefine "a" as a NewType \
# E: Name "a" already defined on line 4
b = NewType('b', int)
def g(): b
b = NewType('b', float) # E: Cannot redefine "b" as a NewType \
# E: Name "b" already defined on line 8
c = NewType('c', str) # type: str # E: Cannot declare the type of a NewType declaration
[case testNewTypeAddingExplicitTypesFails]
from typing import NewType
UserId = NewType('UserId', int)
a = 3 # type: UserId # E: Incompatible types in assignment (expression has type "int", variable has type "UserId")
[out]
[case testNewTypeTestSubclassingFails]
from typing import NewType
class A: pass
B = NewType('B', A)
class C(B): pass # E: Cannot subclass "NewType"
[out]
[case testCannotUseNewTypeWithProtocols]
from typing import Protocol, NewType
class P(Protocol):
attr: int = 0
class D:
attr: int
C = NewType('C', P) # E: NewType cannot be used with protocol classes
x: C = C(D()) # We still accept this, treating 'C' as non-protocol subclass.
reveal_type(x.attr) # N: Revealed type is "builtins.int"
x.bad_attr # E: "C" has no attribute "bad_attr"
C(1) # E: Argument 1 to "C" has incompatible type "int"; expected "P"
[out]
[case testNewTypeAny]
from typing import NewType
Any = NewType('Any', int)
Any(5)
[case testNewTypeWithIsInstanceAndIsSubclass]
from typing import NewType
T = NewType('T', int)
d: object
if isinstance(d, T): # E: Cannot use isinstance() with NewType type
reveal_type(d) # N: Revealed type is "builtins.int"
issubclass(object, T) # E: Cannot use issubclass() with NewType type
[builtins fixtures/isinstancelist.pyi]
[case testInvalidNewTypeCrash]
from typing import List, NewType, Union
N = NewType('N', XXX) # E: Argument 2 to NewType(...) must be subclassable (got "Any") \
# E: Name "XXX" is not defined
x: List[Union[N, int]]
[builtins fixtures/list.pyi]
[case testTypingExtensionsNewType]
from typing_extensions import NewType
N = NewType("N", int)
x: N
[builtins fixtures/tuple.pyi]