11from __future__ import annotations
22
3+ import inspect
34from collections .abc import Awaitable , Callable , Mapping , Sequence
45from dataclasses import dataclass
56from pathlib import Path
@@ -26,31 +27,49 @@ class State(NamedTuple, Generic[_Type]):
2627 set_value : Callable [[_Type | Callable [[_Type ], _Type ]], None ]
2728
2829
29- ComponentConstructor = Callable [..., "ComponentType " ]
30+ ComponentConstructor = Callable [..., "Component " ]
3031"""Simple function returning a new component"""
3132
32- RootComponentConstructor = Callable [[], "ComponentType " ]
33+ RootComponentConstructor = Callable [[], "Component " ]
3334"""The root component should be constructed by a function accepting no arguments."""
3435
3536
3637Key : TypeAlias = str | int
3738
3839
39- @runtime_checkable
40- class ComponentType (Protocol ):
41- """The expected interface for all component-like objects"""
42-
43- key : Key | None
44- """An identifier which is unique amongst a component's immediate siblings"""
40+ class Component :
41+ """An object for rending component models."""
4542
46- type : Any
47- """The function or class defining the behavior of this component
43+ __slots__ = "__weakref__" , "_args" , "_func" , "_kwargs" , "_sig" , "key" , "type"
4844
49- This is used to see if two component instances share the same definition.
50- """
45+ def __init__ (
46+ self ,
47+ function : Callable [..., Component | VdomDict | str | None ],
48+ key : Any | None ,
49+ args : tuple [Any , ...],
50+ kwargs : dict [str , Any ],
51+ sig : inspect .Signature ,
52+ ) -> None :
53+ self .key = key
54+ self .type = function
55+ self ._args = args
56+ self ._kwargs = kwargs
57+ self ._sig = sig
58+
59+ def render (self ) -> Component | VdomDict | str | None :
60+ return self .type (* self ._args , ** self ._kwargs )
5161
52- def render (self ) -> VdomDict | ComponentType | str | None :
53- """Render the component's view model."""
62+ def __repr__ (self ) -> str :
63+ try :
64+ args = self ._sig .bind (* self ._args , ** self ._kwargs ).arguments
65+ except TypeError :
66+ return f"{ self .type .__name__ } (...)"
67+ else :
68+ items = ", " .join (f"{ k } ={ v !r} " for k , v in args .items ())
69+ if items :
70+ return f"{ self .type .__name__ } ({ id (self ):02x} , { items } )"
71+ else :
72+ return f"{ self .type .__name__ } ({ id (self ):02x} )"
5473
5574
5675_Render_co = TypeVar ("_Render_co" , covariant = True )
@@ -787,7 +806,7 @@ class VdomTypeDict(TypedDict):
787806
788807 tagName : str
789808 key : NotRequired [Key | None ]
790- children : NotRequired [Sequence [ComponentType | VdomChild ]]
809+ children : NotRequired [Sequence [Component | VdomChild ]]
791810 attributes : NotRequired [VdomAttributes ]
792811 eventHandlers : NotRequired [EventHandlerDict ]
793812 inlineJavaScript : NotRequired [InlineJavaScriptDict ]
@@ -815,7 +834,7 @@ def __getitem__(self, key: Literal["key"]) -> Key | None: ...
815834 @overload
816835 def __getitem__ (
817836 self , key : Literal ["children" ]
818- ) -> Sequence [ComponentType | VdomChild ]: ...
837+ ) -> Sequence [Component | VdomChild ]: ...
819838 @overload
820839 def __getitem__ (self , key : Literal ["attributes" ]) -> VdomAttributes : ...
821840 @overload
@@ -833,7 +852,7 @@ def __setitem__(self, key: Literal["tagName"], value: str) -> None: ...
833852 def __setitem__ (self , key : Literal ["key" ], value : Key | None ) -> None : ...
834853 @overload
835854 def __setitem__ (
836- self , key : Literal ["children" ], value : Sequence [ComponentType | VdomChild ]
855+ self , key : Literal ["children" ], value : Sequence [Component | VdomChild ]
837856 ) -> None : ...
838857 @overload
839858 def __setitem__ (
@@ -857,7 +876,7 @@ def __setitem__(self, key: VdomDictKeys, value: Any) -> None:
857876 super ().__setitem__ (key , value )
858877
859878
860- VdomChild : TypeAlias = ComponentType | VdomDict | str | None | Any
879+ VdomChild : TypeAlias = Component | VdomDict | str | None | Any
861880"""A single child element of a :class:`VdomDict`"""
862881
863882VdomChildren : TypeAlias = Sequence [VdomChild ] | VdomChild
@@ -994,7 +1013,7 @@ def __call__(
9941013 ) -> ContextProviderType [_Type ]: ...
9951014
9961015
997- class ContextProviderType (ComponentType , Protocol [_Type ]):
1016+ class ContextProviderType (Component , Protocol [_Type ]):
9981017 """A component which provides a context value to its children"""
9991018
10001019 type : Context [_Type ]
0 commit comments