-
-
Notifications
You must be signed in to change notification settings - Fork 275
/
types.py
399 lines (357 loc) · 12.2 KB
/
types.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
from __future__ import annotations
from abc import ABC, abstractmethod
from enum import Enum, auto
from itertools import chain
from typing import (
TYPE_CHECKING,
Any,
ClassVar,
Dict,
FrozenSet,
Iterable,
Iterator,
List,
Optional,
Sequence,
Set,
Tuple,
Type,
TypeVar,
Union,
)
import pydantic
from packaging import version
from pydantic import StrictBool, StrictInt, StrictStr, create_model
from datamodel_code_generator import Protocol, runtime_checkable
from datamodel_code_generator.format import PythonVersion
from datamodel_code_generator.imports import (
IMPORT_ABC_MAPPING,
IMPORT_ABC_SEQUENCE,
IMPORT_DICT,
IMPORT_LIST,
IMPORT_LITERAL,
IMPORT_LITERAL_BACKPORT,
IMPORT_MAPPING,
IMPORT_OPTIONAL,
IMPORT_SEQUENCE,
IMPORT_UNION,
Import,
)
from datamodel_code_generator.reference import Reference, _BaseModel
T = TypeVar('T')
class StrictTypes(Enum):
str = 'str'
bytes = 'bytes'
int = 'int'
float = 'float'
bool = 'bool'
def chain_as_tuple(*iterables: Iterable[T]) -> Tuple[T, ...]:
return tuple(chain(*iterables))
@runtime_checkable
class Modular(Protocol):
@property
def module_name(self) -> str:
raise NotImplementedError
class DataType(_BaseModel):
class Config:
extra = 'forbid'
copy_on_model_validation = (
False
if version.parse(pydantic.VERSION) < version.parse('1.9.2')
else 'none'
)
type: Optional[str]
reference: Optional[Reference]
data_types: List[DataType] = []
is_func: bool = False
kwargs: Optional[Dict[str, Any]]
import_: Optional[Import] = None
python_version: PythonVersion = PythonVersion.PY_37
is_optional: bool = False
is_dict: bool = False
is_list: bool = False
is_custom_type: bool = False
literals: List[Union[StrictBool, StrictInt, StrictStr]] = []
use_standard_collections: bool = False
use_generic_container: bool = False
use_union_operator: bool = False
alias: Optional[str] = None
parent: Optional[Any] = None
children: List[Any] = []
strict: bool = False
dict_key: Optional[DataType] = None
_exclude_fields: ClassVar[Set[str]] = {'parent', 'children'}
_pass_fields: ClassVar[Set[str]] = {'parent', 'children', 'data_types', 'reference'}
@classmethod
def from_import(
cls: Type[DataTypeT],
import_: Import,
*,
is_optional: bool = False,
is_dict: bool = False,
is_list: bool = False,
is_custom_type: bool = False,
strict: bool = False,
kwargs: Optional[Dict[str, Any]] = None,
) -> 'DataTypeT':
return cls(
type=import_.import_,
import_=import_,
is_optional=is_optional,
is_dict=is_dict,
is_list=is_list,
is_func=True if kwargs else False,
is_custom_type=is_custom_type,
strict=strict,
kwargs=kwargs,
)
@property
def unresolved_types(self) -> FrozenSet[str]:
return frozenset(
{
t.reference.path
for data_types in self.data_types
for t in data_types.all_data_types
if t.reference
}
| ({self.reference.path} if self.reference else set())
)
def replace_reference(self, reference: Reference) -> None:
if not self.reference: # pragma: no cover
raise Exception(
f'`{self.__class__.__name__}.replace_reference()` can\'t be called'
f' when `reference` field is empty.'
)
self.reference.children.remove(self)
self.reference = reference
reference.children.append(self)
@property
def module_name(self) -> Optional[str]:
if self.reference and isinstance(self.reference.source, Modular):
return self.reference.source.module_name
return None # pragma: no cover
@property
def full_name(self) -> str:
module_name = self.module_name
if module_name:
return f'{module_name}.{self.reference.short_name}' # type: ignore
return self.reference.short_name # type: ignore
@property
def all_data_types(self) -> Iterator[DataType]:
for data_type in self.data_types:
yield from data_type.all_data_types
yield self
@property
def all_imports(self) -> Iterator[Import]:
for data_type in self.data_types:
yield from data_type.all_imports
yield from self.imports
@property
def imports(self) -> Iterator[Import]:
if self.import_:
yield self.import_
imports: Tuple[Tuple[bool, Import], ...] = (
(self.is_optional and not self.use_union_operator, IMPORT_OPTIONAL),
(len(self.data_types) > 1 and not self.use_union_operator, IMPORT_UNION),
)
if any(self.literals):
import_literal = (
IMPORT_LITERAL
if self.python_version.has_literal_type
else IMPORT_LITERAL_BACKPORT
)
imports = (
*imports,
(any(self.literals), import_literal),
)
if self.use_generic_container:
if self.use_standard_collections:
imports = (
*imports,
(self.is_list, IMPORT_ABC_SEQUENCE),
(self.is_dict, IMPORT_ABC_MAPPING),
)
else:
imports = (
*imports,
(self.is_list, IMPORT_SEQUENCE),
(self.is_dict, IMPORT_MAPPING),
)
elif not self.use_standard_collections:
imports = (
*imports,
(self.is_list, IMPORT_LIST),
(self.is_dict, IMPORT_DICT),
)
for field, import_ in imports:
if field and import_ != self.import_:
yield import_
if self.dict_key:
yield from self.dict_key.imports
def __init__(self, **values: Any) -> None:
if not TYPE_CHECKING:
super().__init__(**values)
for type_ in self.data_types:
if type_.type == 'Any' and type_.is_optional:
if any(
t for t in self.data_types if t.type != 'Any'
): # pragma: no cover
self.is_optional = True
self.data_types = [
t
for t in self.data_types
if not (t.type == 'Any' and t.is_optional)
]
break
for data_type in self.data_types:
if data_type.reference or data_type.data_types:
data_type.parent = self
if self.reference:
self.reference.children.append(self)
@property
def type_hint(self) -> str:
type_: Optional[str] = self.alias or self.type
if not type_:
if self.is_union:
if self.use_union_operator:
type_ = ' | '.join(
data_type.type_hint for data_type in self.data_types
)
else:
type_ = f"Union[{', '.join(data_type.type_hint for data_type in self.data_types)}]"
elif len(self.data_types) == 1:
type_ = self.data_types[0].type_hint
elif self.literals:
type_ = (
f"Literal[{', '.join(repr(literal) for literal in self.literals)}]"
)
else:
if self.reference:
type_ = self.reference.short_name
else:
# TODO support strict Any
# type_ = 'Any'
type_ = ''
if self.reference and self.python_version == PythonVersion.PY_36:
type_ = f"'{type_}'"
if self.is_list:
if self.use_generic_container:
list_ = 'Sequence'
elif self.use_standard_collections:
list_ = 'list'
else:
list_ = 'List'
type_ = f'{list_}[{type_}]' if type_ else list_
elif self.is_dict:
if self.use_generic_container:
dict_ = 'Mapping'
elif self.use_standard_collections:
dict_ = 'dict'
else:
dict_ = 'Dict'
if self.dict_key or type_:
key = self.dict_key.type_hint if self.dict_key else 'str'
type_ = f'{dict_}[{key}, {type_ or "Any"}]'
else: # pragma: no cover
type_ = dict_
if self.is_optional and type_ != 'Any':
if self.use_union_operator:
type_ = f'{type_} | None'
else:
type_ = f'Optional[{type_}]'
elif self.is_func:
if self.kwargs:
kwargs: str = ', '.join(f'{k}={v}' for k, v in self.kwargs.items())
return f'{type_}({kwargs})'
return f'{type_}()'
return type_
@property
def is_union(self) -> bool:
return len(self.data_types) > 1
DataType.update_forward_refs()
DataTypeT = TypeVar('DataTypeT', bound=DataType)
class Types(Enum):
integer = auto()
int32 = auto()
int64 = auto()
number = auto()
float = auto()
double = auto()
decimal = auto()
time = auto()
string = auto()
byte = auto()
binary = auto()
date = auto()
date_time = auto()
password = auto()
email = auto()
uuid = auto()
uuid1 = auto()
uuid2 = auto()
uuid3 = auto()
uuid4 = auto()
uuid5 = auto()
uri = auto()
hostname = auto()
ipv4 = auto()
ipv4_network = auto()
ipv6 = auto()
ipv6_network = auto()
boolean = auto()
object = auto()
null = auto()
array = auto()
any = auto()
class DataTypeManager(ABC):
def __init__(
self,
python_version: PythonVersion = PythonVersion.PY_37,
use_standard_collections: bool = False,
use_generic_container_types: bool = False,
strict_types: Optional[Sequence[StrictTypes]] = None,
use_non_positive_negative_number_constrained_types: bool = False,
use_union_operator: bool = False,
) -> None:
self.python_version = python_version
self.use_standard_collections: bool = use_standard_collections
self.use_generic_container_types: bool = use_generic_container_types
self.strict_types: Sequence[StrictTypes] = strict_types or ()
self.use_non_positive_negative_number_constrained_types: bool = (
use_non_positive_negative_number_constrained_types
)
self.use_union_operator: bool = use_union_operator
if (
use_generic_container_types and python_version == PythonVersion.PY_36
): # pragma: no cover
raise Exception(
"use_generic_container_types can not be used with target_python_version 3.6.\n"
" The version will be not supported in a future version"
)
if (
use_union_operator and not python_version.has_union_operator
): # pragma: no cover
raise Exception(
"use_union_operator can not be used with target_python_version 3.9 or earlier.\n"
" The version will be not supported in a future version"
)
if TYPE_CHECKING:
self.data_type: Type[DataType]
else:
self.data_type: Type[DataType] = create_model(
'ContextDataType',
python_version=python_version,
use_standard_collections=use_standard_collections,
use_generic_container=use_generic_container_types,
use_union_operator=use_union_operator,
__base__=DataType,
)
@abstractmethod
def get_data_type(self, types: Types, **kwargs: Any) -> DataType:
raise NotImplementedError
def get_data_type_from_full_path(
self, full_path: str, is_custom_type: bool
) -> DataType:
return self.data_type.from_import(
Import.from_full_path(full_path), is_custom_type=is_custom_type
)