This repository has been archived by the owner on Sep 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
objects.py
480 lines (304 loc) · 7.29 KB
/
objects.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
from __future__ import annotations
import collections
import dataclasses
import datetime
import enum
import numbers
import typing
try:
from typing_extensions import TypedDict
except ImportError:
from typing import TypedDict # type: ignore
import inflection
import typic
# import pandas
import pydantic
import sqlalchemy
from sqlalchemy.ext.declarative import declarative_base
from typic.compat import Literal
def get_id(x) -> str:
return inflection.underscore(str(x))
@dataclasses.dataclass
class FromDict:
foo: typing.Optional[str] = None
@classmethod
def from_dict(cls, dikt: typing.Mapping):
return cls(**dikt)
def to_dict(self) -> dict:
return dataclasses.asdict(self)
@dataclasses.dataclass
class Data:
foo: str
@dataclasses.dataclass
class Nested:
data: Data
@dataclasses.dataclass
class NestedSeq:
datum: typing.List[Data]
@dataclasses.dataclass
class NestedFromDict:
data: Data
@classmethod
def from_dict(cls, dikt: typing.Mapping):
data = Data(**dikt["data"])
return cls(data)
@dataclasses.dataclass
class DefaultNone:
none: typing.Optional[str] = None
@dataclasses.dataclass
class DefaultEllipsis:
ellipsis: str = ...
@typic.klass
class Forward:
foo: "FooNum"
class FooNum(str, enum.Enum):
bar = "bar"
@dataclasses.dataclass
class NestedDoubleReference:
first: Data
second: Data = ...
@typic.klass
class A:
b: typing.Optional[B] = None
@typic.klass
class B:
a: typing.Optional[A] = None
@typic.klass
class ABs:
a: typing.Optional[A] = None
bs: typing.Optional[typing.Iterable[B]] = None
@typic.klass
class C:
c: typing.Optional[C] = None
@dataclasses.dataclass
class D:
d: typing.Optional[D] = None
@dataclasses.dataclass
class E:
d: typing.Optional[D] = None
f: typing.Optional[F] = None
@dataclasses.dataclass
class F:
g: "G"
@typic.klass
class G:
h: typing.Optional[int] = None
@typic.klass
class H:
hs: typing.Iterable[H]
@dataclasses.dataclass
class J:
js: typing.Iterable[J]
@dataclasses.dataclass
class ThreeOptionals:
a: typing.Optional[str]
b: typing.Optional[str] = None
c: typing.Optional[str] = None
class Class:
var: str
def __init__(self, var: str):
self.var = var
class NoParams:
var: str
@dataclasses.dataclass(frozen=True)
class Frozen:
var: bool
@typic.klass
class Typic:
var: str
@typic.klass
class SubTypic(Typic):
sub: str
@typic.klass
class Base:
def __init_subclass__(cls, **kwargs):
super().__init_subclass__(**kwargs)
@typic.klass
class SuperBase(Base):
super: str
def __setattr__(self, key, value):
super().__setattr__(key, value)
class MetaSlotsClass(type):
def __new__(mcs, name, bases, namespace):
cls = super().__new__(mcs, name, bases, namespace)
...
cls = typic.klass(cls, slots=True)
...
return cls
@typic.klass(frozen=True)
class FrozenTypic:
var: str
class Inherited(Typic):
pass
@typic.klass
class KlassVarSubscripted:
var: typing.ClassVar[str] = "foo"
def func(bar: int):
return bar
def optional(bar: str = None):
return bar
def varargs(*args: Data, **kwargs: Data):
return args + tuple(kwargs.values())
@typic.al(strict=True)
def strictvaradd(*args: int, **kwargs: int):
return sum(args) + sum(kwargs.values())
class Method:
def math(self, a: int) -> int:
return a * a
@typic.klass(delay=True, always=False)
class KlassDelayed:
foo: str
@typic.al(delay=True, always=False)
@dataclasses.dataclass
class Delayed:
foo: str
@typic.al(delay=True)
def delayed(foo: str) -> str:
return foo
UserID = typing.NewType("UserID", int)
DateDict = typing.NewType("DateDict", typing.Dict[datetime.datetime, str])
@typic.constrained(max_length=5)
class ShortStr(str): ...
@typic.constrained(values=ShortStr)
class ShortStrList(list): ...
@typic.constrained(gt=1000)
class LargeInt(int): ...
@typic.constrained(gt=1000)
class LargeFloat(float): ...
@typic.constrained(values=LargeInt, keys=ShortStr)
class LargeIntDict(dict): ...
@typic.constrained(keys=ShortStr)
class ShortKeyDict(dict): ...
@typic.constrained(items={"foo": LargeInt}, values=ShortStr)
class ItemizedValuedDict(dict): ...
@typic.constrained(items={"foo": LargeInt}, keys=ShortStr)
class ItemizedKeyedDict(dict): ...
@typic.constrained(items={"foo": LargeInt})
class ItemizedDict(dict): ...
@typic.constrained(items={"foo": LargeInt}, keys=ShortStr, values=ShortStr)
class ItemizedKeyedValuedDict(dict): ...
ShortStrDictT = typing.Dict[ShortStr, ShortStr]
@typic.klass
class Constrained:
short: ShortStr
large: LargeInt
@typic.klass
class NestedConstrained:
mapping: typing.Mapping[str, Constrained]
array: typing.List[Constrained]
constr: LargeIntDict
other_constr: ShortStrDictT
@typic.klass
class TClass:
a: int
class TDict(TypedDict):
a: int
class TDictPartial(TypedDict, total=False):
a: int
class NTup(typing.NamedTuple):
a: int
ntup = collections.namedtuple("ntup", ["a"])
Base = declarative_base()
class Alchemy(Base):
__tablename__ = "alchemy"
id = sqlalchemy.Column(sqlalchemy.BigInteger, primary_key=True, autoincrement=True)
bar = sqlalchemy.Column(sqlalchemy.Text, nullable=False)
class Pydantic(pydantic.BaseModel):
bar: str
id: typing.Optional[int] = None
@typic.klass
class Typical:
bar: str
id: typing.Optional[typic.ReadOnly[int]] = None
@typic.klass
class Source:
test: typing.Optional[str] = typic.field(init=False)
field_to_ignore: str = "Ignore me"
def __post_init__(self):
self.test = "Something"
@typic.klass
class Dest:
test: typing.Optional[str] = None
@typic.klass
class ABlah:
key: Literal[3]
field: "typing.Union[AFoo, ABar, ABlah, None]"
@typic.klass
class AFoo:
key: Literal[1]
field: str
@typic.klass
class ABar:
key: Literal[2]
field: bytes
@typic.klass
class CBlah:
key: typing.ClassVar[int] = 3
field: typing.Union[CFoo, CBar, CBlah, None]
@typic.klass
class CFoo:
key: typing.ClassVar[int] = 1
field: str
@typic.klass
class CBar:
key: typing.ClassVar[int] = 2
field: bytes
@dataclasses.dataclass
class DBlah:
key: typing.ClassVar[int] = 3
field: "typing.Union[DFoo, DBar, DBlah, None]"
@dataclasses.dataclass
class DFoo:
key: typing.ClassVar[int] = 1
field: str
@dataclasses.dataclass
class DBar:
key: typing.ClassVar[int] = 2
field: bytes
@typic.klass
class MutableClassVar:
f: typing.ClassVar[typing.List[str]] = []
@dataclasses.dataclass
class Pep585:
data: dict[str, int]
@dataclasses.dataclass
class Pep604:
union: DFoo | DBar
@typic.al
def pep585(data: dict[str, int]) -> dict[str, int]:
return data
@typic.al
def pep604(union: DFoo | DBar) -> DFoo | DBar:
return union
@typic.al
def number(n: numbers.Number) -> numbers.Number:
return n
TYPIC_OBJECTS = [
Typic,
Inherited,
FrozenTypic,
KlassDelayed,
KlassVarSubscripted,
Delayed,
Constrained,
NestedConstrained,
]
STD_OBJECTS = [
FromDict,
Data,
Nested,
NestedSeq,
NestedFromDict,
DefaultNone,
Forward,
FooNum,
Class,
NoParams,
UserID,
DateDict,
ShortStr,
LargeInt,
TDict,
NTup,
ntup,
]