-
Notifications
You must be signed in to change notification settings - Fork 344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fail-on-template-vars: improve compatibility with Django behavior #1130
base: main
Are you sure you want to change the base?
fail-on-template-vars: improve compatibility with Django behavior #1130
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.DoesNotExist
which 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
InvalidVarException
behavior 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.