variables implicitly passed to block cause unexpected error for def
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 459
- Forks
- 90
- PR merge metrics
- No merged PRs in 30d
Description
Migrated issue, originally created by Anonymous
Try out the following 3-level inheritance setup, stripped down to show only the situation I want to point out:
Test setup:
#base.mako:
<%block name="foo"/>
#middle.mako:
<%inherit file="base.mako"/>
<%block name="foo">foo</%block>
#final.mako:
<%inherit file="middle.mako"/>
<%block name="foo">
${parent.foo()}
</%block>
# test script:
from mako.template import Template
from mako.lookup import TemplateLookup
mylookup = TemplateLookup(directories=['.'], module_directory='.')
if __name__ == '__main__':
mytemplate = mylookup.get_template('final.mako')
print mytemplate.render(**{'c': 'foo'})
OK, that works fine. Actually it's doing a really great job of helping me "normalize" and modularize the templating for my site, helps keep it all D.R.Y., etc.
Now let's say a coworker who's unaware of Mako 0.4.1 and its handy new "block"s, who was working on our project a few months back when it was built on defs instead, goes to add another template at the level of final.mako, and uses the old syntax we had for the project. We forgot to tell him that our template inheritance API changed and that he needs to use a block where he used to use a def in this case. So as far as he knows we use defs for inheritance as we used to. Let's say he's lazy and hasn't re-read the middle and base templates lately, he just wants to inherit from them as he always did before. He creates the following:
#problem.mako
<%inherit file="middle.mako">
<%def name="foo()">
${parent.foo()}
</%def>
Here's the point of this ticket. He gets this error:
TypeError: render_foo() got an unexpected keyword argument 'c'
That's not too far off; ideally he would reason that the source of the problem has something to do with something calling something with args when it doesn't expect them. But remember this guy has no idea he's on Mako 0.4.1 and that blocks exist or that middle.mako and base.mako have been converted to use blocks instead of defs. So he's not thinking about pageargs or lack thereof. Instead he goes on a wild goose chase asking the server guys why they're sending this variable 'c' in the context when they didn't used to. In fact they've always been sending in 'c', it just happens that 'c' is there for legacy reasons and has never been used in this project, so this guy never dealt with it before and he thinks its sudden appearance is the problem.
Ok, that's very made-up, but the point is, our hypothetical developer perhaps ought to get an error saying that a block is trying to call a def where a block is expected. Actually the current TypeError is accurate and appropriate, but maybe it should be augmented by a bit of suggestion text e.g. "make sure you're not unknowingly mixing defs and blocks."
Attachments: middle.mako | 3leveltest.py | final.mako | base.mako
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the attached 3leveltest.py and the middle.mako, final.mako, and base.mako templates to reproduce the unexpected keyword argument error. Trace how the mixed def and block inheritance produces the TypeError; done should be an error that identifies the mismatch or suggests that defs and blocks may have been mixed.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- web-dev
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100