Repository navigation
Description
What happens
When a component declares neither template_data() nor TemplateData, the
template still renders with each Kwargs field by name, but
citry check --types reports no ty findings for its Python expressions. A
typo such as {{ owner.nmae }}, or count="project.count" (a string) passed
to a count: int input, passes the check; the first raises at render time,
and the second renders the wrong text.
Adding a template_data() that just returns the same fields makes the same
finding appear.
The Getting Started guide tells readers to rely on this default
(docs_site/content/getting-started/data-in-components.md:80-81: "By default,
the template can use each Kwargs field by its own name"), so a reader who
follows it gets no type checks for their templates.
Expected
The check reports the same ty findings with and without a pass-through
template_data().
Repro
Citry 0.6.0, citry-core 1.8.0, citry-lsp 0.2.0, ty 0.0.89, citry installed
normally (not editable) in a virtual environment.
@dataclass
class Owner:
name: str
class Greeting(Component):
citry = citry_app
class Kwargs:
owner: Owner
template = """
<p>Hello {{ owner.nmae }}</p>
"""
$ citry --app app.citry_app:citry_app check --types --format json
{..."exit_code":0,"findings":[],...}Rendering it raises AttributeError: 'Owner' object has no attribute 'nmae'.
Add this method to the class and run the same command:
def template_data(self, kwargs: Kwargs, slots):
return {"owner": kwargs.owner}{..."exit_code":1,"findings":[{"code":"citry.python.unresolved-attribute",
"message":"Object of type `Owner` has no attribute `nmae`",...}]}In a parent template, <c-Card count="project.count" /> against
count: int behaves the same way: no finding without the parent's
template_data(), and Object of type Literal["project.count"] is not assignable to int with it.
Cause
Line numbers are on origin/main (695bd31, matches the published wheels).
- With no override, the method chain ends at the base class, so the checker
copiesComponent.template_datafrom the installedcitry/component.py
(packages/py/citry_lsp/citry_lsp/engine.py:11371-11382,
packages/py/citry/citry/component.py:1123). The generated code is
correct: it readsowner = kwargs.ownerand then evaluatesowner.nmae. virtual_document_uri()puts the generated file next to its source file
(packages/py/citry_lsp/citry_lsp/type_analysis.py:620-635, through
_virtual_document_parent()at:735-740), so it ends up at
.venv/lib/python3.12/site-packages/citry/__citry_component_....py.- ty returns no diagnostics for that file, because it sits in the installed
packages and not in the project. An experiment that moved the same generated file
intoapp/components/made the finding appear.
A possible fix is to place a generated file whose source is outside the
workspace inside the workspace, as _virtual_document_parent() already does
for a source on another Windows drive. The editor runs the same
semantic_diagnostics() code (packages/py/citry_lsp/citry_lsp/semantic.py:469),
so it is probably affected too. That was not checked.
Found by
The error-prevention benchmark, issue #158.
Activity