11import textwrap
2- from typing import Never , Literal , Union , TypeVar
2+ import typing
3+ from typing import Literal , Never , TypeVar , Union
34
45from typemap .type_eval import eval_typing
56from typemap .typing import (
6- NewProtocol ,
7- Member ,
7+ Attrs ,
8+ FromUnion ,
89 GetArg ,
910 GetName ,
11+ GetQuals ,
1012 GetType ,
13+ Is ,
1114 Iter ,
12- Attrs ,
15+ Member ,
1316 Members ,
14- FromUnion ,
17+ NewProtocol ,
1518 Uppercase ,
16- Is ,
1719)
1820
1921from . import format_helper
2022
21-
2223type OrGotcha [K ] = K | Literal ["gotcha!" ]
2324
2425type StrForInt [X ] = (str | OrGotcha [X ]) if X is int else (X | OrGotcha [X ])
@@ -39,6 +40,8 @@ class Base[T]:
3940 t : dict [str , StrForInt [T ]]
4041 kkk : K
4142
43+ fin : typing .Final [int ]
44+
4245 def foo (self , a : T | None , b : int = 0 ) -> dict [str , T ]:
4346 pass
4447
@@ -75,14 +78,20 @@ class Final(Mine, Ordinary, Wrapper[float], AnotherBase[float], Last[int]):
7578
7679
7780type AllOptional [T ] = NewProtocol [
78- * [Member [GetName [p ], GetType [p ] | None ] for p in Iter [Attrs [T ]]]
81+ * [
82+ Member [GetName [p ], GetType [p ] | None , GetQuals [p ]]
83+ for p in Iter [Attrs [T ]]
84+ ]
7985]
8086
8187type OptionalFinal = AllOptional [Final ]
8288
8389
8490type Capitalize [T ] = NewProtocol [
85- * [Member [Uppercase [GetName [p ]], GetType [p ]] for p in Iter [Attrs [T ]]]
91+ * [
92+ Member [Uppercase [GetName [p ]], GetType [p ], GetQuals [p ]]
93+ for p in Iter [Attrs [T ]]
94+ ]
8695]
8796
8897type Prims [T ] = NewProtocol [
@@ -102,6 +111,7 @@ class Final(Mine, Ordinary, Wrapper[float], AnotherBase[float], Last[int]):
102111 if not Is [t , Literal ]
103112 ]
104113 ],
114+ GetQuals [p ],
105115 ]
106116 for p in Iter [Attrs [T ]]
107117 ]
@@ -137,6 +147,7 @@ class Final(Mine, Ordinary, Wrapper[float], AnotherBase[float], Last[int]):
137147 if not Is [IsLiteral [t ], Literal [True ]]
138148 ]
139149 ],
150+ GetQuals [p ],
140151 ]
141152 for p in Iter [Attrs [T ]]
142153 ]
@@ -152,6 +163,7 @@ class Final:
152163 iii: str | int | typing.Literal['gotcha!']
153164 t: dict[str, str | int | typing.Literal['gotcha!']]
154165 kkk: ~K
166+ fin: typing.Final[int]
155167 x: tests.test_type_dir.Wrapper[int | None]
156168 ordinary: str
157169 def foo(self, a: int | None, b: int = 0) -> dict[str, int]: ...
@@ -172,6 +184,7 @@ class AllOptional[tests.test_type_dir.Final]:
172184 iii: str | int | typing.Literal['gotcha!'] | None
173185 t: dict[str, str | int | typing.Literal['gotcha!']] | None
174186 kkk: ~K | None
187+ fin: typing.Final[int | None]
175188 x: tests.test_type_dir.Wrapper[int | None] | None
176189 ordinary: str | None
177190 """ )
@@ -186,6 +199,7 @@ class Capitalize[tests.test_type_dir.Final]:
186199 III: str | int | typing.Literal['gotcha!']
187200 T: dict[str, str | int | typing.Literal['gotcha!']]
188201 KKK: ~K
202+ FIN: typing.Final[int]
189203 X: tests.test_type_dir.Wrapper[int | None]
190204 ORDINARY: str
191205 """ )
@@ -197,6 +211,7 @@ def test_type_dir_4():
197211 assert format_helper .format_class (d ) == textwrap .dedent ("""\
198212 class Prims[tests.test_type_dir.Final]:
199213 last: int | typing.Literal[True]
214+ fin: typing.Final[int]
200215 ordinary: str
201216 """ )
202217
@@ -211,6 +226,7 @@ class NoLiterals1[tests.test_type_dir.Final]:
211226 iii: str | int
212227 t: dict[str, str | int | typing.Literal['gotcha!']]
213228 kkk: ~K
229+ fin: typing.Final[int]
214230 x: tests.test_type_dir.Wrapper[int | None]
215231 ordinary: str
216232 """ )
@@ -225,6 +241,7 @@ class NoLiterals2[tests.test_type_dir.Final]:
225241 iii: str | int
226242 t: dict[str, str | int | typing.Literal['gotcha!']]
227243 kkk: ~K
244+ fin: typing.Final[int]
228245 x: tests.test_type_dir.Wrapper[int | None]
229246 ordinary: str
230247 """ )
0 commit comments