Repository navigation
fix: pass origin to StringifiedNode - #1646
Conversation
| if hasattr(wrapped_node, "token"): | ||
| self.token = wrapped_node.token | ||
| if hasattr(wrapped_node, "origin"): | ||
| self.origin = wrapped_node.origin |
There was a problem hiding this comment.
The fix from #1635 is preserved, just the IF statements were removed.
Performance Benchmark ResultsComparing PR changes against master branch: Benchmarks that have stayed the same:
|
| "pytest-asyncio==1.3.0", | ||
| "syrupy", | ||
| "ruff", | ||
| "ruff==0.15.15", |
There was a problem hiding this comment.
Unrelated to the StringifiedNode work. In the last PR i pushed (#1645), I accidentally broke ruff checks. While looking into it, I was getting different errors locally then what I got in the CI. The reason was because locally I was on v0.15 while CI was on v0.15.15.
So pinned the version of ruff to avoid this discrepancy
| # Pass its origin so nested expression nodes can be annotated against the correct source file | ||
| # when an error is raised. Guarded with `getattr` so a plain-dict context (no `.template`) | ||
| # degrades gracefully to `None`. | ||
| origin = getattr(getattr(context, "template", None), "origin", None) |
There was a problem hiding this comment.
I was skeptical whether the context object indeed contains template attr. Opus 4.8 traced it and confirmed that resolve_template_string indeed receives the Django's Context inst and that context.template points to the owner Template.
BaseNode.wrapper_render(self, context) node.py:116
context = live Django Context, bound to the host template
via Context.bind_template() inside Template._render()
context.template = template from bind_template().
│
▼
self._params_resolver(context) node.py:128
▼
resolver(context) → compiled_tag(context) template_tag.py:264
▼
compiled_func(context) ← Rust-generated, see below djc_core/compile.py
▼
template_string(context, source, token, filters, tags, expr) ← SAME context object
▼
resolve_template_string(context, ...) template_tag.py:151
Follow up to #1597 and #1635.
As I'm reading through older work while I was away, I found the two issues #1597 and #1635.
After a review I found that they resolved the issue, but didn't resolve the fundamental problem that the StringifiedNode wasn't using the Origin reference, which the Django's debug mode expects.
When a multi-node template expression (e.g.
value="prefix {{ x }}"passed to acomponent) raised while Django template debug mode was enabled, the internal
StringifiedNodewrapping the expression carried no templateorigin. Django'sdebug-mode traceback annotator (
Node.render_annotated) readsoriginto pointthe error at the failing source line, so without it the annotator could not
locate the line, and in some cases masked the original exception entirely.
#1635 patched this defensively by copying
originoff the wrapped node whenpresent. This PR makes the origin reliably available by threading the host
template's origin from the render context all the way down to the expression
nodes, so the annotator always has a real source to compare against.
What changed
resolve_template_string()now reads the host template's origin from the liverender
Contextand passes it toTemplateExpression. This is the onlyrender-time call site with access to the context, so it's where the origin is
injected.
TemplateExpression.__init__accepts an optionaloriginand stamps it ontothe
Parser, so every parsed node (includingStringifiedNode) inherits areal origin.
StringifiedNodekeeps a last-resort fallback: if a node still has no origin(e.g. an expression built directly in code rather than via
resolve_template_string), it borrows the origin fromcontext.render_context.templateat render time.origindefaults toNone, so direct constructors (and existing callers)remain backward-compatible.