55from dataclasses import dataclass , field
66from typing import Any , Callable , Sequence , TypedDict , TypeVar
77
8+ from reactpy .backend .types import Location
9+ from reactpy .core .component import Component
810from reactpy .core .vdom import is_vdom
9- from reactpy .types import ComponentType , Key
11+ from reactpy .types import Key
1012from typing_extensions import Protocol , Self , TypeAlias
1113
1214ConversionFunc : TypeAlias = Callable [[str ], Any ]
15+ """A function that converts a string to a specific type."""
16+
1317ConverterMapping : TypeAlias = dict [str , ConversionFunc ]
18+ """A mapping of conversion types to their respective functions."""
1419
1520
1621@dataclass (frozen = True )
1722class Route :
18- """A route that can be matched against a path."""
23+ """
24+ A class representing a route that can be matched against a path.
1925
20- path : str
21- """The path to match against."""
26+ Attributes:
27+ path (str): The path to match against.
28+ element (Any): The element to render if the path matches.
29+ routes (Sequence[Self]): Child routes.
2230
23- element : Any = field (hash = False )
24- """The element to render if the path matches."""
31+ Methods:
32+ __hash__() -> int: Returns a hash value for the route based on its path, element, and child routes.
33+ """
2534
35+ path : str
36+ element : Any = field (hash = False )
2637 routes : Sequence [Self ]
27- """Child routes."""
2838
2939 def __hash__ (self ) -> int :
3040 el = self .element
@@ -33,36 +43,87 @@ def __hash__(self) -> int:
3343
3444
3545RouteType = TypeVar ("RouteType" , bound = Route )
46+ """A type variable for `Route`."""
47+
3648RouteType_contra = TypeVar ("RouteType_contra" , bound = Route , contravariant = True )
49+ """A contravariant type variable for `Route`."""
3750
3851
3952class Router (Protocol [RouteType_contra ]):
40- """Return a component that renders the first matching route."""
53+ """Return a component that renders the matching route(s)."""
54+
55+ def __call__ (self , * routes : RouteType_contra ) -> Component :
56+ """
57+ Process the given routes and return a component that renders the matching route(s).
4158
42- def __call__ (self , * routes : RouteType_contra ) -> ComponentType : ...
59+ Args:
60+ *routes: A variable number of route arguments.
61+
62+ Returns:
63+ The resulting component after processing the routes.
64+ """
4365
4466
4567class Resolver (Protocol [RouteType_contra ]):
4668 """Compile a route into a resolver that can be matched against a given path."""
4769
48- def __call__ (self , route : RouteType_contra ) -> CompiledRoute : ...
70+ def __call__ (self , route : RouteType_contra ) -> CompiledRoute :
71+ """
72+ Compile a route into a resolver that can be matched against a given path.
73+
74+ Args:
75+ route: The route to compile.
76+
77+ Returns:
78+ The compiled route.
79+ """
4980
5081
5182class CompiledRoute (Protocol ):
52- """A compiled route that can be matched against a path."""
83+ """
84+ A protocol for a compiled route that can be matched against a path.
85+
86+ Attributes:
87+ key (Key): A property that uniquely identifies this resolver.
88+ """
5389
5490 @property
55- def key (self ) -> Key :
56- """Uniquely identified this resolver."""
91+ def key (self ) -> Key : ...
5792
5893 def resolve (self , path : str ) -> tuple [Any , dict [str , Any ]] | None :
59- """Return the path's associated element and path parameters or None."""
94+ """
95+ Return the path's associated element and path parameters or None.
96+
97+ Args:
98+ path (str): The path to resolve.
99+
100+ Returns:
101+ A tuple containing the associated element and a dictionary of path parameters, or None if the path cannot be resolved.
102+ """
60103
61104
62105class ConversionInfo (TypedDict ):
63- """Information about a conversion type."""
106+ """
107+ A TypedDict that holds information about a conversion type.
108+
109+ Attributes:
110+ regex (str): The regex to match the conversion type.
111+ func (ConversionFunc): The function to convert the matched string to the expected type.
112+ """
64113
65114 regex : str
66- """The regex to match the conversion type."""
67115 func : ConversionFunc
68- """The function to convert the matched string to the expected type."""
116+
117+
118+ @dataclass
119+ class RouteState :
120+ """
121+ Represents the state of a route in the application.
122+
123+ Attributes:
124+ set_location: A callable to set the location.
125+ params: A dictionary containing route parameters.
126+ """
127+
128+ set_location : Callable [[Location ], None ]
129+ params : dict [str , Any ]
0 commit comments