Typing¶
Wybthon requires Python 3.14. Its accessors, components, actions, and stores carry type information that both pyright (including Pylance) and mypy understand with no plugin or extra configuration.
Component props¶
A component declares its inputs as ordinary, annotated parameters. To a type checker, an @component is the function itself, returning a Node, so pyright, Pylance, and mypy check every call against the function's own signature, with no plugin:
from collections.abc import Callable
from wybthon import Node, component, create_signal, html, prop
@component
def Greeting(
name: str, copies: int = 1, tags: list[str] = prop(default_factory=list), on_wave: Callable[[], None] | None = None
) -> Node:
def wave() -> None:
if on_wave is not None:
on_wave()
return html(t"<p onclick={wave}>{name * copies}</p>")
name, set_name = create_signal("Ada")
Greeting(name="Ada")
Greeting(name="Ada", copies=2, on_wave=lambda: print("hi"))
html(t"<div>{Greeting(name=name(), copies=2)}</div>")
- Inside the component, a parameter has its annotated type:
nameis astr, andcopiesis anint. The compiler makes each read a tracked read of the parent's current value, so{name * copies}updates when either changes. - A caller passes values of the declared types. In a template,
{Greeting(name=name())}type-checks asname: str, and the compiler still passes a getter, so the prop stays reactive. In Python code outside a template,Greeting(name=name)passes the accessor itself, which works at run time (the read unwraps it) but is a type error, because anAccessor[str]isn't astr. - A missing required prop, a value of the wrong type, and an unknown keyword are all type errors, and the messages name the component:
Greeting(name=42) # incompatible type
Greeting() # missing "name"
Greeting(name="Ada", typo=1) # unexpected keyword "typo"
mypy reports the last two as Missing positional argument "name" in call to "Greeting" and Unexpected keyword argument "typo" for "Greeting". Positional arguments bind to parameters as in any call, so Greeting("Ada") is checked as Greeting(name="Ada").
At run time, dev mode raises TypeError for the same unknown and missing props, so code that isn't type checked still fails loudly.
Callable parameters¶
A parameter annotated as callable, with Callable[...] or Accessor[T], alone or in a union with None, reads as exactly what the parent passed. That's how callbacks work: reading on_wave never calls it, so on_wave() calls the parent's function. Every other parameter unwraps an accessor the parent passes, so a parameter that should hold a function must be annotated as one.
Annotate a parameter Accessor[T] when the component needs the accessor itself, for example to hand it to For:
from wybthon import Accessor, For, Node, component, html
def tag(item: str, index: Accessor[int]) -> Node:
return html(t"<li>{item}</li>")
@component
def TagList(items: Accessor[list[str]]) -> Node:
return html(t"<ul>{For(items, tag)}</ul>") # items is the accessor itself
Reading a callable parameter returns what the parent passed without calling it, so reading it once at the top of the body freezes nothing. The parent passes the accessor by name, {TagList(items=tags)}; an expression such as tags() would hand the component a getter, which every read unwraps, even for a callable parameter.
Defaults¶
Defaults are ordinary parameter defaults (copies: int = 1, on_wave: ... = None). Use prop(default_factory=list) for a mutable value; the factory runs once per component instance. prop() is typed to return the factory's result, so tags: list[str] = prop(default_factory=list) checks.
Children¶
Declare a children parameter, annotated with Children, to accept content. Children is an alias of Any, for documentation. Both the children keyword and item syntax are checked:
from wybthon import Children, Node, component, html
@component
def Card(title: str, children: Children = None) -> Node:
return html(t"<div><h2>{title}</h2>{children}</div>")
Card(title="Hi")[html(t"<p>a</p>"), html(t"<p>b</p>")]
Card(title="Hi", children=html(t"<p>a</p><p>b</p>"))
Positional arguments aren't children: they bind to parameters in order, so Card(html(t"<p>a</p>")) passes the markup as title and is reported as an incompatible type. Item syntax on a call type-checks through Node.__getitem__.
Forwarded props¶
A **rest parameter collects the props a component doesn't declare. Annotate it **attrs: Any: a checker then accepts any extra keyword, as the component does at run time, while the declared parameters stay checked.
What a component call returns¶
@component is typed to return the function it decorates with its return type replaced by Node, the type of anything that renders. That's how the checker validates the arguments, and it's why a component call can be returned from a function annotated -> Node, kept in a list[Node], or passed as children. At run time the call returns a VNode, which is a Node too. A component with no parameters takes no arguments.
Under mypy --strict, annotate component functions with -> Node; html returns a Node as well.
Templates¶
A template's interpolations are ordinary Python expressions, so the checker sees them like any other code, before the compiler turns the dynamic ones into getters. What's checked depends on where a value appears:
| In a template | Checked |
|---|---|
{expression} in any position |
Statically, as ordinary code: an undefined name, a wrong attribute, or a bad call is reported. |
{Card(title=user().name)} |
Statically, with full prop checking, like any component call. The checker sees user().name as a str; the compiler passes a getter. |
<{Card} title={user().name}>...</{Card}> |
The expression user().name is checked as code. A plain checker run doesn't check it against Card's signature, but wyb check does. Without it, an unknown or missing prop raises TypeError at run time, in dev mode, when the template renders. |
| The HTML markup itself | When the module is compiled: markup the parser would rewrite is a SyntaxError with the file and line (or a TemplateError for a template compiled at run time). |
Type checkers can't see inside the template's static text, so on their own they don't check a component tag's attributes against its parameters. Run them through wyb check (below) to check tags too, or use the call form inside an interpolation; use the tag form when passing nested markup as children reads better.
html() is annotated to take a string.templatelib.Template, so passing a plain string is a type error, and it raises TypeError at run time:
Checking tag props with wyb check¶
wyb check runs mypy or pyright on your project and checks every component tag's props as if the tag were a call:
For each html(t"...") call, it checks a call such as Card(title=user().name, children=...) per component tag, with each attribute as a keyword: a static value as its string, a bare attribute as True, an {expression} as itself, an attribute that mixes text and interpolations as a str, and a spread as **expression. Content between the tags is passed as children, so a component with no children parameter (and no **rest parameter) is reported. Each error in a tag is reported at the tag's line, among the checker's other errors:
app/pages.py:12: error: in <{Card}>: Unexpected keyword argument "bogus" for "Card" [call-arg]
app/pages.py:14: error: in <{Card}>: Missing positional argument "title" in call to "Card" [call-arg]
The command exits with status 1 when there are errors and 2 when the checker isn't installed. It reads your project's checker configuration (mypy.ini, setup.cfg, pyproject.toml, or pyrightconfig.json in the current directory), and it skips .venv, dist, build, and hidden directories. Under mypy's defaults, the body of an unannotated function isn't checked, so neither are the tags in it.
A few tags aren't checked as calls: built-ins imported from Wybthon (Show, For, Loading, Portal, Link, and the rest), contexts made with create_context, and a plain function in the same module that takes the props dictionary instead of keywords.
Control flow¶
For and Show have generic overloads, so their callbacks are checked against the data you pass. With For, the keyed strategy decides which arguments are accessors:
| Call | Callback arguments |
|---|---|
For(items, fn) (the default, keyed=True) |
(item: T, index: Accessor[int]) |
For(items, fn, keyed=False) |
(item: Accessor[T], index: int) |
For(items, fn, keyed=lambda item: ...) |
(item: Accessor[T], index: Accessor[int]) |
from typing import TypedDict
from wybthon import Accessor, For, Node, create_signal, html
class Todo(TypedDict):
id: int
title: str
todos, set_todos = create_signal([Todo(id=1, title="Write docs")])
def row(todo: Todo, index: Accessor[int]) -> Node:
return html(t"<li>{todo['title']}</li>")
def slot(todo: Accessor[Todo], index: int) -> Node:
return html(t"<li>{index}: {todo()['title']}</li>")
For(todos, row)
For(todos, slot, keyed=False)
For(todos, lambda todo, i: html(t"<li>{todo['title']}</li>")) # todo: Todo
For(todos, lambda todo, i: html(t"<li>{todo['titel']}</li>")) # error: no key "titel"
html(t"<ul>{For(todos(), row)}</ul>") # in a template, the compiler passes a getter
A lambda's parameters are inferred from the overload, so the fourth call is reported even though nothing is annotated. In a template, For(todos(), row) checks the same way: the checker sees a list[Todo].
Show passes the truthy value to a one-argument callback as an Accessor, or as the value itself with keyed=True. Either way, None is narrowed away, because the branch only renders when the value is truthy:
from typing import TypedDict
from wybthon import Show, create_memo, create_signal, html
class User(TypedDict):
name: str
users, set_users = create_signal([User(name="Ada")])
user = create_memo(lambda: next(iter(users()), None)) # Memo[User | None]
Show(user, lambda u: html(t"<p>{u()['name']}</p>")) # u: Accessor[User]
Show(user, lambda u: html(t"<p>{u['name']}</p>"), keyed=True) # u: User
Show(user, html(t"<p>Signed in</p>"), fallback=html(t"<p>Sign in</p>"))
The callback's type comes from when, so pass the accessor itself (Show(user, ...)) when the callback takes the value. {Show(user(), panel)} works the same at run time, but a checker sees a plain User | None there and accepts only a zero-argument callback or a node.
Repeat's callback receives an int index. Switch and Match take their children untyped.
Accessors, setters, and memos¶
Accessor[T]is readable: call it for a tracked read, or.peek()for an untracked one.Setter[T]accepts a value or an updater functionT -> T.Memo[T]is a disposable derived accessor. Anasync defpassed tocreate_memoproduces aMemoof the awaited type.
from wybthon import Accessor, Memo, create_memo, create_signal
count, set_count = create_signal(0)
doubled: Memo[int] = create_memo(lambda: count() * 2)
reader: Accessor[int] = count
set_count(lambda n: n + 1)
Actions¶
An @action keeps its parameters and result type. An async action returns an asyncio.Future[T], which supports cancellation, and the action object has .pending().
from wybthon import action
@action
def save(name: str) -> int:
return len(name)
length: int = save("Ada")
Stores¶
A store is typed as its own data. create_store(initial) returns tuple[T, StoreSetter[T]], where T is the type of initial: a dataclass, a TypedDict, list[Row], or any other type you give it. Reads check against your own annotations, with no checker plugin:
from dataclasses import dataclass, field
from typing import assert_type
from wybthon import create_store, deep, reconcile, snapshot
@dataclass
class Task:
title: str
done: bool = False
def toggle(self) -> None:
self.done = not self.done
@dataclass
class Board:
tasks: list[Task] = field(default_factory=list)
board, edit = create_store(Board([Task("Write docs")]))
assert_type(board, Board)
assert_type(board.tasks[0].done, bool)
def finish(draft: Board) -> None:
draft.tasks[0].toggle()
draft.tasks.append(Task("Ship it"))
edit(finish)
edit(Board())
edit(reconcile(Board([Task("Review")]), key="title"))
assert_type(snapshot(board), Board)
assert_type(deep(board.tasks), list[Task])
The setter accepts a Callable[[T], object] (a draft callback), replacement data of type T, or a reconcile result, so a draft callback is checked like any function taking a Board. snapshot and deep return the type they're given, and create_projection(fn, initial) is typed as initial's type.
For a dict store, give the initial data a TypedDict type. Read and write it with subscripts, since dict stores have no attribute access:
from typing import TypedDict, assert_type
from wybthon import create_store
class Person(TypedDict):
name: str
age: int
initial: Person = {"name": "Ada", "age": 36}
person, write = create_store(initial)
assert_type(person, Person)
assert_type(person["age"], int)
write(lambda draft: draft.update({"name": "Grace"}))
An empty list store takes its item type from the constructor: create_store(list[Person]()). The static type is your data's type, but the runtime view is a store: a dict store is a Store (a Mapping, not a dict), a list store is a StoreList (a Sequence), and a dataclass store is a StoreObject that isinstance reports as your dataclass. A TypedDict annotation isn't runtime validation.
Running the checkers¶
No plugin is needed. A minimal configuration:
[tool.mypy]
python_version = "3.14"
strict = true
[tool.pyright]
pythonVersion = "3.14"
typeCheckingMode = "strict"
The repository's tests/typing fixtures check valid calls and expected errors (missing, mistyped, and unknown props, an accessor passed where a value is declared, a mistyped component call inside a template, a mistyped For row, and a plain string passed to html()) as part of the unit test gate.
Next steps¶
- Read Components for the full props model, and Templates for template syntax.
- See Authoring patterns for callbacks, children, and forwarding attributes.
- Browse the
componentAPI forChildrenandprop, and The compiler for what happens to parameters and template expressions.