-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathtest_convert.py
More file actions
143 lines (93 loc) · 3.77 KB
/
test_convert.py
File metadata and controls
143 lines (93 loc) · 3.77 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
from typing import Generic, TypeVar
import pytest
from egglog import *
# TODO: Revert global conversion state after each test w/ fixture
@pytest.mark.skip(reason="We dont support looking up by metaclass anymore")
def test_conversion_custom_metaclass():
class MyMeta(type):
pass
class MyType(metaclass=MyMeta):
pass
class MyTypeExpr(Expr):
def __init__(self) -> None: ...
converter(MyMeta, MyTypeExpr, lambda x: MyTypeExpr())
assert expr_parts(convert(MyType(), MyTypeExpr)) == expr_parts(MyTypeExpr())
def test_conversion():
class MyType:
pass
class MyTypeExpr(Expr):
def __init__(self) -> None: ...
converter(MyType, MyTypeExpr, lambda x: MyTypeExpr())
assert expr_parts(convert(MyType(), MyTypeExpr)) == expr_parts(MyTypeExpr())
def test_conversion_transitive_forward():
class MyType:
pass
class MyTypeExpr(Expr):
def __init__(self) -> None: ...
class MyTypeExpr2(Expr):
def __init__(self) -> None: ...
converter(MyType, MyTypeExpr, lambda x: MyTypeExpr())
converter(MyTypeExpr, MyTypeExpr2, lambda x: MyTypeExpr2())
assert expr_parts(convert(MyType(), MyTypeExpr2)) == expr_parts(MyTypeExpr2())
def test_conversion_transitive_backward():
class MyType:
pass
class MyTypeExpr(Expr):
def __init__(self) -> None: ...
class MyTypeExpr2(Expr):
def __init__(self) -> None: ...
converter(MyTypeExpr, MyTypeExpr2, lambda x: MyTypeExpr2())
converter(MyType, MyTypeExpr, lambda x: MyTypeExpr())
assert expr_parts(convert(MyType(), MyTypeExpr2)) == expr_parts(MyTypeExpr2())
def test_conversion_transitive_cycle():
class MyType:
pass
class MyTypeExpr(Expr):
def __init__(self) -> None: ...
class MyTypeExpr2(Expr):
def __init__(self) -> None: ...
converter(MyType, MyTypeExpr, lambda x: MyTypeExpr())
converter(MyTypeExpr, MyTypeExpr2, lambda x: MyTypeExpr2())
converter(MyTypeExpr2, MyTypeExpr, lambda x: MyTypeExpr())
assert expr_parts(convert(MyType(), MyTypeExpr2)) == expr_parts(MyTypeExpr2())
assert expr_parts(convert(MyType(), MyTypeExpr)) == expr_parts(MyTypeExpr())
T = TypeVar("T", bound=BaseExpr)
def test_convert_to_generic():
"""
Tests that if you have a conversion from a python type to a generic type, it will work for a
particular instance of that generic even if the general instance is registered
"""
class G(BuiltinExpr, Generic[T]):
def __init__(self, x: T) -> None: ...
converter(i64, G[i64], G)
assert expr_parts(convert(10, G[i64])) == expr_parts(G(i64(10)))
with pytest.raises(ConvertError):
convert(10, G[String])
with pytest.raises(ConvertError):
convert("hi", G[i64])
def test_convert_to_unbound_generic():
"""
Tests that if you have a conversion from a python type to a generic type, it will work for a
particular instance of that generic even if the general instance is registered
"""
class G(BuiltinExpr, Generic[T]):
def __init__(self, x: i64) -> None: ...
converter(i64, G, G[get_type_args()[0]]) # type: ignore[misc, operator]
assert expr_parts(convert(10, G[String])) == expr_parts(G[String](i64(10)))
def test_convert_generic_transitive():
"""
If we have A -> B and B[C] -> D then we should
have A -> D conversion
"""
class A(Expr):
def __init__(self) -> None: ...
class B(BuiltinExpr, Generic[T]):
def __init__(
self,
) -> None: ...
class C(Expr): ...
class D(Expr):
def __init__(self) -> None: ...
converter(A, B, lambda _: B[get_type_args()[0]]()) # type: ignore[misc, operator]
converter(B[C], D, lambda _: D())
assert expr_parts(convert(A(), D)) == expr_parts(D())