-
Notifications
You must be signed in to change notification settings - Fork 57
/
test_frontend_type_conversion.py
108 lines (73 loc) · 2.89 KB
/
test_frontend_type_conversion.py
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
from __future__ import annotations
import ast
from collections.abc import Callable
from sys import _getframe # pyright: ignore[reportPrivateUsage]
from typing import Any, Generic, Literal, TypeAlias, TypeVar
import pytest
from xdsl.frontend.dialects.builtin import (
_FrontendType, # pyright: ignore[reportPrivateUsage]
)
from xdsl.frontend.exception import CodeGenerationException
from xdsl.frontend.type_conversion import TypeConverter
from xdsl.ir import ParametrizedAttribute
from xdsl.irdl import irdl_attr_definition
@irdl_attr_definition
class A(ParametrizedAttribute):
name = "a"
class _A(_FrontendType):
@staticmethod
def to_xdsl() -> Callable[[], Any]:
return A.__call__
class _B:
pass
T = TypeVar("T")
class _C(Generic[T]):
pass
@irdl_attr_definition
class D(ParametrizedAttribute):
name = "d"
class _D(Generic[T], _FrontendType):
@staticmethod
def to_xdsl() -> Callable[[], Any]:
return D.__call__
a: TypeAlias = _A
b: TypeAlias = _B
c2: TypeAlias = _C[Literal[2]]
d12: TypeAlias = _D[tuple[Literal[1], Literal[2]]]
globals: dict[str, Any] = _getframe(0).f_globals
def test_raises_exception_on_unknown_type():
type_converter = TypeConverter(globals)
type_hint = ast.Name("unknown", lineno=0, col_offset=0)
with pytest.raises(CodeGenerationException) as err:
type_converter.convert_type_hint(type_hint)
assert err.value.msg == "Unknown type hint 'unknown'."
def test_raises_exception_on_non_frontend_type_I():
type_converter = TypeConverter(globals)
type_hint = ast.Name("b", lineno=0, col_offset=0)
with pytest.raises(CodeGenerationException) as err:
type_converter.convert_type_hint(type_hint)
assert (
err.value.msg == "Unknown type hint for type 'b' inside 'ast.Name' expression."
)
def test_raises_exception_on_non_frontend_type_II():
type_converter = TypeConverter(globals)
type_hint = ast.Name("c2", lineno=0, col_offset=0)
with pytest.raises(CodeGenerationException) as err:
type_converter.convert_type_hint(type_hint)
assert err.value.msg == "'c2' is not a frontend type."
def test_raises_exception_on_nontrivial_generics():
type_converter = TypeConverter(globals)
type_hint = ast.Name("d12", lineno=0, col_offset=0)
with pytest.raises(CodeGenerationException) as err:
type_converter.convert_type_hint(type_hint)
assert (
err.value.msg
== "Expected 1 type argument for generic type 'd12', got 2 type arguments instead."
)
def test_type_conversion_caches_type():
type_converter = TypeConverter(globals)
type_hint = ast.Name("a", lineno=0, col_offset=0)
assert "a" not in type_converter.name_to_xdsl_type_map
xdsl_type = type_converter.convert_type_hint(type_hint)
assert type_converter.name_to_xdsl_type_map["a"] == xdsl_type
assert type_converter.xdsl_to_frontend_type_map[xdsl_type.__class__] == _A