Skip to content

feat: check component input types at render time - #208

Merged
JuroOravec merged 7 commits into
mainfrom
feat/runtime-input-types
Oct 20, 2026
Merged

JuroOravec merged 7 commits into
mainfrom
feat/runtime-input-types

Conversation

@JuroOravec

@JuroOravec JuroOravec commented Oct 20, 2026 •

Copy link
Copy Markdown
Member

Closes one of the gaps the error-prevention benchmark found (#207, #158): Card(title=123) against title: str used to render 123 with no error.

Release together with citry-ui. Published citry-ui 0.3.0 accepts any citry>=0.6.0, and some of its annotations are narrower than the values its components accept (for example heading_level="2" against Literal[2, ..., 6]). This branch opts every citry_ui component out of the check, but an older citry-ui with this Citry would fail. Both changelogs say to upgrade together; the release order or a version constraint is tracked in #209.

What changes

Citry now checks the outer type of each component input against its annotation on every render, in every mode:

Card.Kwargs.title expects str, got int (123). Pass a value of the
expected type, or change the annotation to accept int.
  • It covers Kwargs on every render path, TemplateData/JsData/CssData, and State built from kwargs or state_data(). It does not cover State rebuilt from an event token, Pydantic models, dataclasses with their own __init__, or Slots.
  • It checks only the outer type (list, not its items), and skips Any, object, type variables, plain Protocols and names imported only for type checking.
  • To turn it off, use Citry(lint=LintSettings(check_input_types=False)) for the whole app, or check_input_types = False in a component's class Lint:. True in a component's Lint turns it back on when the app turns it off. Citry reads the component's choice once, when the class is defined.

Edge cases, each documented:

  • Django lazy strings that wrap a str are accepted for str.
  • Decimal is rejected for float; annotate float | Decimal to accept both.
  • A field's own default always passes, so x: str = None keeps working.
  • int accepts bool and numpy integers.

A related fix: event handlers whose State has a tuple field no longer fail with a 500 after JSON turns the tuple into a list.

Cost

The budget was under 1% render regression on the Kwargs-heavy benchmark scenario. Measured against origin/main in alternating fresh processes, the median was +0.17% (12 pairs). One check costs about 300 ns; about 29 ns with the check off.

Checks

  • Every non-Rust phase of the fast profile passed. The Cargo phases could not run in this worktree, and there is no Rust change.
  • mypy --platform linux passed. docs tests: 1,050 passed. The docs build had 0 errors and 0 warnings.
  • The check found and fixed several real mistakes in existing code: a string size, an int passed where a string was expected, and a list passed for a bool.
  • An adversarial reviewer with a prose pass and a docs cold reader reviewed the change; their findings are folded in.

🤖 Generated with Claude Code

JuroOravec and others added 2 commits October 20, 2026 13:42
When a component renders, Citry now checks each Kwargs, TemplateData,
JsData, CssData, and render-time State field against its annotation and
raises TypeError for a value of the wrong outer type, in every mode.
Citry(check_input_types=False) and Component.check_input_types turn the
check off. Annotations resolve in the module of the class that declares
them; names that cannot be evaluated, Any, TypeVar, and non-runtime
Protocols stay unchecked. A field's own default always passes, int
accepts bool and registered integers, float accepts real numbers but not
Decimal, and str accepts Django lazy text.

The check found real type mismatches in the benchmark scenario, the docs
search modal, and two tests; those are fixed. The built-in <c-cache> and
the Citry UI components keep their own, more specific input errors by
turning the general check off.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- Event calls turn a JSON list back into a tuple for State fields
  annotated as a tuple, so a handler that renders from its State no
  longer fails the Kwargs check (and handlers see the declared type).
- <c-i18n> and <c-mark> keep their own tag-named input errors, like
  <c-cache>.
- The error message writes None as None, adds the module when two
  classes share a name, asks for an allowed value for Literal and
  type[...], and suggests c-<name> when a template attribute passed text.
- Django lazy text passes for str only when it wraps a str; a
  subscripted generic type alias checks the type it stands for.
- Docs: reorder the value-type section so the common fixes come first,
  correct the default and skipped-annotation rules, update the
  django-components migration guide (DJC-072, new DJC-096), and state in
  both changelogs that Citry UI must be upgraded together with Citry.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
JuroOravec and others added 2 commits October 20, 2026 15:06
On Python 3.10 a parameterized generic such as `tuple[str, ...]` passes
`isinstance(hint, type)`, and isinstance() and issubclass() then reject it.
A browser update to a State field annotated that way failed with a 500, and
a handler's `list[...]` return annotation could raise while documenting the
API. The State update check now accepts a JSON list for a tuple field and
tells generics apart by their origin; the OpenAPI data check does the same.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Dialog and Form pass None when the caller chose no color or variant, and
get_styling_css() treats None as the default styling, but Button's
annotations did not allow None. The input type check rejected the
benchmark dialog's render in the e2e suite.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
JuroOravec and others added 2 commits October 20, 2026 18:33
Replace the app-wide Citry(check_input_types=...) argument with a
check_input_types field on LintSettings, and the per-component class
attribute with check_input_types in the nested Lint class, which combines
across bases like the other Lint settings. ComponentMeta resolves the
component's choice once per class while validating Lint, so renders read
one stored value. Citry UI components and the built-in mark, cache, and
i18n components opt out through their Lint class.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Replace the structural Citry UI test with one that installs the library and
renders an input its annotation does not name, document when Lint's
check_input_types is read, use one TypeError wording for both settings,
and cover TemplateData, State, base-order precedence, per-app library
installs, and simple components. Reword the Citry UI opt-out comment and
the affected docs and changelog entries.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts:
#	CHANGELOG.md
#	packages/py/citry/citry/_linting.py
#	packages/py/citry/citry/settings.py
@JuroOravec
JuroOravec merged commit 90f7e31 into main Oct 20, 2026
29 checks passed
@JuroOravec
JuroOravec deleted the feat/runtime-input-types branch October 20, 2026 21:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant