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

Handle NotImplemented correctly in ErrorDetail.__ne__ #8538

Merged
merged 1 commit into from
Aug 1, 2022
Merged

Handle NotImplemented correctly in ErrorDetail.__ne__ #8538

merged 1 commit into from
Aug 1, 2022

Conversation

allanlewis
Copy link
Contributor

As per the sole commit:

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__.

refs #7433 #7531

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__`.
@tomchristie
Copy link
Member

Wait. Haven't we got both of these wrong? In what context is NotImplemented returned?
Surely it should be raised as an exception.

What code is returning it?

Copy link
Member

@tomchristie tomchristie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test - does this unapprove?

@allanlewis
Copy link
Contributor Author

Test - does this unapprove?

You need to "dismiss review" from the panel at the bottom.

Wait. Haven't we got both of these wrong? In what context is NotImplemented returned?
Surely it should be raised as an exception.

What code is returning it?

No, we've got this right, see the docs:

NotImplemented
A special value which should be returned by the binary special methods (e.g. __eq__(), __lt__(), __add__(), __rsub__(), etc.) to indicate that the operation is not implemented with respect to the other type; may be returned by the in-place binary special methods (e.g. __imul__(), __iand__(), etc.) for the same purpose. It should not be evaluated in a boolean context. NotImplemented is the sole instance of the types.NotImplementedType type.
...
Changed in version 3.9: Evaluating NotImplemented in a boolean context is deprecated. While it currently evaluates as true, it will emit a DeprecationWarning. It will raise a TypeError in a future version of Python.

@tomchristie
Copy link
Member

Gotcha, thanks.

@allanlewis
Copy link
Contributor Author

As I said in #7433 (comment) I'm happy to add a test for this case, given some guidance.

@tomchristie
Copy link
Member

Perhaps we're okay with it not having one.
PR #7531 didn't.
Consistency is a virtue, no?

@wieczorek1990
Copy link
Contributor

Please rename „r” to „result” or something else.

@tomchristie tomchristie merged commit 224168a into encode:master Aug 1, 2022
@tomchristie
Copy link
Member

@wieczorek1990 The change here is consistent with the existing code above.

@allanlewis
Copy link
Contributor Author

Thanks, @tomchristie 🙏

@allanlewis allanlewis deleted the 7433-handle-not-implemented-for-ErrorDetail-__ne__ branch August 1, 2022 14:25
@wieczorek1990
Copy link
Contributor

I don’t think short names are good code.

@tomchristie
Copy link
Member

Generally agreed.
A pull request changing them for both cases would be accepted.

@wieczorek1990
Copy link
Contributor

@tomchristie Here: #8585

sigvef pushed a commit to sigvef/django-rest-framework that referenced this pull request 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
Development

Successfully merging this pull request may close these issues.

3 participants