fail-on-template-vars: improve compatibility with Django behavior#1130
fail-on-template-vars: improve compatibility with Django behavior#1130xavfernandez wants to merge 1 commit intopytest-dev:mainfrom
Conversation
dcf035b to
2413623
Compare
2413623 to
8e2c999
Compare
With `OneToOneField`, Django raises `Model.DoesNotExist` which is converted by its template engine to `string_if_invalid`: https://github.com/django/django/blob/5.0.7/django/template/base.py#L932-L933 It is usually falsy, hence the need for `InvalidVarException.__bool__` to return `bool(self.origin_value)` to be consistent with Django's default behavior. However to trigger `InvalidVarException` behavior and its dreaded `InvalidVarException.__mod__`, it needs to go through this check: https://github.com/django/django/blob/5.0.7/django/template/base.py#L716 and thus also needs to be truthy hence the stack inspecting `__bool__` method to know what to return.
8e2c999 to
d7da5ac
Compare
bluetech
left a comment
There was a problem hiding this comment.
The purpose of the pytest-django feature is to loudly fail on invalid. Can you explain a bit more why the ObjectDoesNotExist case is different from the other invalid variable scenarios?
This is not about not failing loudly but about being consistent with Django's default behavior. When a It is usually falsy (the default being If you comment the added This is an issue because when enabling |
|
Django’s As I concluded in my post, I think |
|
I agree that I've read your blog post (and discovered a few issues I wasn't aware of 😬 ) but by simply adding a : to the current
The current implementation is still flawed but not that often. This PR would handle an hypothetical example 11. In this situation, I think that perfect is the enemy of good and that this PR improves the current situation. Even if I disagree with your conclusion and would actually recommend the use of pytest-django's fail-on-template-vars option in new projects (with a few patches, like this PR 😅 ) I recognize that the That's actually what I've been doing here |
With
OneToOneField, Django raisesModel.DoesNotExistwhich is converted by its template engine tostring_if_invalid: https://github.com/django/django/blob/5.0.7/django/template/base.py#L932-L933It is usually falsy, hence the need for
InvalidVarException.__bool__to returnbool(self.origin_value)to be consistent with Django's default behavior.However to trigger
InvalidVarExceptionbehavior and its dreadedInvalidVarException.__mod__, it needs to go through this check: https://github.com/django/django/blob/5.0.7/django/template/base.py#L716 and thus also needs to be truthy hence the stack inspecting__bool__method to know what to return.