1111 Literal ,
1212 Protocol ,
1313 TypeVar ,
14+ Unpack ,
1415 overload ,
1516 runtime_checkable ,
1617)
@@ -780,8 +781,8 @@ class DangerouslySetInnerHTML(TypedDict):
780781}
781782
782783
783- class _VdomDict (TypedDict ):
784- """Dictionary representation of what the `Vdom` class eventually resolves into ."""
784+ class VdomTypeDict (TypedDict ):
785+ """TypedDict representation of what the `VdomDict` should look like ."""
785786
786787 tagName : str
787788 key : NotRequired [Key | None ]
@@ -791,7 +792,58 @@ class _VdomDict(TypedDict):
791792 importSource : NotRequired [ImportSourceDict ]
792793
793794
794- VdomDict = _VdomDict | dict [str , Any ]
795+ class VdomDict (dict ):
796+ """A dictionary representing a virtual DOM element."""
797+
798+ def __init__ (self , ** kwargs : Unpack [VdomTypeDict ]) -> None :
799+ invalid_keys = set (kwargs ) - ALLOWED_VDOM_KEYS
800+ if invalid_keys :
801+ msg = f"Invalid keys: { invalid_keys } ."
802+ raise ValueError (msg )
803+
804+ super ().__init__ (** kwargs )
805+
806+ @overload
807+ def __getitem__ (self , key : Literal ["tagName" ]) -> str : ...
808+ @overload
809+ def __getitem__ (self , key : Literal ["key" ]) -> Key | None : ...
810+ @overload
811+ def __getitem__ (
812+ self , key : Literal ["children" ]
813+ ) -> Sequence [ComponentType | VdomChild ]: ...
814+ @overload
815+ def __getitem__ (self , key : Literal ["attributes" ]) -> VdomAttributes : ...
816+ @overload
817+ def __getitem__ (self , key : Literal ["eventHandlers" ]) -> EventHandlerDict : ...
818+ @overload
819+ def __getitem__ (self , key : Literal ["importSource" ]) -> ImportSourceDict : ...
820+ def __getitem__ (self , key : VdomDictKeys ) -> Any :
821+ return super ().__getitem__ (key )
822+
823+ @overload
824+ def __setitem__ (self , key : Literal ["tagName" ], value : str ) -> None : ...
825+ @overload
826+ def __setitem__ (self , key : Literal ["key" ], value : Key | None ) -> None : ...
827+ @overload
828+ def __setitem__ (
829+ self , key : Literal ["children" ], value : Sequence [ComponentType | VdomChild ]
830+ ) -> None : ...
831+ @overload
832+ def __setitem__ (
833+ self , key : Literal ["attributes" ], value : VdomAttributes
834+ ) -> None : ...
835+ @overload
836+ def __setitem__ (
837+ self , key : Literal ["eventHandlers" ], value : EventHandlerDict
838+ ) -> None : ...
839+ @overload
840+ def __setitem__ (
841+ self , key : Literal ["importSource" ], value : ImportSourceDict
842+ ) -> None : ...
843+ def __setitem__ (self , key : VdomDictKeys , value : Any ) -> None :
844+ if key not in ALLOWED_VDOM_KEYS :
845+ raise KeyError (f"Invalid key: { key } " )
846+ super ().__setitem__ (key , value )
795847
796848
797849VdomChild : TypeAlias = ComponentType | VdomDict | str | None | Any
@@ -875,14 +927,14 @@ class VdomDictConstructor(Protocol):
875927 @overload
876928 def __call__ (
877929 self , attributes : VdomAttributes , / , * children : VdomChildren
878- ) -> VdomDict | dict [ str , Any ] : ...
930+ ) -> VdomDict : ...
879931
880932 @overload
881- def __call__ (self , * children : VdomChildren ) -> VdomDict | dict [ str , Any ] : ...
933+ def __call__ (self , * children : VdomChildren ) -> VdomDict : ...
882934
883935 def __call__ (
884936 self , * attributes_and_children : VdomAttributes | VdomChildren
885- ) -> VdomDict | dict [ str , Any ] : ...
937+ ) -> VdomDict : ...
886938
887939
888940class LayoutUpdateMessage (TypedDict ):
0 commit comments