-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtypes.py
988 lines (738 loc) · 26 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
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
"""Types shared across modules."""
import dataclasses
import datetime
import enum
from open_alchemy.helpers import custom_python_types
import typing
try: # pragma: no cover
from typing import Literal # pylint: disable=unused-import
from typing import Protocol
from typing import TypedDict
except ImportError: # pragma: no cover
from typing_extensions import Literal # type: ignore
from typing_extensions import Protocol # type: ignore
from typing_extensions import TypedDict # type: ignore
Schema = typing.Dict[str, typing.Any]
Schemas = typing.Dict[str, Schema]
TKwargs = typing.Dict[str, typing.Any]
TOptKwargs = typing.Optional[TKwargs]
@enum.unique
class KeyPrefixes(str, enum.Enum):
"""The allowed prefixes for extension properties."""
SHORT = "x-"
NAMESPACES = "x-open-alchemy-"
@enum.unique
class OpenApiProperties(str, enum.Enum):
"""All the OpenAPI properties that can be defined."""
TYPE: Literal["type"] = "type"
NULLABLE: Literal["nullable"] = "nullable"
FORMAT: Literal["format"] = "format"
MAX_LENGTH: Literal["maxLength"] = "maxLength"
READ_ONLY: Literal["readOnly"] = "readOnly"
WRITE_ONLY: Literal["writeOnly"] = "writeOnly"
DESCRIPTION: Literal["description"] = "description"
ITEMS: Literal["items"] = "items"
REF: Literal["$ref"] = "$ref"
DEFAULT: Literal["default"] = "default"
REQUIRED: Literal["required"] = "required"
PROPERTIES: Literal["properties"] = "properties"
@enum.unique
class ExtensionProperties(str, enum.Enum):
"""All the extension properties that can be defined."""
AUTOINCREMENT: Literal["x-autoincrement"] = "x-autoincrement"
INDEX: Literal["x-index"] = "x-index"
UNIQUE: Literal["x-unique"] = "x-unique"
PRIMARY_KEY: Literal["x-primary-key"] = "x-primary-key"
TABLENAME: Literal["x-tablename"] = "x-tablename"
INHERITS: Literal["x-inherits"] = "x-inherits"
JSON: Literal["x-json"] = "x-json"
BACKREF: Literal["x-backref"] = "x-backref"
SECONDARY: Literal["x-secondary"] = "x-secondary"
USELIST: Literal["x-uselist"] = "x-uselist"
KWARGS: Literal["x-kwargs"] = "x-kwargs"
FOREIGN_KEY_KWARGS: Literal["x-foreign-key-kwargs"] = "x-foreign-key-kwargs"
FOREIGN_KEY: Literal["x-foreign-key"] = "x-foreign-key"
FOREIGN_KEY_COLUMN: Literal["x-foreign-key-column"] = "x-foreign-key-column"
COMPOSITE_INDEX: Literal["x-composite-index"] = "x-composite-index"
COMPOSITE_UNIQUE: Literal["x-composite-unique"] = "x-composite-unique"
SERVER_DEFAULT: Literal["x-server-default"] = "x-server-default"
MIXINS: Literal["x-mixins"] = "x-mixins"
DICT_IGNORE: Literal["x-dict-ignore"] = "x-dict-ignore"
BACKREFS: Literal["x-backrefs"] = "x-backrefs"
DE_REF: Literal["x-de-$ref"] = "x-de-$ref"
SCHEMA_NAME: Literal["x-schema-name"] = "x-schema-name"
class ModelFactory(Protocol):
"""Defines interface for model factory."""
def __call__(self, *, name: str) -> typing.Type:
"""Call signature for ModelFactory."""
...
ColumnList = typing.List[str]
ColumnListList = typing.List[ColumnList]
class _UniqueBase(TypedDict, total=True):
"""Base class for unique schema."""
columns: typing.List[str]
class Unique(_UniqueBase, total=False):
"""Unique schema."""
name: typing.Optional[str]
UniqueList = typing.List[Unique]
AnyUnique = typing.Union[ColumnList, ColumnListList, Unique, UniqueList]
class _IndexBase(TypedDict, total=True):
"""Base class for index schema."""
expressions: typing.List[str]
class Index(_IndexBase, total=False):
"""Index schema."""
name: typing.Optional[str]
unique: bool
IndexList = typing.List[Index]
AnyIndex = typing.Union[ColumnList, ColumnListList, Index, IndexList]
TColumnDefault = typing.Optional[typing.Union[str, int, float, bool]]
TPyColumnDefault = typing.Optional[
typing.Union[str, int, float, bool, bytes, datetime.date, datetime.datetime, custom_python_types.duration]
]
_ColumnSchemaBase = TypedDict(
"_ColumnSchemaBase",
{
"x-dict-ignore": bool,
"format": str,
"x-primary-key": bool,
"maxLength": int,
"nullable": bool,
"description": str,
"x-json": bool,
"default": TColumnDefault,
"x-server-default": str,
"x-generated": bool,
"readOnly": bool,
"writeOnly": bool,
"x-foreign-key": str,
},
total=False,
)
class ColumnSchema(_ColumnSchemaBase, total=True):
"""Schema for column definitions."""
type: str
_ObjectRefSchemaBase = TypedDict(
"_ObjectRefSchemaBase", {"type": str, "x-de-$ref": str}, total=True
)
class ObjectRefSchema(_ObjectRefSchemaBase, total=False):
"""Schema for object reference definitions."""
nullable: bool
description: str
readOnly: bool
writeOnly: bool
_ArrayRefSchemaBase = TypedDict(
"_ArrayRefSchemaBase",
{"description": str, "readOnly": bool, "writeOnly": bool},
total=False,
)
class ArrayRefSchema(_ArrayRefSchemaBase, total=True):
"""Schema for array reference definitions."""
type: str
items: _ObjectRefSchemaBase
class TNameSchema(typing.NamedTuple):
"""The name and schema of a schema."""
name: str
schema: Schema
@enum.unique
class PropertyType(str, enum.Enum):
"""The type of a property."""
SIMPLE = "SIMPLE"
JSON = "JSON"
RELATIONSHIP = "RELATIONSHIP"
BACKREF = "BACKREF"
@enum.unique
class RelationshipType(str, enum.Enum):
"""The relationship type."""
MANY_TO_ONE = "MANY_TO_ONE"
ONE_TO_ONE = "ONE_TO_ONE"
ONE_TO_MANY = "ONE_TO_MANY"
MANY_TO_MANY = "MANY_TO_MANY"
TMixins = typing.List[str]
@dataclasses.dataclass
class PropertyArtifacts:
"""Information about a property."""
type: Literal[
PropertyType.SIMPLE,
PropertyType.JSON,
PropertyType.RELATIONSHIP,
PropertyType.BACKREF,
]
schema: typing.Union[
Schema,
ColumnSchema,
ObjectRefSchema,
ArrayRefSchema,
]
required: typing.Optional[bool]
description: typing.Optional[str]
class _OpenApiSimplePropertyTypedDictBase(TypedDict, total=False):
"""TypedDict representation of the OpenAPI artifacts for a simple property."""
format: str
max_length: int
nullable: bool
default: typing.Union[int, float, str, bool]
read_only: bool
write_only: bool
class OpenApiSimplePropertyTypedDict(_OpenApiSimplePropertyTypedDictBase, total=True):
"""TypedDict representation of the OpenAPI artifacts for a simple property."""
type: str
@dataclasses.dataclass
class OpenApiSimplePropertyArtifacts:
"""OpenAPI artifacts for the simple property."""
type: str
format: typing.Optional[str]
max_length: typing.Optional[int]
nullable: typing.Optional[bool]
default: typing.Optional[typing.Union[int, float, str, bool]]
read_only: typing.Optional[bool]
write_only: typing.Optional[bool]
def to_dict(self) -> OpenApiSimplePropertyTypedDict:
"""Convert to dictionary."""
return_dict: OpenApiSimplePropertyTypedDict = {"type": self.type}
opt_keys: typing.List[
Literal[
"format",
"max_length",
"nullable",
"default",
"read_only",
"write_only",
]
] = [
"format",
"max_length",
"nullable",
"default",
"read_only",
"write_only",
]
for opt_key in opt_keys:
value = getattr(self, opt_key)
if value is None:
continue
return_dict[opt_key] = value
return return_dict
class _ExtensionSimplePropertyTypedDictBase(TypedDict, total=False):
"""TypedDict representation of the extension artifacts for a simple property."""
autoincrement: bool
index: bool
unique: bool
server_default: str
foreign_key: str
kwargs: TKwargs
foreign_key_kwargs: TKwargs
class ExtensionSimplePropertyTypedDict(
_ExtensionSimplePropertyTypedDictBase, total=True
):
"""TypedDict representation of the extension artifacts for a simple property."""
primary_key: bool
@dataclasses.dataclass
class ExtensionSimplePropertyArtifacts:
"""OpenAPI artifacts for the simple property."""
primary_key: bool
autoincrement: typing.Optional[bool]
index: typing.Optional[bool]
unique: typing.Optional[bool]
server_default: typing.Optional[str]
foreign_key: typing.Optional[str]
kwargs: typing.Optional[TKwargs]
foreign_key_kwargs: typing.Optional[TKwargs]
dict_ignore: typing.Optional[bool]
def to_dict(self) -> ExtensionSimplePropertyTypedDict:
"""Convert to dictionary."""
return_dict: ExtensionSimplePropertyTypedDict = {
"primary_key": self.primary_key
}
opt_keys: typing.List[
Literal[
"autoincrement",
"index",
"unique",
"server_default",
"foreign_key",
"kwargs",
"foreign_key_kwargs",
]
] = [
"autoincrement",
"index",
"unique",
"server_default",
"foreign_key",
"kwargs",
"foreign_key_kwargs",
]
for opt_key in opt_keys:
value = getattr(self, opt_key)
if value is None:
continue
return_dict[opt_key] = value
return return_dict
class _SimplePropertyTypedDictBase(TypedDict, total=False):
"""TypedDict representation of the simple property."""
description: str
class SimplePropertyTypedDict(_SimplePropertyTypedDictBase, total=True):
"""TypedDict representation of the simple property."""
type: str
open_api: OpenApiSimplePropertyTypedDict
extension: ExtensionSimplePropertyTypedDict
schema: ColumnSchema
required: bool
@dataclasses.dataclass
class SimplePropertyArtifacts(PropertyArtifacts):
"""Information about a simple property."""
type: Literal[PropertyType.SIMPLE]
open_api: OpenApiSimplePropertyArtifacts
extension: ExtensionSimplePropertyArtifacts
schema: ColumnSchema
required: bool
def to_dict(self) -> SimplePropertyTypedDict:
"""Convert to dictionary."""
return_dict: SimplePropertyTypedDict = {
"type": self.type,
"open_api": self.open_api.to_dict(),
"extension": self.extension.to_dict(),
"schema": self.schema,
"required": self.required,
}
if self.description is not None:
return_dict["description"] = self.description
return return_dict
class OpenApiJsonPropertyTypedDict(TypedDict, total=False):
"""TypedDict representation of the OpenAPI artifacts for a JSON property."""
nullable: bool
read_only: bool
write_only: bool
@dataclasses.dataclass
class OpenApiJsonPropertyArtifacts:
"""OpenAPI artifacts for the JSON property."""
nullable: typing.Optional[bool]
read_only: typing.Optional[bool]
write_only: typing.Optional[bool]
def to_dict(self) -> OpenApiJsonPropertyTypedDict:
"""Convert to dictionary."""
return_dict: OpenApiJsonPropertyTypedDict = {}
opt_keys: typing.List[Literal["nullable", "read_only", "write_only"]] = [
"nullable",
"read_only",
"write_only",
]
for opt_key in opt_keys:
value = getattr(self, opt_key)
if value is None:
continue
return_dict[opt_key] = value
return return_dict
class _ExtensionJsonPropertyTypedDictBase(TypedDict, total=False):
"""TypedDict representation of the extension artifacts for a JSON property."""
index: bool
unique: bool
foreign_key: str
kwargs: TKwargs
foreign_key_kwargs: TKwargs
class ExtensionJsonPropertyTypedDict(_ExtensionJsonPropertyTypedDictBase, total=True):
"""TypedDict representation of the extension artifacts for a JSON property."""
primary_key: bool
@dataclasses.dataclass
class ExtensionJsonPropertyArtifacts:
"""OpenAPI artifacts for the JSON property."""
primary_key: bool
index: typing.Optional[bool]
unique: typing.Optional[bool]
foreign_key: typing.Optional[str]
kwargs: typing.Optional[TKwargs]
foreign_key_kwargs: typing.Optional[TKwargs]
def to_dict(self) -> ExtensionJsonPropertyTypedDict:
"""Convert to dictionary."""
return_dict: ExtensionJsonPropertyTypedDict = {"primary_key": self.primary_key}
opt_keys: typing.List[
Literal[
"index",
"unique",
"foreign_key",
"kwargs",
"foreign_key_kwargs",
]
] = [
"index",
"unique",
"foreign_key",
"kwargs",
"foreign_key_kwargs",
]
for opt_key in opt_keys:
value = getattr(self, opt_key)
if value is None:
continue
return_dict[opt_key] = value
return return_dict
class _JsonPropertyTypedDictBase(TypedDict, total=False):
"""TypedDict representation of the JSON property."""
description: str
class JsonPropertyTypedDict(_JsonPropertyTypedDictBase, total=True):
"""TypedDict representation of the JSON property."""
type: str
open_api: OpenApiJsonPropertyTypedDict
extension: ExtensionJsonPropertyTypedDict
schema: Schema
required: bool
@dataclasses.dataclass
class JsonPropertyArtifacts(PropertyArtifacts):
"""Information about a JSON property."""
type: Literal[PropertyType.JSON]
open_api: OpenApiJsonPropertyArtifacts
extension: ExtensionJsonPropertyArtifacts
schema: Schema
required: bool
def to_dict(self) -> JsonPropertyTypedDict:
"""Convert to dictionary."""
return_dict: JsonPropertyTypedDict = {
"type": self.type,
"open_api": self.open_api.to_dict(),
"extension": self.extension.to_dict(),
"schema": self.schema,
"required": self.required,
}
if self.description is not None:
return_dict["description"] = self.description
return return_dict
@dataclasses.dataclass
class RelationshipPropertyArtifacts(PropertyArtifacts):
"""Information about a relationship property."""
type: Literal[PropertyType.RELATIONSHIP]
sub_type: Literal[
RelationshipType.MANY_TO_ONE,
RelationshipType.ONE_TO_ONE,
RelationshipType.ONE_TO_MANY,
RelationshipType.MANY_TO_MANY,
]
parent: str
backref_property: typing.Optional[str]
kwargs: typing.Optional[TKwargs]
write_only: typing.Optional[bool]
description: typing.Optional[str]
required: bool
@dataclasses.dataclass
class NotManyToManyRelationshipPropertyArtifacts(RelationshipPropertyArtifacts):
"""Information about a relationship that is not many-to-many property."""
sub_type: Literal[
RelationshipType.MANY_TO_ONE,
RelationshipType.ONE_TO_ONE,
RelationshipType.ONE_TO_MANY,
]
foreign_key: str
foreign_key_property: str
class _OneToManyRelationshipPropertyTypedDictBase(TypedDict, total=False):
"""TypedDict representation of the one-to-many relationship property."""
backref_property: str
kwargs: TKwargs
write_only: bool
description: str
class OneToManyRelationshipPropertyTypedDict(
_OneToManyRelationshipPropertyTypedDictBase, total=True
):
"""TypedDict representation of the one-to-many relationship property."""
type: str
sub_type: str
parent: str
required: bool
schema: ArrayRefSchema
foreign_key: str
foreign_key_property: str
@dataclasses.dataclass
class OneToManyRelationshipPropertyArtifacts(
NotManyToManyRelationshipPropertyArtifacts
):
"""Information about a one-to-many relationship property."""
sub_type: Literal[
RelationshipType.ONE_TO_MANY,
]
schema: ArrayRefSchema
def to_dict(self) -> OneToManyRelationshipPropertyTypedDict:
"""Convert to dictionary."""
return_dict: OneToManyRelationshipPropertyTypedDict = {
"type": self.type,
"sub_type": self.sub_type,
"parent": self.parent,
"required": self.required,
"schema": self.schema,
"foreign_key": self.foreign_key,
"foreign_key_property": self.foreign_key_property,
}
opt_keys: typing.List[
Literal["backref_property", "kwargs", "write_only", "description"]
] = [
"backref_property",
"kwargs",
"write_only",
"description",
]
for opt_key in opt_keys:
value = getattr(self, opt_key)
if value is None:
continue
return_dict[opt_key] = value
return return_dict
class _XToOneRelationshipPropertyTypedDictBase(TypedDict, total=False):
"""TypedDict representation of the x-to-one relationship property."""
backref_property: str
kwargs: TKwargs
write_only: bool
description: str
nullable: bool
class XToOneRelationshipPropertyTypedDict(
_XToOneRelationshipPropertyTypedDictBase, total=True
):
"""TypedDict representation of the x-to-one relationship property."""
type: str
sub_type: str
parent: str
required: bool
schema: ObjectRefSchema
foreign_key: str
foreign_key_property: str
@dataclasses.dataclass
class XToOneRelationshipPropertyArtifacts(NotManyToManyRelationshipPropertyArtifacts):
"""Information about a x-to-one relationship property."""
sub_type: Literal[
RelationshipType.MANY_TO_ONE,
RelationshipType.ONE_TO_ONE,
]
schema: ObjectRefSchema
nullable: typing.Optional[bool]
def to_dict(self) -> XToOneRelationshipPropertyTypedDict:
"""Convert to dictionary."""
return_dict: XToOneRelationshipPropertyTypedDict = {
"type": self.type,
"sub_type": self.sub_type,
"parent": self.parent,
"required": self.required,
"schema": self.schema,
"foreign_key": self.foreign_key,
"foreign_key_property": self.foreign_key_property,
}
opt_keys: typing.List[
Literal[
"backref_property",
"kwargs",
"write_only",
"description",
"nullable",
]
] = [
"backref_property",
"kwargs",
"write_only",
"description",
"nullable",
]
for opt_key in opt_keys:
value = getattr(self, opt_key)
if value is None:
continue
return_dict[opt_key] = value
return return_dict
ManyToOneRelationshipPropertyTypedDict = XToOneRelationshipPropertyTypedDict
@dataclasses.dataclass
class ManyToOneRelationshipPropertyArtifacts(XToOneRelationshipPropertyArtifacts):
"""Information about a many-to-one relationship property."""
sub_type: Literal[
RelationshipType.MANY_TO_ONE,
]
def to_dict( # pylint: disable=useless-super-delegation
self,
) -> ManyToOneRelationshipPropertyTypedDict:
"""Convert to dictionary."""
return super().to_dict()
OneToOneRelationshipPropertyTypedDict = XToOneRelationshipPropertyTypedDict
@dataclasses.dataclass
class OneToOneRelationshipPropertyArtifacts(XToOneRelationshipPropertyArtifacts):
"""Information about a one-to-one relationship property."""
sub_type: Literal[
RelationshipType.ONE_TO_ONE,
]
def to_dict( # pylint: disable=useless-super-delegation
self,
) -> OneToOneRelationshipPropertyTypedDict:
"""Convert to dictionary."""
return super().to_dict()
class _ManyToManyRelationshipPropertyTypedDictBase(TypedDict, total=False):
"""TypedDict representation of the many-to-many relationship property."""
backref_property: str
kwargs: TKwargs
write_only: bool
description: str
class ManyToManyRelationshipPropertyTypedDict(
_ManyToManyRelationshipPropertyTypedDictBase, total=True
):
"""TypedDict representation of the many-to-many relationship property."""
type: str
sub_type: str
parent: str
required: bool
schema: ArrayRefSchema
secondary: str
@dataclasses.dataclass
class ManyToManyRelationshipPropertyArtifacts(RelationshipPropertyArtifacts):
"""Information about a x-to-one relationship property."""
sub_type: Literal[
RelationshipType.MANY_TO_MANY,
]
secondary: str
schema: ArrayRefSchema
def to_dict(self) -> ManyToManyRelationshipPropertyTypedDict:
"""Convert to dictionary."""
return_dict: ManyToManyRelationshipPropertyTypedDict = {
"type": self.type,
"sub_type": self.sub_type,
"parent": self.parent,
"required": self.required,
"schema": self.schema,
"secondary": self.secondary,
}
opt_keys: typing.List[
Literal["backref_property", "kwargs", "write_only", "description"]
] = [
"backref_property",
"kwargs",
"write_only",
"description",
]
for opt_key in opt_keys:
value = getattr(self, opt_key)
if value is None:
continue
return_dict[opt_key] = value
return return_dict
TAnyRelationshipPropertyTypedDict = typing.Union[
ManyToOneRelationshipPropertyTypedDict,
OneToOneRelationshipPropertyTypedDict,
OneToManyRelationshipPropertyTypedDict,
ManyToManyRelationshipPropertyTypedDict,
]
TAnyRelationshipPropertyArtifacts = typing.Union[
ManyToOneRelationshipPropertyArtifacts,
OneToOneRelationshipPropertyArtifacts,
OneToManyRelationshipPropertyArtifacts,
ManyToManyRelationshipPropertyArtifacts,
]
class _BackrefPropertyTypedDictBase(TypedDict, total=False):
"""TypedDict representation of the backref property."""
description: str
class BackrefPropertyTypedDict(_BackrefPropertyTypedDictBase, total=True):
"""TypedDict representation of the backref property."""
type: str
sub_type: str
properties: typing.List[str]
schema: Schema
@enum.unique
class BackrefSubType(str, enum.Enum):
"""The possible types of backreferences."""
OBJECT = "OBJECT"
ARRAY = "ARRAY"
@dataclasses.dataclass
class BackrefPropertyArtifacts(PropertyArtifacts):
"""Information about a back reference property."""
type: Literal[PropertyType.BACKREF]
sub_type: Literal[BackrefSubType.OBJECT, BackrefSubType.ARRAY]
properties: typing.List[str]
schema: Schema
required: None
def to_dict(self) -> BackrefPropertyTypedDict:
"""Convert to dictionary."""
return_dict: BackrefPropertyTypedDict = {
"type": self.type,
"sub_type": self.sub_type,
"properties": self.properties,
"schema": self.schema,
}
if self.description is not None:
return_dict["description"] = self.description
return return_dict
TAnyPropertyTypedDict = typing.Union[
SimplePropertyTypedDict,
JsonPropertyTypedDict,
TAnyRelationshipPropertyTypedDict,
BackrefPropertyTypedDict,
]
TAnyPropertyArtifacts = typing.Union[
SimplePropertyArtifacts,
JsonPropertyArtifacts,
TAnyRelationshipPropertyArtifacts,
BackrefPropertyArtifacts,
]
class PropertyValue(TypedDict, total=True):
"""Artifacts for a property."""
artifacts: TAnyPropertyTypedDict
PropertiesValue = typing.Dict[str, PropertyValue]
class _ModelTypedDictBase(TypedDict, total=False):
"""TypedDict representation of the model artifacts."""
inherits: bool
parent: str
description: str
mixins: TMixins
kwargs: TKwargs
composite_index: IndexList
composite_unique: UniqueList
class ModelTypedDict(_ModelTypedDictBase, total=True):
"""TypedDict representation of the model artifacts."""
tablename: str
class _ModelValueBase(TypedDict, total=False):
"""Record artifacts of a model."""
properties: PropertiesValue
class ModelValue(_ModelValueBase, total=True):
"""Record artifacts of a model."""
artifacts: ModelTypedDict
class ModelBackrefArtifacts(typing.NamedTuple):
"""Artifacts for model back references."""
type: BackrefSubType
child: str
@dataclasses.dataclass
class ModelExPropertiesArtifacts:
"""Information about a model excluding its properties."""
tablename: str
inherits: typing.Optional[bool]
parent: typing.Optional[str]
description: typing.Optional[str]
mixins: typing.Optional[TMixins]
kwargs: typing.Optional[TKwargs]
composite_index: typing.Optional[IndexList]
composite_unique: typing.Optional[UniqueList]
backrefs: typing.List[typing.Tuple[str, ModelBackrefArtifacts]]
def to_dict(self) -> ModelTypedDict:
"""Convert to dictionary."""
return_dict: ModelTypedDict = {"tablename": self.tablename}
opt_keys: typing.List[
Literal[
"inherits",
"parent",
"description",
"mixins",
"kwargs",
"composite_index",
"composite_unique",
]
] = [
"inherits",
"parent",
"description",
"mixins",
"kwargs",
"composite_index",
"composite_unique",
]
for opt_key in opt_keys:
value = getattr(self, opt_key)
if value is None:
continue
return_dict[opt_key] = value
return return_dict
@dataclasses.dataclass
class ModelArtifacts(ModelExPropertiesArtifacts):
"""Full information about a model."""
properties: typing.List[typing.Tuple[str, TAnyPropertyArtifacts]]
ModelsValue = typing.Dict[str, ModelValue]
class SpecValue(TypedDict, total=False):
"""Record artifacts for a specification."""
models: ModelsValue
ModelsModelArtifacts = typing.Dict[str, ModelArtifacts]