Skip to content

feat: let a component list its template_data() keys for citry check - #423

Merged
JuroOravec merged 1 commit into
mainfrom
feat/lint-template-data-keys
Oct 25, 2026
Merged

JuroOravec merged 1 commit into
mainfrom
feat/lint-template-data-keys

Conversation

@JuroOravec

Copy link
Copy Markdown
Member

Fixes #149. This is the second PR for it. The first, #402, made citry check leave out findings in registered libraries. This one adds the option the maintainer approved, so that citry check --include-libraries can check Citry UI's own templates.

What changes

Citry reads template_data() to learn which names a template can use. When the method builds its result through a helper, Citry can't tell a typo from a name the helper adds. It then reports every name in the template as "not declared. Citry could not determine whether it is supplied dynamically". About 60 Citry UI templates were in that state, and they produced about 1,500 errors with --include-libraries.

A component can now list its keys:

class CAvatar(LibraryComponent):
    class Lint:
        template_data_keys = ("alt", "attrs", "img_attrs", "src", ...)

    def template_data(self, kwargs, slots):
        data = self._normalized(kwargs)
        data.update({...})
        return data
  • It's check-only. Only citry check and the editor read the list; rendering ignores it.
  • What it accepts: the template may use the listed keys, the keys Citry can read from the method, and the declared variables. A typo gets the "not available in this template" message, as it would for a literal dict. The editor's completion offers the listed keys.
  • The drift warning. A key the method writes as a literal, such as {"title": ...} or data["title"] = ..., and the list leaves out gets a warning in citry check. It's the new code citry.template.undeclared-template-data-key.
  • Source analysis: it now also keeps the literal keys set on a helper's result, as in data = build(kwargs); data["x"] = .... It still treats that result as possibly holding more keys.
  • Error modes, checked when the class is defined:
    • a value that isn't a tuple, list or frozenset of strings raises TypeError;
    • a name that can't be a Python variable name, such as "class", or a name listed twice, raises ValueError;
    • a class that declares both the list and a TemplateData schema raises TypeError.
  • Inheritance: a subclass's list replaces its base's, and template_data_keys = None clears an inherited list. A TemplateData schema on a subclass replaces an inherited list too, so an app's subclass of a Citry UI component can still describe its data with a schema.

Citry UI

The 60 Citry UI components with this problem now declare their keys. How I got the lists:

  1. I recorded the keys each component's template_data() returned across the whole citry_ui test suite.
  2. I compared them with the names their templates use. Every name was among them, so this pass turned up no real template bug.

citry check --include-libraries on an app that registers only Citry UI now reports no unknown-variable errors and no undeclared-key warnings. Two existing citry.js-data.unsupported-type warnings remain, for the serverDefaults of CCascader and CInfiniteScroll; they're out of scope.

Each Lint class sits after the component's input schemas, as the Citry UI guide asks.

Docs and downstream

  • User docs:
    • docs_site/content/ide/template-linting.md: the new "List template data keys" section;
    • the Component.Lint docstring.
  • Diagnostics catalog:
    • the new catalog entry, plus the unknown-variable fix text, which now points to the list;
    • _diagnostic_catalog.py, regenerated.
  • Design and contributor docs:
    • docs/design/template_linting.md: namespace policy, resolution order and error modes;
    • packages/py/citry_ui/docs/component-authoring.md: when to declare the list and how to keep it current.
  • Changelogs: CHANGELOG.md and packages/protocol/diagnostics/CHANGELOG.md.
  • Editor wire record: TemplateLintInfo carries the list to the language server. The field is left out when it's empty, so a language server from an older citry still reads the record.

Tests

  • packages/py/citry/tests/test_lint_template_data_keys.py covers:
    • the list closing the namespace;
    • the drift warning, including its range in the Python file;
    • subclass replacement, None and the schema-on-subclass case;
    • every error mode;
    • the editor wire round trip.
  • test_template_data_source.py: keys set on a helper's result are kept, and a non-call value still proves nothing.
  • citry_lsp/tests/test_engine.py: the editor reports the typo against the list and offers the listed key as a completion.
  • citry_ui/tests/test_citry_ui_registration.py:
    • test_citry_ui_templates_check_clean_against_their_declared_keys runs include_libraries=True and fails when a template reads a name its list leaves out;
    • test_a_subclass_of_a_citry_ui_component_may_describe_its_data_with_a_schema.

Known limits

  • The editor applies the list only where it can read the component's template_data chain. Where it can't, for example a decorated template_data, citry check still reports a typo but the editor shows nothing.
  • A key set as a keyword argument, such as data.update(badge=1), has no source position, so the drift warning doesn't cover it.
  • The drift warning repeats once for each subclass that inherits the method, as the js_data warnings do.
  • The list closes the namespace even when Kwargs allows extra keys that pass through.
  • Release note: citry_ui declares citry>=0.6.0, and citry 0.6.0 rejects an unknown Lint setting. The next citry-ui release needs to raise that floor. check_input_types already needed it.

Evidence (local)

PR CI runs the cargo phases and the full matrix.

🤖 Generated with Claude Code

Citry reads template_data() to learn which names a template can use. When
the method builds its result through a helper, Citry cannot tell a typo
from a name the helper adds, so citry check could only say that a name
might be supplied dynamically. About 60 Citry UI templates were in that
state, which is why citry check --include-libraries reported ~1,500 such
errors on an app that registers Citry UI.

A component can now list the keys in Lint.template_data_keys. Only
citry check and the editor read it. The template may then use the listed
keys, the keys Citry can read from the method, and the declared variables,
so a typo is reported as a name the template cannot use, and the editor
offers the listed keys as completions. citry check warns about a key the
method writes as a literal that the list leaves out
(citry.template.undeclared-template-data-key).

The source analysis also keeps the literal keys set on a helper's result,
as in data = build(kwargs); data["x"] = ..., while still treating the
result as possibly holding more keys.

Error modes, all when the class is defined: a value that is not a tuple,
list or frozenset of strings raises TypeError; a name that cannot be a
Python variable name, such as "class", or a name listed twice raises
ValueError; a class that declares both the list and a TemplateData schema
raises TypeError. A schema on a subclass replaces an inherited list, so
a subclass of a Citry UI component can still add one, and
template_data_keys = None clears an inherited list.

The 60 Citry UI components now declare their keys, taken from the keys
their template_data() returned across the whole Citry UI test suite.
Every name their templates use was among them, so no template bug turned
up. A new Citry UI test checks those templates with
--include-libraries and keeps the lists complete.

Fixes #149

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@JuroOravec
JuroOravec merged commit ce06c34 into main Oct 25, 2026
19 of 29 checks passed
@JuroOravec
JuroOravec deleted the feat/lint-template-data-keys branch October 25, 2026 16:19
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.

citry check reports ~1,600 errors on any app that registers citry_ui

1 participant