Skip to content

Commit d5dd013

Browse files
committed
Add type checking
1 parent 44f76d2 commit d5dd013

File tree

4 files changed

+28
-12
lines changed

4 files changed

+28
-12
lines changed

.github/workflows/test-python.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,18 @@ jobs:
8080
run: pip install --upgrade pip hatch uv
8181
- name: Check Python formatting
8282
run: hatch fmt src tests --check
83+
84+
python-types:
85+
runs-on: ubuntu-latest
86+
steps:
87+
- uses: actions/checkout@v4
88+
- uses: oven-sh/setup-bun@v2
89+
with:
90+
bun-version: latest
91+
- uses: actions/setup-python@v5
92+
with:
93+
python-version: 3.x
94+
- name: Install Python Dependencies
95+
run: pip install --upgrade pip hatch uv
96+
- name: Run Python type checker
97+
run: hatch run python:type_check

docs/src/about/contributing.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ By utilizing `hatch`, the following commands are available to manage the develop
4343
| `hatch fmt --formatter` | Run only formatters |
4444
| `hatch run javascript:check` | Run the JavaScript linter/formatter |
4545
| `hatch run javascript:fix` | Run the JavaScript linter/formatter and write fixes to disk |
46+
| `hatch run python:type_check` | Run the Python type checker |
4647

4748
??? tip "Configure your IDE for linting"
4849

src/reactpy_router/routers.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
from typing import TYPE_CHECKING, Any, Literal, cast
88

99
from reactpy import component, use_memo, use_state
10-
from reactpy.backend.hooks import ConnectionContext, use_connection
1110
from reactpy.backend.types import Connection, Location
11+
from reactpy.core.hooks import ConnectionContext, use_connection
1212
from reactpy.types import ComponentType, VdomDict
1313

1414
from reactpy_router.components import FirstLoad, History
@@ -20,16 +20,16 @@
2020

2121
from reactpy.core.component import Component
2222

23-
from reactpy_router.types import CompiledRoute, Resolver, Router, RouteType
23+
from reactpy_router.types import CompiledRoute, Resolver, Route, Router
2424

2525
__all__ = ["browser_router", "create_router"]
2626
_logger = getLogger(__name__)
2727

2828

29-
def create_router(resolver: Resolver[RouteType]) -> Router[RouteType]:
29+
def create_router(resolver: Resolver[Route]) -> Router[Route]:
3030
"""A decorator that turns a resolver into a router"""
3131

32-
def wrapper(*routes: RouteType) -> Component:
32+
def wrapper(*routes: Route) -> Component:
3333
return router(*routes, resolver=resolver)
3434

3535
return wrapper
@@ -38,13 +38,13 @@ def wrapper(*routes: RouteType) -> Component:
3838
_starlette_router = create_router(StarletteResolver)
3939

4040

41-
def browser_router(*routes: RouteType) -> Component:
41+
def browser_router(*routes: Route) -> Component:
4242
"""This is the recommended router for all ReactPy-Router web projects.
4343
It uses the JavaScript [History API](https://developer.mozilla.org/en-US/docs/Web/API/History_API)
4444
to manage the history stack.
4545
4646
Args:
47-
*routes (RouteType): A list of routes to be rendered by the router.
47+
*routes (Route): A list of routes to be rendered by the router.
4848
4949
Returns:
5050
A router component that renders the given routes.
@@ -54,8 +54,8 @@ def browser_router(*routes: RouteType) -> Component:
5454

5555
@component
5656
def router(
57-
*routes: RouteType,
58-
resolver: Resolver[RouteType],
57+
*routes: Route,
58+
resolver: Resolver[Route],
5959
) -> VdomDict | None:
6060
"""A component that renders matching route(s) using the given resolver.
6161
@@ -110,7 +110,7 @@ def on_first_load(event: dict[str, Any]) -> None:
110110
return None
111111

112112

113-
def _iter_routes(routes: Sequence[RouteType]) -> Iterator[RouteType]:
113+
def _iter_routes(routes: Sequence[Route]) -> Iterator[Route]:
114114
for parent in routes:
115115
for child in _iter_routes(parent.routes):
116116
yield replace(child, path=parent.path + child.path) # type: ignore[misc]

src/reactpy_router/types.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@ def __hash__(self) -> int:
4646
return hash((self.path, key, self.routes))
4747

4848

49-
RouteType = TypeVar("RouteType", bound=Route)
50-
"""A type variable for `Route`."""
51-
5249
RouteType_contra = TypeVar("RouteType_contra", bound=Route, contravariant=True)
5350
"""A contravariant type variable for `Route`."""
5451

@@ -66,6 +63,7 @@ def __call__(self, *routes: RouteType_contra) -> Component:
6663
Returns:
6764
The resulting component after processing the routes.
6865
"""
66+
...
6967

7068

7169
class Resolver(Protocol[RouteType_contra]):
@@ -81,6 +79,7 @@ def __call__(self, route: RouteType_contra) -> CompiledRoute:
8179
Returns:
8280
The compiled route.
8381
"""
82+
...
8483

8584

8685
class CompiledRoute(Protocol):
@@ -104,6 +103,7 @@ def resolve(self, path: str) -> tuple[Any, dict[str, Any]] | None:
104103
Returns:
105104
A tuple containing the associated element and a dictionary of path parameters, or None if the path cannot be resolved.
106105
"""
106+
...
107107

108108

109109
class ConversionInfo(TypedDict):

0 commit comments

Comments
 (0)