-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
remove usage of django's lazy function for error messages #6545
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
Conversation
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.
I believe the proposed change will be easier to review wit proper test cases. thanks for your work!
Hi @auvipy |
And I don't know why Travis tests have failed. I have ran the tests with Tox in my local computer and everything seemed working. |
I am in the process of restarting your Travis jobs since you appear to have hit a temporal issue that affected non-Python 2.7 builds. |
This was probably related to the temporary compat issue with tox/tox-venv (tox-venv only runs on Python 3 builds) |
try: | ||
error_dict = exc_info.error_dict | ||
except AttributeError: | ||
for error in exc_info.error_list: | ||
error.params = error.params or {} | ||
error.params[error.code] = error.params.get('limit_value', '') |
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.
I've figured out what the rest of this is doing, but I can't figure out what this line is supposed to be doing.
It's not clear where this parameter was being previously injected, or if it was, and I also don't see anything explaining what this is supposed to be doing.
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.
Django's BaseValidator takes a limit_value
and that value is used for formatting messages. For example, this is the message template of MaxValueValidator
.
message = _('Ensure this value is less than or equal to %(limit_value)s.')
But in Rest Framework, Serializers use code
property of Validators as keyword for error message templates. This is IntegerField error message, for example:
'Ensure this value is less than or equal to {max_value}.
This block of code extracts needed keyword from it's Validator classes and adds them to params dict for error formatting.
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.
So, just to confirm, this is an issue with the current DRF logic and it's just being fixed in this PR alongside the stated removal of lazy()
?
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.
Yes. Actually a performance issue.
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.
Given that this is new logic, this is where I lean towards having new tests to cover this new logic. It's clear that there are no current tests that cover it (otherwise they would be failing), and I'd hate to see a regression in the future because they are not covered.
I'd also love to see it broken out into a separate PR, so it can be independently reviewed and not hold up the general refactor that is happening here, but I'm not stuck on that.
Hi, I also noticed this slowness. I made a different fix: #6644. The approach here is problematic, because Django's ValidationError expects the error message to be formatted with |
Hi @mehdipourfar. Thanks for going to the effort to implement this. I realize this is based off my suggestion in #5452 (comment), but I ultimately agree that it's flawed. Ideally, we'd switch all validation messages to be %-based to be consistent with Django, but the headache isn't worth it. |
In my benchmarks, using Django's lazy function inside
__init__
method of serializers makes serialization process 3 times slower. This commit:ValidationError
.