11from functools import partial
22from itertools import chain
3- from typing import AbstractSet , Dict , Iterable , List , Tuple , Type , Union
3+ from typing import AbstractSet , Dict , Iterable , List , Optional , Tuple , Type , Union
44
55from typing_extensions import Literal
66
99
1010
1111class SingleType (BaseType ):
12+ _typing_cls = None
1213 __slots__ = ["_type" , "_hash" ]
1314
1415 def __init__ (self , t : MetaData ):
@@ -40,11 +41,20 @@ def replace(self, t: 'MetaData', **kwargs) -> 'SingleType':
4041 self .type = t
4142 return self
4243
44+ def to_typing_code (self , types_style : Dict [Union ['BaseType' , Type ['BaseType' ]], dict ]) \
45+ -> Tuple [ImportPathList , str ]:
46+ imports , nested = metadata_to_typing (self .type , types_style = types_style )
47+ return (
48+ [* imports , (self ._typing_cls .__module__ , self ._typing_cls ._name )],
49+ f"{ self ._typing_cls ._name } [{ nested } ]"
50+ )
51+
4352 def _to_hash_string (self ) -> str :
4453 return f"{ type (self ).__name__ } /{ get_hash_string (self .type )} "
4554
4655
4756class ComplexType (BaseType ):
57+ _typing_cls = None
4858 __slots__ = ["_types" , "_sorted" , "_hash" ]
4959
5060 def __init__ (self , * types : MetaData ):
@@ -114,8 +124,8 @@ def to_typing_code(self, types_style: Dict[Union['BaseType', Type['BaseType']],
114124 imports , nested = zip (* map (partial (metadata_to_typing , types_style = types_style ), self ))
115125 nested = ", " .join (nested )
116126 return (
117- list ( chain .from_iterable (imports )) ,
118- f"[{ nested } ]"
127+ [ * chain .from_iterable (imports ), ( self . _typing_cls . __module__ , self . _typing_cls . _name )] ,
128+ f"{ self . _typing_cls . _name } [{ nested } ]"
119129 )
120130
121131 def _to_hash_string (self ) -> str :
@@ -126,20 +136,14 @@ class DOptional(SingleType):
126136 """
127137 Field of this type may not be presented in JSON object
128138 """
129-
130- def to_typing_code (self , types_style : Dict [Union ['BaseType' , Type ['BaseType' ]], dict ]) \
131- -> Tuple [ImportPathList , str ]:
132- imports , nested = metadata_to_typing (self .type , types_style = types_style )
133- return (
134- [* imports , ('typing' , 'Optional' )],
135- f"Optional[{ nested } ]"
136- )
139+ _typing_cls = Optional
137140
138141
139142class DUnion (ComplexType ):
140143 """
141144 Same as typing.Union. Nested types are unique.
142145 """
146+ _typing_cls = Union
143147
144148 def __init__ (self , * types : Union [type , BaseType , dict ]):
145149 hashes = set ()
@@ -198,36 +202,18 @@ def _extract_nested_types(self):
198202 else :
199203 yield t
200204
201- def to_typing_code (self , types_style : Dict [Union ['BaseType' , Type ['BaseType' ]], dict ]) \
202- -> Tuple [ImportPathList , str ]:
203- imports , nested = super ().to_typing_code (types_style )
204- return (
205- [* imports , ('typing' , 'Union' )],
206- "Union" + nested
207- )
208-
209205
210206class DTuple (ComplexType ):
211- def to_typing_code (self , types_style : Dict [Union ['BaseType' , Type ['BaseType' ]], dict ]) \
212- -> Tuple [ImportPathList , str ]:
213- imports , nested = super ().to_typing_code (types_style )
214- return (
215- [* imports , ('typing' , 'Tuple' )],
216- "Tuple" + nested
217- )
207+ _typing_cls = Tuple
218208
219209
220210class DList (SingleType ):
221- def to_typing_code (self , types_style : Dict [Union ['BaseType' , Type ['BaseType' ]], dict ]) \
222- -> Tuple [ImportPathList , str ]:
223- imports , nested = metadata_to_typing (self .type , types_style = types_style )
224- return (
225- [* imports , ('typing' , 'List' )],
226- f"List[{ nested } ]"
227- )
211+ _typing_cls = List
228212
229213
230214class DDict (SingleType ):
215+ _typing_cls = Dict
216+
231217 # Dict is single type because keys of JSON dict are always strings.
232218 def to_typing_code (self , types_style : Dict [Union ['BaseType' , Type ['BaseType' ]], dict ]) \
233219 -> Tuple [ImportPathList , str ]:
0 commit comments