ashes icon indicating copy to clipboard operation
ashes copied to clipboard

Opt-in errors when a parameter is missing, or an unused parameter is passed

Open ysangkok opened this issue 4 years ago • 3 comments

I noticed that missing parameters are rendered as empty strings.

>>> ashes_env.register_source('hello', 'Hello, {name}!')
<Template name='hello'>
>>> ashes_env.render('hello', {})
'Hello, !'

It would be nice if it was possible to opt-in to errors. My use case is that I want to test that I am not passing the wrong parameters for a template.

Thanks in advance, I like the library.

ysangkok avatar Jan 29 '21 22:01 ysangkok

Hey there! Glad you like ashes. This isn't possible with the library right this second, but I've got some ideas. Most revolve around this line here.

Would a log statement work? Or are you trying to raise a full-blown error?

mahmoud avatar Feb 01 '21 02:02 mahmoud

I mainly just want to use it for testing, I want to make sure that my dataclasses have the right fields, which I turn into dictionaries and pass into ashes. So it could be a boolean, or it could be a logging context-manager that I could inspect for warnings (and get a bool). Or it could be an optional kwarg. I don't mind too much, as long as I can make my test fail if the dataclass doesn't match the template.

ysangkok avatar Feb 01 '21 02:02 ysangkok

Years ago, I started work on extending ashes to track the utilized fields. I never got it to recurse correctly though.

The use-case for me was to build a model of what ashes used in the template. This would allow us to write tests where we could ensure we pass the correct data into ashes, but also optimize the data structure. Our app renders the templates in the cloud with ashes, or in a browser/app with JavaScript; being able to strip unused fields from the JavaScript payload was a big goal.

jvanasco avatar Feb 01 '21 03:02 jvanasco