bpo-34850: Emit a warning for "is" and "is not" with a literal.#9642
Conversation
| } | ||
| PyObject *value = e->v.Constant.value; | ||
| return (value == Py_None | ||
| || value == Py_False |
There was a problem hiding this comment.
I wouldn't special case True and False, those are not normally identity checked. I don't doubt that someone may have create APIs returning multiple possible types that requires this to differentiate between "real" values and bools... but yuck.
|
When you're done making the requested changes, leave the comment: |
|
i plopped a do not merge label on here as the discussion in the bug over whether we even want this isn't settled. |
Python/compile.c
Outdated
| if (!right || !left) { | ||
| const char *msg = (op == Is) | ||
| ? "\"is\" with a literal. Did you mean \"==\"" | ||
| : "\"is not\" with a literal. Did you mean \"!=\""; |
There was a problem hiding this comment.
I think this warning looks good. I'd like to, but can't come up with a good way to work in the phrase "identity check".
There was a problem hiding this comment.
Do you have suggestions for the wording of the news or What's New entry?
There was a problem hiding this comment.
How about:
The compiler now produces a SyntaxWarning when identity checks (
isandis not) are used with string literals. These can often work by accident in CPython, but are not guaranteed by the language spec. The warning advises users to use equality tests (==and!=) instead.
There was a problem hiding this comment.
Thanks! But a warning will be emitted not just for string literals, but for numerical literals and tuples (empty tuple is a singleton in CPython, this is an implementation detail).
There was a problem hiding this comment.
You can say "certain types of literals (e.g. strings, ints)" rather than "string literals".
Revert change here; will make separate PR to be applied and backported regardless of fate of this PR.
Patch by Serhiy Storchaka (in PR pythonGH-9642). (cherry picked from commit 5fa247d) Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
Patch by Serhiy Storchaka (in PR pythonGH-9642). (cherry picked from commit 5fa247d) Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
Python 3.8 is now displaying a warning when is is used with a literal. Here are the relevant errors with 2.4.0: ``` /usr/lib/python3.8/site-packages/digitalocean/LoadBalancer.py:19: SyntaxWarning: "is" with a literal. Did you mean "=="? if type is 'cookies': ``` See python/cpython#9642 for more info
…302) Python 3.8 is now displaying a warning when is is used with a literal. Here are the relevant errors with 2.4.0: ``` /usr/lib/python3.8/site-packages/digitalocean/LoadBalancer.py:19: SyntaxWarning: "is" with a literal. Did you mean "=="? if type is 'cookies': ``` See python/cpython#9642 for more info
…(#302) Python 3.8 is now displaying a warning when is is used with a literal. Here are the relevant errors with 2.4.0: ``` /usr/lib/python3.8/site-packages/digitalocean/LoadBalancer.py:19: SyntaxWarning: "is" with a literal. Did you mean "=="? if type is 'cookies': ``` See python/cpython#9642 for more info
https://bugs.python.org/issue34850