Repository navigation
feat(events): experimental, typed _render_inputs option for Render actions - #800
Merged
Merged
Conversation
…actions A Render from an event handler had no template globals and no provided values, so a template that reads a request value (the user, the locale, Django's request) failed on re-render. The new engine-wide extensions_defaults["events"]["_render_inputs"] function takes the call's EventRequest and returns the template_globals and provides that Citry passes to every component it renders for that call, on the HTML, Vue and <c-mark> paths. It runs after the handler, so after the CSRF and State token checks, and a render the handler built itself is left alone. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01KUhrHaXagmW5ZxpRAMwyom
Ask for the render inputs only after the renderer check, test the no-JavaScript answer and an extension's on_event answer, correct the docs claim about which hosts call the function on the event loop, and move and rewrite the docs section for first-time readers. The versioning page now names _render_inputs as an exception to the "Other _ names" rule. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01KUhrHaXagmW5ZxpRAMwyom
The function now returns a RenderInputs TypedDict whose fields reuse the types of CitryElement.render()'s template_globals and provides (shared aliases in citry_element.py), and the typed Events base declares _render_inputs as Callable[[EventRequest], RenderInputs] | None for type checkers. RenderInputs is exported from citry.ext.events. _render_inputs is now an ordinary per-component Events option: a component's Events class may assign one, which wins over extensions_defaults["events"], and None turns the app-wide function off. The calling component's value applies to every Render in the call. A method written inside the Events class is refused at class definition, because Citry calls the function with the request alone. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01KUhrHaXagmW5ZxpRAMwyom
…s in the reference Refuse a _render_inputs value that cannot take exactly one argument (such as a method written in the Events class) at Citry(...) or class definition, and accept a lambda or staticmethod in the class body. Add RenderInputs to the Events reference page, correct the docs' error rule for the app-wide value, and note that mypy checks the return type and the number of parameters but not the parameter type. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01KUhrHaXagmW5ZxpRAMwyom
(Opus 5.5) Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01KUhrHaXagmW5ZxpRAMwyom
This was referenced Nov 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
(Opus 5.5)
Summary
When an event handler returns a component, Citry renders it again with no template globals and no provided values. A template that reads a request value, such as the current user, Django's
request, a context processor value, or the locale, fails the call. This adds an Experimental Events option,_render_inputs, that supplies those values.What the function returns: the keyword arguments Citry passes to
.render()for each component aRenderre-renders. These are the sametemplate_globals=andprovides=you would pass when rendering the component yourself. When the function returns{"template_globals": {"user": user}, "provides": {"locale": user.locale}}Citry renders the returned component as if you had written
Typed: the return value is
RenderInputs, aTypedDictexported fromcitry.ext.events. Its two optional fields reuse the types of therender()parameters of the same name: shared aliases incitry/citry_element.py, whichrender()'s own signature also uses. The function type isCallable[[EventRequest], RenderInputs] | None, declared for type checkers on the typedcitry.Eventsbase.Where to set it. You can set it for the whole app, or on one component's nested
Eventsclass:Precedence: a component's own value wins over
extensions_defaults["events"], and_render_inputs = Noneon a component turns the app-wide function off. This is the same three-level resolution as_contextand_csrf(component, thenextensions_defaults, then the built-in default, which isNone). Citry uses the value of the component whose handler ran, for everyRenderin that call, including a different component the handler returns.When it runs: once per event call, after the handler, and only when Citry renders a
CitryElementfor aRender. That covers the HTML fragment, the Vue prepared render,<c-mark>targets on both renderers, and the no-JavaScript answer. A render the handler built itself (aCitryRender) keeps its own inputs.Error modes:
None, anasync deffunction, or a function that cannot take exactly one argument raisesValueError. That includes a method written inside theEventsclass, because a method also expectsself. The app-wide value fails atCitry(...)and a component's value at class definition.template_globals/provides(each a mapping orNone), fails the call withhandler_error. TheTypeErrornames the setting.EventErrorfrom the function answers with its own status.This is change E1 of the citry-django redesign. citry-django will use it to give event re-renders
request, context processor values and the locale.Plan (Mechanism 2)
Prior art
CitryElement.render(template_globals=, provides=)(citry/citry_element.py:143) andrender_prepared_direct(..., template_globals=, provides=)(citry/_vue/capture.py:304) already accept the inputs. The Events encoders called them with no arguments:ext/events/results.py:102(HTML),_vue/events.py:663(Vue), and_vue/capture.py:335(the<c-mark>payload).EventsExtension._resolve_config_value(ext/events/extension.py, component, thenextensions_defaults, then_FACTORY_DEFAULTS) in_compute_info, the same path_guard,_contextand_csrftake._csp_nonce(ext/events/handlers.pyENGINE_CONFIG_NAMES,dispatcher.py_page_csp_nonce) is the model for a per-call function carried onRenderEncodingContext(ext/events/results.py:72-85).Citry.template_globals(citry/settings.py:874) is engine-wide but fixed, so it cannot hold per-request values. No existing hook supplies render inputs for Events._options are listed in the versioning page: covered options atversioning.md:215-224, Experimental list at:290-320.Chosen design
_render_inputsis an ordinary per-component Events option, resolved once at class definition intoEventsInfo.render_inputs._call_render_inputsin_complete_callcalls the calling component's function after CSRF, the State token check, the handler and the renderer check.RenderEncodingContextfields carry the result to every encoder'srender()call.TypedDictthat reusesrender()'s parameter types._options, and the "Other_names" bullet names it as an exception.Alternatives considered
_csp_nonce). Rejected after maintainer review: a component, or a library of components, may need its own inputs. The per-component form costs nothing on the call path, because it is resolved at class definition.Eventsinstance (state, context, request) instead ofEventRequest. Rejected: it gives the function handler-specific inputs, which would make it a second handler..render(template_globals=..., provides=...)itself. It works today and stays supported, because such a render is left alone. Rejected as the main path because every handler would repeat it.on_event_resulthook. Rejected: actions are already encoded by then.What would show the design is wrong:
RenderEncodingContext, such as a future encoder orencode_actions(render_context=None)reached from production code;Security (Mechanism 7)
Places touched: text and markup (
{{ }}of globals), JavaScript and JSON (Vue prepared data, fragment JSON), translated messages (acitry_i18nprovide sets the locale), the CSP nonce (unchanged, still from_csp_nonce).docs/design/trust_boundaries.mdhas a row in section 5.CitryElement.render()torender_impl, the same path as anyrender(template_globals=, provides=). They get the same sandbox, escaping and serialization. Provide keys still passvalidate_provide_key._csrfpolicy, State token verification,_guard, and Django's CSRF middleware all run first. Tests cover a bad State token, and a missingX-Citry-Eventsheader on WSGI, ASGI and Django.<c-mark>on both renderers, no-JS answer,citry_i18nlocale, nonce) and found each safe or author-only by design. It also checked the Mechanism 8 advisory survey (classesxss-escape,ssr-serialize,sanitizer-bypass,i18n-locale,csrf,island,xreq-state,cache-xuser). Thecache-xuserfinding led to aCache.vary()note in the docs. The follow-up review of the per-component change did not reopen the survey. That change alters only which function is chosen, not how the values reach the page.Versioning
RenderInputs). It breaks no covered behavior, so it needs a minor release (citry 1.1.0)._render_inputs. Error text is not covered.citry-corechange.Benchmark
This is a focused timing of one stateful
Rendercall throughEventsDispatcher.dispatch(a component with a 20-row loop). There is no Events render benchmark underbenchmarks/. Threshold: flag more than 3% on the default path (no function set). Budget: about 10 minutes. The runs were repeated after the lookup moved from the settings dict toEventsInfo: 10 interleaved before/after rounds, each the median of 15 x 300 calls, on macOS with Python 3.13. The machine had load bursts on both sides (about 600 vs 645 us), so the table shows the median of the round medians and the best minimum:html-fragment/1, median of medianshtml-fragment/1, best minimumvue-prepared/1, median of mediansvue-prepared/1, best minimumAll four figures are below the 3% threshold. Setting a function made no measurable difference in the first round of runs.
Tests
test_events_dispatch.py::TestRenderInputs(30 tests) covers:<c-mark>renders, the no-JS answer, and anon_eventanswer all get the inputs;EventRequestand runs once per call;Noneturning the app-wide value off, the calling component's function applying to a different returned component, a lambda or staticmethod in the class body, and a method refused at class definition.test_events_host_parity.py(6 tests): WSGI, ASGI and Django each apply it, and none calls it before a failing CSRF check.test_events_typing.py: checked by mypy and pyright in the gate.Noneare accepted, andelement.render(**inputs)proves theTypedDictkeys and types matchrender().Callableclass attribute as a method, so it does not.test_events.py: the recognized-names error text.ruff checkandruff format --check(ruff 0.14.13, the locked version),mypyboth natively and with--platform linux, andpyrighton the typing test: all clean.test_events*.py, Vue Events and markers, Django and request adapters,test_provide.py,test_extension.py,test_render.py): 2839 passed, 2 skipped, 1 xfailed, 1 failed.test_events_pydantic_bounded.py::test_a_state_write_with_an_alias_key_under_a_forbidding_model_takes_the_json_answer, also fails on unchangedmainin this venv (citry-core 2.1.2 from PyPI, not the lockfile environment).scripts/check.py, and the docs build checks (python -m docs_site build-check), which need the docs extras.RenderInputsis added todocs_site/reference.yml, so thecrossrefguard can resolve its link.Review
docs-cold-reader. Their findings are folded in: the call ordering, the no-JS andon_eventtests, the async-host claim, theCache.vary()note, and the docs restructure..render()statement, per-component form): an independent technical and prose review. Folded in:RenderInputsadded to the reference page;🤖 Generated with Claude Code
https://claude.ai/code/session_01KUhrHaXagmW5ZxpRAMwyom