-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
bpo-35712: Make using NotImplemented in a boolean context issue a dep… #13195
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.
LGTM. Just added few style nits.
The Azure Pipelines test failure was due to some unrelated issue with test_httplib and a bad cert. Looks like master fixed this in the past half hour, so I merged and pushed the new code to avoid the automated test failure. |
Could your please update the PR? Resolve conflicts and retarget this change to 3.9. I am going to merge this PR immediately after updating. It is too later for 3.8, but we have a lot of time to test it in 3.9. |
33a1802
to
170346f
Compare
Hoping I did it correctly; a merge covering hundreds of commits seemed wrong, so I did a rebase and force push; only conflicts were in the 3.8 What's New (which I removed, before making a new commit adding the same info to the 3.9 What's New), and in object.c (where tp_reserved got renamed to tp_as_async in the comments near where I'd defined tp_as_number for NotImplemented; kept the new name). Otherwise rebased with minimal annoyance. |
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.
Please document the unique behavior of NotImplemented in Doc/reference/datamodel.rst
and Doc/library/constants.rst
.
170346f
to
f309a02
Compare
@@ -146,6 +146,12 @@ Deprecated | |||
of Python. For the majority of use cases users can leverage the Abstract Syntax | |||
Tree (AST) generation and compilation stage, using the :mod:`ast` module. | |||
|
|||
* Using :data:`NotImplemented` in a boolean context has been deprecated, | |||
as it is almost exclusively the result of incorrect rich comparator | |||
implementations. It will be made a :exc:`TypeError` in a future version |
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 am not sure that it will be a TypeError. It may be a RuntimeWarning.
The programmer can silence a warning programmatically if it is raised in the code which he cannot change, but he cannot silence a TypeError.
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.
Sorry, I didn't see this comment before pushing my changes to datamodel/constants, so they still reference TypeError.
In any event, is there some reason to stop at RuntimeWarning? I thought DeprecationWarning was supposed to give all the "code you cannot change" (third party packages) time to update themselves so nothing is using the error-prone code path by the time it becomes an actual error?
I can change it, but I'd prefer this to be an error eventually; RuntimeWarning is used in a few places, so the guidelines aren't clear, but in most cases it seems to boil down to either a mostly harmless thing where the warning tells you you asked Python to do something nonsensical/suboptimal (e.g. bad buffering configuration), and it chose to ignore you and do something valid/more optimal instead, or in the case of asyncio, a case that is probably an error (failing to await a coroutine), but the problem can't be detected until an unpredictable time after the problem occurred, so raising an exception isn't feasible at that point (the same way exceptions in del are suppressed). In most of these cases, Python can stumble along and ignoring the warning doesn't leave you doing anything wrong, just possibly suboptimal. In almost all cases, if NotImplemented is used in a boolean context, and there is no clearly valid way for Python to handle it (what should have delegated or raised TypeError becomes a return of True or False), thus my preference for (eventually) making it an error.
…recation, explicitly discourage using it in a boolean context.
@serhiy-storchaka, I believe @MojoVampire made all the requested changes and had an outstanding question for you in the last comment. Hopefully this can be merged soon. |
|
…recation warning
A few tests and standard library functions were using it in boolean context (the standard library usage was technically correct, but obscure to the point of code smell, and easy to fix) while one test was using it on the assumption that it had not defined any methods beyond what
object
provides (and the change gives it__bool__
) so I tweaked them as necessary (making the standard lib code more clear and using other sentinels likeEllipsis
orobject()
for the relevant tests).https://bugs.python.org/issue35712