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

test_empty_not_allowed_if_allow_empty_is_set_to_false emits deprecation warning in Python 3.9 #7433

Closed
5 of 6 tasks
tirkarthi opened this issue Jul 29, 2020 · 4 comments
Closed
5 of 6 tasks

Comments

@tirkarthi
Copy link
Contributor

Checklist

  • I have verified that that issue exists against the master branch of Django REST framework.
  • I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
  • This is not a usage question. (Those should be directed to the discussion group instead.)
  • This cannot be dealt with as a third party library. (We prefer new functionality to be in the form of third party libraries where possible.)
  • I have reduced the issue to the simplest possible case.
  • I have included a failing test as a pull request. (If you are unable to do so we can still accept the issue.)

Steps to reproduce

test_empty_not_allowed_if_allow_empty_is_set_to_false emits deprecation warning in Python 3.9 due to NotImplemented being used in boolean context which always evaluates to True

tests/test_serializer_nested.py::TestNestedSerializerWithMany::test_empty_not_allowed_if_allow_empty_is_set_to_false
  /root/django-rest-framework/rest_framework/exceptions.py:77: DeprecationWarning: NotImplemented should not be used in a boolean context
    return r and self.code == other.code

Expected behavior

Actual behavior

@tomchristie
Copy link
Member

Believe this to be closed via #7531

@allanlewis
Copy link
Contributor

I'm getting a similar error from __ne__ on 3.12.4, which includes the fix from #7531:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/unittest/mock.py", line 1336, in patched
    return func(*newargs, **newkeywargs)
  File "<my-code>", line <redacted>, in <redacted>
    assert_that(exc.exception.detail).is_equal_to(
  File "<redacted>/assertpy/base.py", line 125, in is_equal_to
    if self.val != other:
  File "<redacted>/rest_framework/exceptions.py", line 84, in __ne__
    return not self.__eq__(other)
DeprecationWarning: NotImplemented should not be used in a boolean context

So does the fix applied to __eq__ need to be applied to __ne__ as well? If we could backport any change to 3.12.x that would be very helpful 🙂

@tomchristie
Copy link
Member

Seems valid to me.

Looks like we need to change from this…

    def __ne__(self, other):
        return not self.__eq__(other)

to this…

    def __ne__(self, other):
        r =  self.__eq__(other)
        if r is NotImplemented:
            return NotImplemented
        return not r

Would you be interested in submitting a pull request for this change?

@allanlewis
Copy link
Contributor

@tomchristie sure, see #8538. I'm not sure how best to add a test to cover that case but I'd be happy to do so given some guidance.

tomchristie pushed a commit that referenced this issue Aug 1, 2022
…#8538)

PR #7531 resolved issue #7433 by updating `ErrorDetails.__eq__` to correctly
handle the `NotImplemented` case. However, Python 3.9 continues to issue the
following warning:

    DeprecationWarning: NotImplemented should not be used in a boolean context

This is because `__ne__` still doesn't handle the `NotImplemented` case
correctly. In order to avoid this warning, this commit makes the same change
for `__ne__` as previously made for `__eq__`.
sigvef pushed a commit to sigvef/django-rest-framework that referenced this issue Dec 3, 2022
…encode#8538)

PR encode#7531 resolved issue encode#7433 by updating `ErrorDetails.__eq__` to correctly
handle the `NotImplemented` case. However, Python 3.9 continues to issue the
following warning:

    DeprecationWarning: NotImplemented should not be used in a boolean context

This is because `__ne__` still doesn't handle the `NotImplemented` case
correctly. In order to avoid this warning, this commit makes the same change
for `__ne__` as previously made for `__eq__`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants