Typing
Typing¶
The codebase uses type hints and aims for clarity over cleverness.
Guidelines:
- Prefer precise types on public APIs; avoid
Any. - Use
Optional[...]where values can be absent. - In Pyodide/browser interop, tolerate
Anyat boundaries but narrow types internally.
mypy configuration¶
- Config is in
mypy.initargeting Python 3.9. Browser shims (e.g.,js,pyodide) are marked asignore_missing_imports. - Run mypy against
src,tests,examples,benchper config.
Examples¶
- Reactivity API
create_signal(value: T, *, equals: Any = ...) -> tuple[Callable[[], T], Callable[[T], None]].equalsis optional: default value equality (==) with identity fast-path,Trueis equivalent,Falseto always notify, or(old, new) -> bool"same value" predicate (useequals=lambda a, b: a is bfor SolidJS-style identity-only semantics).create_memo(fn: Callable[[], T]) -> Callable[[], T]create_effect(fn: Callable[[], Any]) -> Computationcreate_resource(fetcher: Callable[..., Awaitable[R]]) -> Resource[R]
from typing import Awaitable
from wybthon import create_signal, create_memo, create_effect, create_resource
count, set_count = create_signal(0)
double = create_memo(lambda: count() * 2)
def log() -> None:
_ = double() # subscribes
c = create_effect(log) # Computation
c.dispose() # stop updates
async def fetch_user() -> dict:
return {"name": "Ada"}
res = create_resource(cast(Callable[[], Awaitable[dict]], fetch_user))
- Component typing
- The
@componentdecorator binds each parameter to a reactive accessor:Callable[[], T]. Annotate the parameter with the underlying value type; Wybthon's machinery handles the wrapping.
from wybthon import VNode, component, h2
@component
def Hello(name: str = "world") -> VNode:
# ``name`` is a Callable[[], str] at runtime; type it as ``str`` for
# readability of the public API.
return h2("Hello, ", name, "!")
- Router types
Route(path: str, component: Union[Callable[[Dict[str, Any]], VNode], type])navigate(path: str, replace: bool = False) -> None
from wybthon import Route, component, h2
@component
def HomePage():
return h2("Home")
routes = [
Route(path="/", component=HomePage),
]
Next steps¶
- See the Style guide for the project's docstring conventions.
- Browse the
reactivityAPI for type-annotated public symbols. - Read the Contributing guide for the local lint/typecheck workflow.