Skip to content

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

Closed
wants to merge 1 commit into from

Conversation

mehdipourfar
Copy link

In my benchmarks, using Django's lazy function inside __init__ method of serializers makes serialization process 3 times slower. This commit:

  1. Removes usages of this function.
  2. Postpone error formatting to the steps after raising ValidationError.
  3. Keeps the code compatible for users who already overrided Field classes to have their own error messages.

Copy link
Member

@auvipy auvipy left a 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!

@mehdipourfar
Copy link
Author

Hi @auvipy
There is already a lot of tests for error message formatting. I don't know what to test. This commit contains no new feature. It only does some refactors and optimization.

@mehdipourfar
Copy link
Author

mehdipourfar commented Mar 31, 2019

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.

@kevin-brown
Copy link
Member

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.

@rpkilby
Copy link
Member

rpkilby commented Apr 3, 2019

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', '')
Copy link
Member

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.

Copy link
Author

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.

Copy link
Member

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()?

Copy link
Author

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.

Copy link
Member

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.

@bluetech
Copy link
Contributor

bluetech commented May 2, 2019

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 %, but this changes to use .format. This is fine for the built-in messages, but does not account for other validators which raise Django's ValidationError.

@rpkilby
Copy link
Member

rpkilby commented May 23, 2019

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.

@rpkilby rpkilby closed this May 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants