-
-
Notifications
You must be signed in to change notification settings - Fork 323
/
Copy pathtypes.py
1073 lines (959 loc) · 31.3 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
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
from collections.abc import Awaitable, Mapping, Sequence
from dataclasses import dataclass
from pathlib import Path
from types import TracebackType
from typing import (
Any,
Callable,
Generic,
Literal,
Protocol,
TypeVar,
overload,
runtime_checkable,
)
from typing_extensions import NamedTuple, NotRequired, TypeAlias, TypedDict, Unpack
CarrierType = TypeVar("CarrierType")
_Type = TypeVar("_Type")
class State(NamedTuple, Generic[_Type]):
value: _Type
set_value: Callable[[_Type | Callable[[_Type], _Type]], None]
ComponentConstructor = Callable[..., "ComponentType"]
"""Simple function returning a new component"""
RootComponentConstructor = Callable[[], "ComponentType"]
"""The root component should be constructed by a function accepting no arguments."""
Key: TypeAlias = str | int
@runtime_checkable
class ComponentType(Protocol):
"""The expected interface for all component-like objects"""
key: Key | None
"""An identifier which is unique amongst a component's immediate siblings"""
type: Any
"""The function or class defining the behavior of this component
This is used to see if two component instances share the same definition.
"""
def render(self) -> VdomDict | ComponentType | str | None:
"""Render the component's view model."""
_Render_co = TypeVar("_Render_co", covariant=True)
_Event_contra = TypeVar("_Event_contra", contravariant=True)
@runtime_checkable
class LayoutType(Protocol[_Render_co, _Event_contra]):
"""Renders and delivers, updates to views and events to handlers, respectively"""
async def render(
self,
) -> _Render_co: ... # Render an update to a view
async def deliver(
self, event: _Event_contra
) -> None: ... # Relay an event to its respective handler
async def __aenter__(
self,
) -> LayoutType[
_Render_co, _Event_contra
]: ... # Prepare the layout for its first render
async def __aexit__(
self,
exc_type: type[Exception],
exc_value: Exception,
traceback: TracebackType,
) -> bool | None:
"""Clean up the view after its final render"""
class CssStyleTypeDict(TypedDict, total=False):
# TODO: This could generated by parsing from `csstype` in the future
# https://www.npmjs.com/package/csstype
accentColor: str | int
alignContent: str | int
alignItems: str | int
alignSelf: str | int
alignTracks: str | int
all: str | int
animation: str | int
animationComposition: str | int
animationDelay: str | int
animationDirection: str | int
animationDuration: str | int
animationFillMode: str | int
animationIterationCount: str | int
animationName: str | int
animationPlayState: str | int
animationTimeline: str | int
animationTimingFunction: str | int
appearance: str | int
aspectRatio: str | int
backdropFilter: str | int
backfaceVisibility: str | int
background: str | int
backgroundAttachment: str | int
backgroundBlendMode: str | int
backgroundClip: str | int
backgroundColor: str | int
backgroundImage: str | int
backgroundOrigin: str | int
backgroundPosition: str | int
backgroundPositionX: str | int
backgroundPositionY: str | int
backgroundRepeat: str | int
backgroundSize: str | int
blockOverflow: str | int
blockSize: str | int
border: str | int
borderBlock: str | int
borderBlockColor: str | int
borderBlockEnd: str | int
borderBlockEndColor: str | int
borderBlockEndStyle: str | int
borderBlockEndWidth: str | int
borderBlockStart: str | int
borderBlockStartColor: str | int
borderBlockStartStyle: str | int
borderBlockStartWidth: str | int
borderBlockStyle: str | int
borderBlockWidth: str | int
borderBottom: str | int
borderBottomColor: str | int
borderBottomLeftRadius: str | int
borderBottomRightRadius: str | int
borderBottomStyle: str | int
borderBottomWidth: str | int
borderCollapse: str | int
borderColor: str | int
borderEndEndRadius: str | int
borderEndStartRadius: str | int
borderImage: str | int
borderImageOutset: str | int
borderImageRepeat: str | int
borderImageSlice: str | int
borderImageSource: str | int
borderImageWidth: str | int
borderInline: str | int
borderInlineColor: str | int
borderInlineEnd: str | int
borderInlineEndColor: str | int
borderInlineEndStyle: str | int
borderInlineEndWidth: str | int
borderInlineStart: str | int
borderInlineStartColor: str | int
borderInlineStartStyle: str | int
borderInlineStartWidth: str | int
borderInlineStyle: str | int
borderInlineWidth: str | int
borderLeft: str | int
borderLeftColor: str | int
borderLeftStyle: str | int
borderLeftWidth: str | int
borderRadius: str | int
borderRight: str | int
borderRightColor: str | int
borderRightStyle: str | int
borderRightWidth: str | int
borderSpacing: str | int
borderStartEndRadius: str | int
borderStartStartRadius: str | int
borderStyle: str | int
borderTop: str | int
borderTopColor: str | int
borderTopLeftRadius: str | int
borderTopRightRadius: str | int
borderTopStyle: str | int
borderTopWidth: str | int
borderWidth: str | int
bottom: str | int
boxDecorationBreak: str | int
boxShadow: str | int
boxSizing: str | int
breakAfter: str | int
breakBefore: str | int
breakInside: str | int
captionSide: str | int
caret: str | int
caretColor: str | int
caretShape: str | int
clear: str | int
clip: str | int
clipPath: str | int
color: str | int
colorScheme: str | int
columnCount: str | int
columnFill: str | int
columnGap: str | int
columnRule: str | int
columnRuleColor: str | int
columnRuleStyle: str | int
columnRuleWidth: str | int
columnSpan: str | int
columnWidth: str | int
columns: str | int
contain: str | int
containIntrinsicBlockSize: str | int
containIntrinsicHeight: str | int
containIntrinsicInlineSize: str | int
containIntrinsicSize: str | int
containIntrinsicWidth: str | int
content: str | int
contentVisibility: str | int
counterIncrement: str | int
counterReset: str | int
counterSet: str | int
cursor: str | int
direction: str | int
display: str | int
emptyCells: str | int
filter: str | int
flex: str | int
flexBasis: str | int
flexDirection: str | int
flexFlow: str | int
flexGrow: str | int
flexShrink: str | int
flexWrap: str | int
float: str | int
font: str | int
fontFamily: str | int
fontFeatureSettings: str | int
fontKerning: str | int
fontLanguageOverride: str | int
fontOpticalSizing: str | int
fontSize: str | int
fontSizeAdjust: str | int
fontStretch: str | int
fontStyle: str | int
fontSynthesis: str | int
fontVariant: str | int
fontVariantAlternates: str | int
fontVariantCaps: str | int
fontVariantEastAsian: str | int
fontVariantLigatures: str | int
fontVariantNumeric: str | int
fontVariantPosition: str | int
fontVariationSettings: str | int
fontWeight: str | int
forcedColorAdjust: str | int
gap: str | int
grid: str | int
gridArea: str | int
gridAutoColumns: str | int
gridAutoFlow: str | int
gridAutoRows: str | int
gridColumn: str | int
gridColumnEnd: str | int
gridColumnStart: str | int
gridRow: str | int
gridRowEnd: str | int
gridRowStart: str | int
gridTemplate: str | int
gridTemplateAreas: str | int
gridTemplateColumns: str | int
gridTemplateRows: str | int
hangingPunctuation: str | int
height: str | int
hyphenateCharacter: str | int
hyphenateLimitChars: str | int
hyphens: str | int
imageOrientation: str | int
imageRendering: str | int
imageResolution: str | int
inherit: str | int
initial: str | int
initialLetter: str | int
initialLetterAlign: str | int
inlineSize: str | int
inputSecurity: str | int
inset: str | int
insetBlock: str | int
insetBlockEnd: str | int
insetBlockStart: str | int
insetInline: str | int
insetInlineEnd: str | int
insetInlineStart: str | int
isolation: str | int
justifyContent: str | int
justifyItems: str | int
justifySelf: str | int
justifyTracks: str | int
left: str | int
letterSpacing: str | int
lineBreak: str | int
lineClamp: str | int
lineHeight: str | int
lineHeightStep: str | int
listStyle: str | int
listStyleImage: str | int
listStylePosition: str | int
listStyleType: str | int
margin: str | int
marginBlock: str | int
marginBlockEnd: str | int
marginBlockStart: str | int
marginBottom: str | int
marginInline: str | int
marginInlineEnd: str | int
marginInlineStart: str | int
marginLeft: str | int
marginRight: str | int
marginTop: str | int
marginTrim: str | int
mask: str | int
maskBorder: str | int
maskBorderMode: str | int
maskBorderOutset: str | int
maskBorderRepeat: str | int
maskBorderSlice: str | int
maskBorderSource: str | int
maskBorderWidth: str | int
maskClip: str | int
maskComposite: str | int
maskImage: str | int
maskMode: str | int
maskOrigin: str | int
maskPosition: str | int
maskRepeat: str | int
maskSize: str | int
maskType: str | int
masonryAutoFlow: str | int
mathDepth: str | int
mathShift: str | int
mathStyle: str | int
maxBlockSize: str | int
maxHeight: str | int
maxInlineSize: str | int
maxLines: str | int
maxWidth: str | int
minBlockSize: str | int
minHeight: str | int
minInlineSize: str | int
minWidth: str | int
mixBlendMode: str | int
objectFit: str | int
objectPosition: str | int
offset: str | int
offsetAnchor: str | int
offsetDistance: str | int
offsetPath: str | int
offsetPosition: str | int
offsetRotate: str | int
opacity: str | int
order: str | int
orphans: str | int
outline: str | int
outlineColor: str | int
outlineOffset: str | int
outlineStyle: str | int
outlineWidth: str | int
overflow: str | int
overflowAnchor: str | int
overflowBlock: str | int
overflowClipMargin: str | int
overflowInline: str | int
overflowWrap: str | int
overflowX: str | int
overflowY: str | int
overscrollBehavior: str | int
overscrollBehaviorBlock: str | int
overscrollBehaviorInline: str | int
overscrollBehaviorX: str | int
overscrollBehaviorY: str | int
padding: str | int
paddingBlock: str | int
paddingBlockEnd: str | int
paddingBlockStart: str | int
paddingBottom: str | int
paddingInline: str | int
paddingInlineEnd: str | int
paddingInlineStart: str | int
paddingLeft: str | int
paddingRight: str | int
paddingTop: str | int
pageBreakAfter: str | int
pageBreakBefore: str | int
pageBreakInside: str | int
paintOrder: str | int
perspective: str | int
perspectiveOrigin: str | int
placeContent: str | int
placeItems: str | int
placeSelf: str | int
pointerEvents: str | int
position: str | int
printColorAdjust: str | int
quotes: str | int
resize: str | int
revert: str | int
right: str | int
rotate: str | int
rowGap: str | int
rubyAlign: str | int
rubyMerge: str | int
rubyPosition: str | int
scale: str | int
scrollBehavior: str | int
scrollMargin: str | int
scrollMarginBlock: str | int
scrollMarginBlockEnd: str | int
scrollMarginBlockStart: str | int
scrollMarginBottom: str | int
scrollMarginInline: str | int
scrollMarginInlineEnd: str | int
scrollMarginInlineStart: str | int
scrollMarginLeft: str | int
scrollMarginRight: str | int
scrollMarginTop: str | int
scrollPadding: str | int
scrollPaddingBlock: str | int
scrollPaddingBlockEnd: str | int
scrollPaddingBlockStart: str | int
scrollPaddingBottom: str | int
scrollPaddingInline: str | int
scrollPaddingInlineEnd: str | int
scrollPaddingInlineStart: str | int
scrollPaddingLeft: str | int
scrollPaddingRight: str | int
scrollPaddingTop: str | int
scrollSnapAlign: str | int
scrollSnapStop: str | int
scrollSnapType: str | int
scrollTimeline: str | int
scrollTimelineAxis: str | int
scrollTimelineName: str | int
scrollbarColor: str | int
scrollbarGutter: str | int
scrollbarWidth: str | int
shapeImageThreshold: str | int
shapeMargin: str | int
shapeOutside: str | int
tabSize: str | int
tableLayout: str | int
textAlign: str | int
textAlignLast: str | int
textCombineUpright: str | int
textDecoration: str | int
textDecorationColor: str | int
textDecorationLine: str | int
textDecorationSkip: str | int
textDecorationSkipInk: str | int
textDecorationStyle: str | int
textDecorationThickness: str | int
textEmphasis: str | int
textEmphasisColor: str | int
textEmphasisPosition: str | int
textEmphasisStyle: str | int
textIndent: str | int
textJustify: str | int
textOrientation: str | int
textOverflow: str | int
textRendering: str | int
textShadow: str | int
textSizeAdjust: str | int
textTransform: str | int
textUnderlineOffset: str | int
textUnderlinePosition: str | int
top: str | int
touchAction: str | int
transform: str | int
transformBox: str | int
transformOrigin: str | int
transformStyle: str | int
transition: str | int
transitionDelay: str | int
transitionDuration: str | int
transitionProperty: str | int
transitionTimingFunction: str | int
translate: str | int
unicodeBidi: str | int
unset: str | int
userSelect: str | int
verticalAlign: str | int
visibility: str | int
whiteSpace: str | int
widows: str | int
width: str | int
willChange: str | int
wordBreak: str | int
wordSpacing: str | int
wordWrap: str | int
writingMode: str | int
zIndex: str | int
# TODO: Enable `extra_items` on `CssStyleDict` when PEP 728 is merged, likely in Python 3.14. Ref: https://peps.python.org/pep-0728/
CssStyleDict = CssStyleTypeDict | dict[str, Any]
EventFunc = Callable[[dict[str, Any]], Awaitable[None] | None]
class DangerouslySetInnerHTML(TypedDict):
__html: str
# TODO: It's probably better to break this one attributes dict down into what each specific
# HTML node's attributes can be, and make sure those types are resolved correctly within `HtmlConstructor`
# TODO: This could be generated by parsing from `@types/react` in the future
# https://www.npmjs.com/package/@types/react?activeTab=code
VdomAttributesTypeDict = TypedDict(
"VdomAttributesTypeDict",
{
"key": Key,
"value": Any,
"defaultValue": Any,
"dangerouslySetInnerHTML": DangerouslySetInnerHTML,
"suppressContentEditableWarning": bool,
"suppressHydrationWarning": bool,
"style": CssStyleDict,
"accessKey": str,
"aria-": None,
"autoCapitalize": str,
"className": str,
"contentEditable": bool,
"data-": None,
"dir": Literal["ltr", "rtl"],
"draggable": bool,
"enterKeyHint": str,
"htmlFor": str,
"hidden": bool | str,
"id": str,
"is": str,
"inputMode": str,
"itemProp": str,
"lang": str,
"onAnimationEnd": EventFunc,
"onAnimationEndCapture": EventFunc,
"onAnimationIteration": EventFunc,
"onAnimationIterationCapture": EventFunc,
"onAnimationStart": EventFunc,
"onAnimationStartCapture": EventFunc,
"onAuxClick": EventFunc,
"onAuxClickCapture": EventFunc,
"onBeforeInput": EventFunc,
"onBeforeInputCapture": EventFunc,
"onBlur": EventFunc,
"onBlurCapture": EventFunc,
"onClick": EventFunc,
"onClickCapture": EventFunc,
"onCompositionStart": EventFunc,
"onCompositionStartCapture": EventFunc,
"onCompositionEnd": EventFunc,
"onCompositionEndCapture": EventFunc,
"onCompositionUpdate": EventFunc,
"onCompositionUpdateCapture": EventFunc,
"onContextMenu": EventFunc,
"onContextMenuCapture": EventFunc,
"onCopy": EventFunc,
"onCopyCapture": EventFunc,
"onCut": EventFunc,
"onCutCapture": EventFunc,
"onDoubleClick": EventFunc,
"onDoubleClickCapture": EventFunc,
"onDrag": EventFunc,
"onDragCapture": EventFunc,
"onDragEnd": EventFunc,
"onDragEndCapture": EventFunc,
"onDragEnter": EventFunc,
"onDragEnterCapture": EventFunc,
"onDragOver": EventFunc,
"onDragOverCapture": EventFunc,
"onDragStart": EventFunc,
"onDragStartCapture": EventFunc,
"onDrop": EventFunc,
"onDropCapture": EventFunc,
"onFocus": EventFunc,
"onFocusCapture": EventFunc,
"onGotPointerCapture": EventFunc,
"onGotPointerCaptureCapture": EventFunc,
"onKeyDown": EventFunc,
"onKeyDownCapture": EventFunc,
"onKeyPress": EventFunc,
"onKeyPressCapture": EventFunc,
"onKeyUp": EventFunc,
"onKeyUpCapture": EventFunc,
"onLostPointerCapture": EventFunc,
"onLostPointerCaptureCapture": EventFunc,
"onMouseDown": EventFunc,
"onMouseDownCapture": EventFunc,
"onMouseEnter": EventFunc,
"onMouseLeave": EventFunc,
"onMouseMove": EventFunc,
"onMouseMoveCapture": EventFunc,
"onMouseOut": EventFunc,
"onMouseOutCapture": EventFunc,
"onMouseUp": EventFunc,
"onMouseUpCapture": EventFunc,
"onPointerCancel": EventFunc,
"onPointerCancelCapture": EventFunc,
"onPointerDown": EventFunc,
"onPointerDownCapture": EventFunc,
"onPointerEnter": EventFunc,
"onPointerLeave": EventFunc,
"onPointerMove": EventFunc,
"onPointerMoveCapture": EventFunc,
"onPointerOut": EventFunc,
"onPointerOutCapture": EventFunc,
"onPointerUp": EventFunc,
"onPointerUpCapture": EventFunc,
"onPaste": EventFunc,
"onPasteCapture": EventFunc,
"onScroll": EventFunc,
"onScrollCapture": EventFunc,
"onSelect": EventFunc,
"onSelectCapture": EventFunc,
"onTouchCancel": EventFunc,
"onTouchCancelCapture": EventFunc,
"onTouchEnd": EventFunc,
"onTouchEndCapture": EventFunc,
"onTouchMove": EventFunc,
"onTouchMoveCapture": EventFunc,
"onTouchStart": EventFunc,
"onTouchStartCapture": EventFunc,
"onTransitionEnd": EventFunc,
"onTransitionEndCapture": EventFunc,
"onWheel": EventFunc,
"onWheelCapture": EventFunc,
"role": str,
"slot": str,
"spellCheck": bool | None,
"tabIndex": int,
"title": str,
"translate": Literal["yes", "no"],
"onReset": EventFunc,
"onResetCapture": EventFunc,
"onSubmit": EventFunc,
"onSubmitCapture": EventFunc,
"formAction": str | Callable,
"checked": bool,
"defaultChecked": bool,
"accept": str,
"alt": str,
"capture": str,
"autoComplete": str,
"autoFocus": bool,
"dirname": str,
"disabled": bool,
"form": str,
"formEnctype": str,
"formMethod": str,
"formNoValidate": str,
"formTarget": str,
"height": str,
"list": str,
"max": int,
"maxLength": int,
"min": int,
"minLength": int,
"multiple": bool,
"name": str,
"onChange": EventFunc,
"onChangeCapture": EventFunc,
"onInput": EventFunc,
"onInputCapture": EventFunc,
"onInvalid": EventFunc,
"onInvalidCapture": EventFunc,
"pattern": str,
"placeholder": str,
"readOnly": bool,
"required": bool,
"size": int,
"src": str,
"step": int | Literal["any"],
"type": str,
"width": str,
"label": str,
"cols": int,
"rows": int,
"wrap": Literal["hard", "soft", "off"],
"rel": str,
"precedence": str,
"media": str,
"onError": EventFunc,
"onLoad": EventFunc,
"as": str,
"imageSrcSet": str,
"imageSizes": str,
"sizes": str,
"href": str,
"crossOrigin": str,
"referrerPolicy": str,
"fetchPriority": str,
"hrefLang": str,
"integrity": str,
"blocking": str,
"async": bool,
"noModule": bool,
"nonce": str,
"referrer": str,
"defer": str,
"onToggle": EventFunc,
"onToggleCapture": EventFunc,
"onLoadCapture": EventFunc,
"onErrorCapture": EventFunc,
"onAbort": EventFunc,
"onAbortCapture": EventFunc,
"onCanPlay": EventFunc,
"onCanPlayCapture": EventFunc,
"onCanPlayThrough": EventFunc,
"onCanPlayThroughCapture": EventFunc,
"onDurationChange": EventFunc,
"onDurationChangeCapture": EventFunc,
"onEmptied": EventFunc,
"onEmptiedCapture": EventFunc,
"onEncrypted": EventFunc,
"onEncryptedCapture": EventFunc,
"onEnded": EventFunc,
"onEndedCapture": EventFunc,
"onLoadedData": EventFunc,
"onLoadedDataCapture": EventFunc,
"onLoadedMetadata": EventFunc,
"onLoadedMetadataCapture": EventFunc,
"onLoadStart": EventFunc,
"onLoadStartCapture": EventFunc,
"onPause": EventFunc,
"onPauseCapture": EventFunc,
"onPlay": EventFunc,
"onPlayCapture": EventFunc,
"onPlaying": EventFunc,
"onPlayingCapture": EventFunc,
"onProgress": EventFunc,
"onProgressCapture": EventFunc,
"onRateChange": EventFunc,
"onRateChangeCapture": EventFunc,
"onResize": EventFunc,
"onResizeCapture": EventFunc,
"onSeeked": EventFunc,
"onSeekedCapture": EventFunc,
"onSeeking": EventFunc,
"onSeekingCapture": EventFunc,
"onStalled": EventFunc,
"onStalledCapture": EventFunc,
"onSuspend": EventFunc,
"onSuspendCapture": EventFunc,
"onTimeUpdate": EventFunc,
"onTimeUpdateCapture": EventFunc,
"onVolumeChange": EventFunc,
"onVolumeChangeCapture": EventFunc,
"onWaiting": EventFunc,
"onWaitingCapture": EventFunc,
},
total=False,
)
# TODO: Enable `extra_items` on `VdomAttributes` when PEP 728 is merged, likely in Python 3.14. Ref: https://peps.python.org/pep-0728/
VdomAttributes = VdomAttributesTypeDict | dict[str, Any]
VdomDictKeys = Literal[
"tagName",
"key",
"children",
"attributes",
"eventHandlers",
"inlineJavaScript",
"importSource",
]
ALLOWED_VDOM_KEYS = {
"tagName",
"key",
"children",
"attributes",
"eventHandlers",
"inlineJavaScript",
"importSource",
}
class VdomTypeDict(TypedDict):
"""TypedDict representation of what the `VdomDict` should look like."""
tagName: str
key: NotRequired[Key | None]
children: NotRequired[Sequence[ComponentType | VdomChild]]
attributes: NotRequired[VdomAttributes]
eventHandlers: NotRequired[EventHandlerDict]
inlineJavaScript: NotRequired[InlineJavaScriptDict]
importSource: NotRequired[ImportSourceDict]
class VdomDict(dict):
"""A light wrapper around Python `dict` that represents a Virtual DOM element."""
def __init__(self, **kwargs: Unpack[VdomTypeDict]) -> None:
if "tagName" not in kwargs:
msg = "VdomDict requires a 'tagName' key."
raise ValueError(msg)
invalid_keys = set(kwargs) - ALLOWED_VDOM_KEYS
if invalid_keys:
msg = f"Invalid keys: {invalid_keys}."
raise ValueError(msg)
super().__init__(**kwargs)
@overload
def __getitem__(self, key: Literal["tagName"]) -> str: ...
@overload
def __getitem__(self, key: Literal["key"]) -> Key | None: ...
@overload
def __getitem__(
self, key: Literal["children"]
) -> Sequence[ComponentType | VdomChild]: ...
@overload
def __getitem__(self, key: Literal["attributes"]) -> VdomAttributes: ...
@overload
def __getitem__(self, key: Literal["eventHandlers"]) -> EventHandlerDict: ...
@overload
def __getitem__(self, key: Literal["inlineJavaScript"]) -> InlineJavaScriptDict: ...
@overload
def __getitem__(self, key: Literal["importSource"]) -> ImportSourceDict: ...
def __getitem__(self, key: VdomDictKeys) -> Any:
return super().__getitem__(key)
@overload
def __setitem__(self, key: Literal["tagName"], value: str) -> None: ...
@overload
def __setitem__(self, key: Literal["key"], value: Key | None) -> None: ...
@overload
def __setitem__(
self, key: Literal["children"], value: Sequence[ComponentType | VdomChild]
) -> None: ...
@overload
def __setitem__(
self, key: Literal["attributes"], value: VdomAttributes
) -> None: ...
@overload
def __setitem__(
self, key: Literal["eventHandlers"], value: EventHandlerDict
) -> None: ...
@overload
def __setitem__(
self, key: Literal["inlineJavaScript"], value: InlineJavaScriptDict
) -> None: ...
@overload
def __setitem__(
self, key: Literal["importSource"], value: ImportSourceDict
) -> None: ...
def __setitem__(self, key: VdomDictKeys, value: Any) -> None:
if key not in ALLOWED_VDOM_KEYS:
raise KeyError(f"Invalid key: {key}")
super().__setitem__(key, value)
VdomChild: TypeAlias = ComponentType | VdomDict | str | None | Any
"""A single child element of a :class:`VdomDict`"""
VdomChildren: TypeAlias = Sequence[VdomChild] | VdomChild
"""Describes a series of :class:`VdomChild` elements"""
class ImportSourceDict(TypedDict):
source: str
fallback: Any
sourceType: str
unmountBeforeUpdate: bool
class VdomJson(TypedDict):
"""A JSON serializable form of :class:`VdomDict` matching the :data:`VDOM_JSON_SCHEMA`"""
tagName: str
key: NotRequired[Key]
error: NotRequired[str]
children: NotRequired[list[Any]]
attributes: NotRequired[VdomAttributes]
eventHandlers: NotRequired[dict[str, JsonEventTarget]]
inlineJavaScript: NotRequired[dict[str, InlineJavaScript]]
importSource: NotRequired[JsonImportSource]
class JsonEventTarget(TypedDict):
target: str
preventDefault: bool
stopPropagation: bool
class JsonImportSource(TypedDict):
source: str
fallback: Any
class InlineJavaScript(str):
"""Simple subclass that flags a user's string in ReactPy VDOM attributes as executable JavaScript."""
pass
class EventHandlerFunc(Protocol):
"""A coroutine which can handle event data"""
async def __call__(self, data: Sequence[Any]) -> None: ...
@runtime_checkable
class EventHandlerType(Protocol):
"""Defines a handler for some event"""
prevent_default: bool
"""Whether to block the event from propagating further up the DOM"""
stop_propagation: bool
"""Stops the default action associate with the event from taking place."""
function: EventHandlerFunc
"""A coroutine which can respond to an event and its data"""
target: str | None
"""Typically left as ``None`` except when a static target is useful.
When testing, it may be useful to specify a static target ID so events can be
triggered programmatically.
.. note::
When ``None``, it is left to a :class:`LayoutType` to auto generate a unique ID.
"""
EventHandlerMapping = Mapping[str, EventHandlerType]
"""A generic mapping between event names to their handlers"""
EventHandlerDict: TypeAlias = dict[str, EventHandlerType]
"""A dict mapping between event names to their handlers"""
InlineJavaScriptMapping = Mapping[str, InlineJavaScript]
"""A generic mapping between attribute names to their inline javascript"""
InlineJavaScriptDict: TypeAlias = dict[str, InlineJavaScript]
"""A dict mapping between attribute names to their inline javascript"""
class VdomConstructor(Protocol):
"""Standard function for constructing a :class:`VdomDict`"""
@overload
def __call__(
self, attributes: VdomAttributes, /, *children: VdomChildren
) -> VdomDict: ...
@overload
def __call__(self, *children: VdomChildren) -> VdomDict: ...
def __call__(
self, *attributes_and_children: VdomAttributes | VdomChildren
) -> VdomDict: ...
class LayoutUpdateMessage(TypedDict):
"""A message describing an update to a layout"""
type: Literal["layout-update"]
"""The type of message"""
path: str
"""JSON Pointer path to the model element being updated"""
model: VdomJson | dict[str, Any]
"""The model to assign at the given JSON Pointer path"""
class LayoutEventMessage(TypedDict):
"""Message describing an event originating from an element in the layout"""
type: Literal["layout-event"]
"""The type of message"""
target: str
"""The ID of the event handler."""
data: Sequence[Any]
"""A list of event data passed to the event handler."""
class Context(Protocol[_Type]):
"""Returns a :class:`ContextProvider` component"""
def __call__(
self,
*children: Any,
value: _Type = ...,
key: Key | None = ...,
) -> ContextProviderType[_Type]: ...
class ContextProviderType(ComponentType, Protocol[_Type]):
"""A component which provides a context value to its children"""
type: Context[_Type]