@@ -34,6 +34,48 @@ def test_unexpected_value(self):
34
34
35
35
36
36
class Test_DoctypeTransformer :
37
+ @pytest .mark .parametrize (
38
+ "doctype" ,
39
+ [
40
+ "((float))" ,
41
+ "(float,)" ,
42
+ "(, )" ,
43
+ "..." ,
44
+ "(..., ...)" ,
45
+ "{}" ,
46
+ "{:}" ,
47
+ "{a:}" ,
48
+ "{:b}" ,
49
+ "{'a',}" ,
50
+ "a or (b or c)" ,
51
+ ",, optional" ,
52
+ ],
53
+ )
54
+ def test_edge_case_errors (self , doctype ):
55
+ transformer = DoctypeTransformer ()
56
+ with pytest .raises (lark .exceptions .UnexpectedInput ):
57
+ transformer .doctype_to_annotation (doctype )
58
+
59
+ @pytest .mark .parametrize ("doctype" , DoctypeTransformer .blacklisted_qualnames )
60
+ def test_reserved_keywords (self , doctype ):
61
+ assert DoctypeTransformer .blacklisted_qualnames
62
+
63
+ transformer = DoctypeTransformer ()
64
+ with pytest .raises (lark .exceptions .VisitError ):
65
+ transformer .doctype_to_annotation (doctype )
66
+
67
+ @pytest .mark .parametrize (
68
+ ("doctype" , "expected" ),
69
+ [
70
+ ("int or float" , "int | float" ),
71
+ ("int or float or str" , "int | float | str" ),
72
+ ],
73
+ )
74
+ def test_natlang_union (self , doctype , expected ):
75
+ transformer = DoctypeTransformer ()
76
+ annotation , _ = transformer .doctype_to_annotation (doctype )
77
+ assert annotation .value == expected
78
+
37
79
@pytest .mark .parametrize (
38
80
("doctype" , "expected" ),
39
81
[
@@ -67,14 +109,19 @@ def test_subscription(self, doctype, expected):
67
109
("list of int" , "list[int]" ),
68
110
("list of int(s)" , "list[int]" ),
69
111
("list of (int or float)" , "list[int | float]" ),
112
+ ("list of (list of int)" , "list[list[int]]" ),
70
113
# Natural tuple variant
71
114
("tuple of (float, int, str)" , "tuple[float, int, str]" ),
72
115
("tuple of (float, ...)" , "tuple[float, ...]" ),
73
116
# Natural dict variant
74
117
("dict of {str: int}" , "dict[str, int]" ),
75
118
("dict of {str: int | float}" , "dict[str, int | float]" ),
76
119
("dict of {str: int or float}" , "dict[str, int | float]" ),
77
- ("dict[list of str]" , "dict[list[str]]" ),
120
+ # Nesting is possible but probably rarely a good idea
121
+ ("list of (list of int(s))" , "list[list[int]]" ),
122
+ ("tuple of (tuple of (float, ...), ...)" , "tuple[tuple[float, ...], ...]" ),
123
+ ("dict of {str: dict of {str: float}}" , "dict[str, dict[str, float]]" ),
124
+ ("dict of {str: list of (list of int(s))}" , "dict[str, list[list[int]]]" ),
78
125
],
79
126
)
80
127
def test_natlang_container (self , doctype , expected ):
@@ -86,7 +133,7 @@ def test_natlang_container(self, doctype, expected):
86
133
"doctype" ,
87
134
[
88
135
"list of int (s)" ,
89
- "list of (float)" ,
136
+ "list of (( float) )" ,
90
137
"list of (float,)" ,
91
138
"list of (, )" ,
92
139
"list of ..." ,
0 commit comments