-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_type_dir.py
More file actions
471 lines (343 loc) · 12.1 KB
/
test_type_dir.py
File metadata and controls
471 lines (343 loc) · 12.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
import textwrap
import typing
from typing import Literal, Never, TypeVar, TypedDict, Union, ReadOnly
from typemap.type_eval import eval_typing, _ensure_context
from typemap_extensions import (
Attrs,
FromUnion,
GetArg,
InitField,
IsAssignable,
Iter,
Member,
Members,
NewProtocol,
Uppercase,
)
from . import format_helper
type OrGotcha[K] = K | Literal['gotcha!']
type StrForInt[X] = (str | OrGotcha[X]) if X is int else (X | OrGotcha[X])
class Ordinary:
ordinary: str
class AnotherBase[I]:
iii: OrGotcha[StrForInt[I]]
# This K is dodgy
K = TypeVar("K")
class Base[T]:
t: dict[str, StrForInt[T]]
fin: typing.Final[int]
def foo(self, a: T | None, *, b: int = 0) -> dict[str, T]:
pass
def base[Z](self, a: T | Z | None, b: K) -> dict[str, T | Z]:
pass
@classmethod
def cbase(cls, a: T | None, b: K) -> dict[str, T]:
pass
@staticmethod
def sbase[Z](a: OrGotcha[T] | Z | None, b: K) -> dict[str, T | Z]:
pass
class CMethod:
@classmethod
def cbase2(cls, lol: int, /, a: bool | None) -> int:
pass
class Wrapper[X](Base[X], AnotherBase[X]):
x: "Wrapper[X | None]"
class Mine(Wrapper[int]):
pass
class Last[O]:
last: O | Literal[True]
class Final(Mine, Ordinary, Wrapper[float], AnotherBase[float], Last[int]):
pass
type BaseArg[T] = (
GetArg[T, Base, Literal[0]] if IsAssignable[T, Base] else Never
)
type AllOptional[T] = NewProtocol[
*[Member[p.name, p.type | None, p.quals] for p in Iter[Attrs[T]]]
]
type OptionalFinal = AllOptional[Final]
type Capitalize[T] = NewProtocol[
*[Member[Uppercase[p.name], p.type, p.quals] for p in Iter[Attrs[T]]]
]
type Prims[T] = NewProtocol[
*[p for p in Iter[Attrs[T]] if IsAssignable[p.type, int | str]]
]
type NoLiterals1[T] = NewProtocol[
*[
Member[
p.name,
Union[
*[
t
for t in Iter[FromUnion[p.type]]
# XXX: 'typing.Literal' is not *really* a type...
# Maybe we can't do this, which maybe is fine.
if not IsAssignable[t, Literal]
]
],
p.quals,
]
for p in Iter[Attrs[T]]
]
]
# Try to implement IsLiteral. This is basically what is recommended
# for doing it in TS.
# XXX: This doesn't work in python! We can subtype str!
type IsLiteral[T] = (
Literal[True]
if (
(IsAssignable[T, str] and not IsAssignable[str, T])
or (IsAssignable[T, bytes] and not IsAssignable[bytes, T])
or (IsAssignable[T, bool] and not IsAssignable[bool, T])
or (IsAssignable[T, int] and not IsAssignable[int, T])
# XXX: enum, None
)
else Literal[False]
)
type NoLiterals2[T] = NewProtocol[
*[
Member[
p.name,
Union[
*[
t
for t in Iter[FromUnion[p.type]]
# XXX: 'typing.Literal' is not *really* a type...
# Maybe we can't do this, which maybe is fine.
# if not IsAssignabletype[t, Literal]
if not IsAssignable[IsLiteral[t], Literal[True]]
]
],
p.quals,
]
for p in Iter[Attrs[T]]
]
]
# Subtyping Eval used to do something
class Eval[T]:
pass
class Loop(Eval[int]):
loop: Loop
class Foo(Eval[int]):
bar: Bar
class Bar(Eval[float]):
foo: Foo
def test_type_dir_link_1():
d = eval_typing(Loop)
loop = d.__annotations__["loop"]
assert loop is d
assert loop is Loop
def test_type_dir_link_2():
d = eval_typing(Foo)
loop = d.__annotations__["bar"].__annotations__["foo"]
assert loop is d
assert loop is Foo
def test_type_dir_1a():
d = eval_typing(Final)
assert format_helper.format_class(d) == textwrap.dedent("""\
class Final:
last: int | typing.Literal[True]
iii: str | int | typing.Literal['gotcha!']
t: dict[str, str | int | typing.Literal['gotcha!']]
fin: typing.Final[int]
x: tests.test_type_dir.Wrapper[int | None]
ordinary: str
def foo(self: Self, a: int | None, *, b: int = ...) -> dict[str, int]: ...
def base[Z](self: Self, a: int | Z | None, b: ~K) -> dict[str, int | Z]: ...
@classmethod
def cbase(cls: type[typing.Self], a: int | None, b: ~K) -> dict[str, int]: ...
@staticmethod
def sbase[Z](a: OrGotcha[int] | Z | None, b: ~K) -> dict[str, int | Z]: ...
""")
def test_type_dir_1b():
d = eval_typing(CMethod)
assert format_helper.format_class(d) == textwrap.dedent("""\
class CMethod:
@classmethod
def cbase2(cls: type[typing.Self], lol: int, /, a: bool | None) -> int: ...
""")
def test_type_dir_2():
d = eval_typing(OptionalFinal)
# XXX: `Atrs` skips methods, true to its name. Perhaps we just need
# `Members` that would iterate over everything
assert format_helper.format_class(d) == textwrap.dedent("""\
class AllOptional[tests.test_type_dir.Final]:
last: int | typing.Literal[True] | None
iii: str | int | typing.Literal['gotcha!'] | None
t: dict[str, str | int | typing.Literal['gotcha!']] | None
fin: typing.Final[int | None]
x: tests.test_type_dir.Wrapper[int | None] | None
ordinary: str | None
""")
def test_type_dir_3():
d = eval_typing(Capitalize[Final])
assert format_helper.format_class(d) == textwrap.dedent("""\
class Capitalize[tests.test_type_dir.Final]:
LAST: int | typing.Literal[True]
III: str | int | typing.Literal['gotcha!']
T: dict[str, str | int | typing.Literal['gotcha!']]
FIN: typing.Final[int]
X: tests.test_type_dir.Wrapper[int | None]
ORDINARY: str
""")
def test_type_dir_4():
d = eval_typing(Prims[Final])
assert format_helper.format_class(d) == textwrap.dedent("""\
class Prims[tests.test_type_dir.Final]:
last: int | typing.Literal[True]
fin: typing.Final[int]
ordinary: str
""")
def test_type_dir_5():
global fuck
d = eval_typing(NoLiterals1[Final])
assert format_helper.format_class(d) == textwrap.dedent("""\
class NoLiterals1[tests.test_type_dir.Final]:
last: int
iii: str | int
t: dict[str, str | int | typing.Literal['gotcha!']]
fin: typing.Final[int]
x: tests.test_type_dir.Wrapper[int | None]
ordinary: str
""")
def test_type_dir_6():
d = eval_typing(NoLiterals2[Final])
assert format_helper.format_class(d) == textwrap.dedent("""\
class NoLiterals2[tests.test_type_dir.Final]:
last: int
iii: str | int
t: dict[str, str | int | typing.Literal['gotcha!']]
fin: typing.Final[int]
x: tests.test_type_dir.Wrapper[int | None]
ordinary: str
""")
class Simple[T]:
simple: T
class Funny[T](Simple[list[T]]):
pass
class Funny2(Funny[int]):
pass
def test_type_dir_7():
d = eval_typing(Funny2)
assert format_helper.format_class(d) == textwrap.dedent("""\
class Funny2:
simple: list[int]
""")
def test_type_dir_9():
d = eval_typing(Last[bool])
assert format_helper.format_class(d) == textwrap.dedent("""\
class Last[bool]:
last: bool | typing.Literal[True]
""")
def test_type_dir_10():
class Lurr:
def foo[T](x: T) -> int if IsAssignable[T, str] else list[int]: ...
d = eval_typing(Lurr)
assert format_helper.format_class(d) == textwrap.dedent("""\
class Lurr:
foo: typing.ClassVar[typemap.typing.GenericCallable[tuple[T], <...>]]
""")
member = _get_member(eval_typing(Members[Lurr]), "foo")
fn = member.__args__[1].__args__[1]
with _ensure_context():
assert fn(str).__args__[1] is int
assert fn(bool).__args__[1] == list[int]
def test_type_dir_get_arg_1():
d = eval_typing(BaseArg[Final])
assert d is int
def _get_member(members, name):
return next(
iter(m for m in members.__args__ if m.__args__[0].__args__[0] == name)
)
def test_type_members_attr_1():
d = eval_typing(Members[Final])
member = _get_member(d, "ordinary")
assert typing.get_origin(member) is Member
_, _, _, _, origin = typing.get_args(member)
assert origin.__name__ == "Ordinary"
def test_type_members_attr_2():
d = eval_typing(Members[Final])
member = _get_member(d, "last")
assert typing.get_origin(member) is Member
_, typ, _, _, origin = typing.get_args(member)
assert typ == int | Literal[True]
assert str(origin) == "tests.test_type_dir.Last[int]"
def test_type_members_attr_3():
d = eval_typing(Members[Last[int]])
member = _get_member(d, "last")
assert typing.get_origin(member) is Member
_, typ, _, _, origin = typing.get_args(member)
assert typ == int | Literal[True]
assert str(origin) == "tests.test_type_dir.Last[int]"
def test_type_members_func_1():
d = eval_typing(Members[Final])
member = _get_member(d, "foo")
assert typing.get_origin(member) is Member
name, typ, quals, _, origin = typing.get_args(member)
assert name == typing.Literal["foo"]
assert quals == typing.Literal["ClassVar"]
assert (
str(typ)
== "\
typing.Callable[\
typemap.typing.Params[typemap.typing.Param[typing.Literal['self'], tests.test_type_dir.Base[int], typing.Never], \
typemap.typing.Param[typing.Literal['a'], int | None, typing.Never], \
typemap.typing.Param[typing.Literal['b'], int, typing.Literal['keyword', \
'default']]], \
dict[str, int]]"
)
assert str(origin) == "tests.test_type_dir.Base[int]"
def test_type_members_func_2():
d = eval_typing(Members[Final])
member = _get_member(d, "cbase")
assert typing.get_origin(member) is Member
name, typ, quals, _origin, _ = typing.get_args(member)
assert name == typing.Literal["cbase"]
assert quals == typing.Literal["ClassVar"]
assert (
str(typ)
== "\
classmethod[tests.test_type_dir.Base[int], typemap.typing.Params[typemap.typing.Param[typing.Literal['a'], int | None, typing.Never], typemap.typing.Param[typing.Literal['b'], ~K, typing.Never]], dict[str, int]]"
)
def test_type_members_func_3():
d = eval_typing(Members[Final])
member = _get_member(d, "sbase")
assert typing.get_origin(member) is Member
name, typ, quals, _origin, _ = typing.get_args(member)
assert name == typing.Literal["sbase"]
assert quals == typing.Literal["ClassVar"]
assert str(typ) == "typemap.typing.GenericCallable[tuple[Z], <...>]"
evaled = eval_typing(
typing.get_args(typ)[1](*typing.get_args(typing.get_args(typ)[0]))
)
assert (
str(evaled)
== "staticmethod[typemap.typing.Params[typemap.typing.Param[typing.Literal['a'], int | typing.Literal['gotcha!'] | Z | None, typing.Never], typemap.typing.Param[typing.Literal['b'], ~K, typing.Never]], dict[str, int | Z]]"
)
# Test initializers
class FieldArgs(TypedDict, total=False):
foo: ReadOnly[bool]
bar: ReadOnly[int]
class Field[T: FieldArgs](InitField[T]):
pass
class Inited:
foo: int = 10
bar: bool = Field(foo=False)
def test_type_dir_inits_1():
d = eval_typing(Inited)
assert format_helper.format_class(d) == textwrap.dedent("""\
class Inited:
foo: int = 10
bar: bool = Field(foo=False)
""")
class AnnoyingProjection:
def foo[T: typing.Member](self, a: T) -> T.name:
pass
def bar[T: typing.Member](self, a: T) -> 'T.name':
pass
def test_type_dir_dot_projection_1():
d = eval_typing(AnnoyingProjection)
assert format_helper.format_class(d) == textwrap.dedent("""\
class AnnoyingProjection:
foo: typing.ClassVar[typemap.typing.GenericCallable[tuple[T], <...>]]
bar: typing.ClassVar[typemap.typing.GenericCallable[tuple[T], <...>]]
""")