Skip to content
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

Validators do not implement __eq__ method #8905

Closed
michaelurban opened this issue Mar 14, 2023 · 5 comments · Fixed by #8925
Closed

Validators do not implement __eq__ method #8905

michaelurban opened this issue Mar 14, 2023 · 5 comments · Fixed by #8925

Comments

@michaelurban
Copy link

Rest Framework validators do not implement the __eq__ method like many of their Django counterparts.

The expression ProhibitSurrogateCharactersValidator() == ProhibitSurrogateCharactersValidator() evaluates to False when it would be useful to have it evaluate to True, especially in tests.

@auvipy
Copy link
Member

auvipy commented Mar 14, 2023

would you mind come with at least one draft implementation of this? I need to see what we can actually achieve with it. I can recall something like this in django core but can't remember exact usecases

@michaelurban
Copy link
Author

michaelurban commented Mar 14, 2023

Implementation - It's in the core validators: https://github.com/django/django/blob/stable/4.2.x/django/core/validators.py#L57

A use case is to be able to test whether a validator appears in a serializer's list of validators. For example: self.assertIn(ValidatorThatImplementsEq(), field.validators)

For this to work lazy formatted validator messages would also need to implement __eq__ IIRC. __eq__ is something they should support regardless.

assert django_validators.MinLengthValidator(
  min_length,
  message=lazy_format(
    field.error_messages["min_length"],
    min_length=min_length,
  ),
) == field.validators[0], 'This will always be "False" because "lazy_format" does not implement "__eq__"'

and

assert str(
    django_validators.MinLengthValidator(
        10,
        message=lazy_format(
            field.error_messages["min_length"],
            min_length=10,
        ),
    ).message
) != str(
    field.validators[1].message
), 'These will be equal because "lazy_format" will evaluate to the same string value'

Serializer fields are difficult to test declaratively because of these omissions.

I'm running into these issues while creating field asserts for my serializers like I use for my models.

self.assertCharField(
    RandomSerializer._declared_fields["name"],
    min_length=10,
    max_length=100,
    allow_blank=True,
)

How could anyone test their serializer field definitions declaratively w\o the __eq__-s? I'm falling back to converting the error messages to strings and comparing them.

@auvipy
Copy link
Member

auvipy commented Mar 15, 2023

as it is a django compat stuff, I am open to contribution in this regard

@michaelurban
Copy link
Author

So is this getting labeled as accepted and put on the todo pile?

@auvipy
Copy link
Member

auvipy commented Mar 17, 2023

yes

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 a pull request may close this issue.

2 participants