-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
bpo-34850: Emit a warning for "is" and "is not" with a literal. #9642
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
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have suggestions for the wording of the news or What's New entry?
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.
How about:
The compiler now produces a SyntaxWarning when identity checks (
is
andis 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 <[email protected]>
Patch by Serhiy Storchaka (in PR pythonGH-9642). (cherry picked from commit 5fa247d) Co-authored-by: Terry Jan Reedy <[email protected]>
Patch by Serhiy Storchaka (in PR GH-9642). (cherry picked from commit 5fa247d) Co-authored-by: Terry Jan Reedy <[email protected]>
Patch by Serhiy Storchaka (in PR GH-9642). (cherry picked from commit 5fa247d) Co-authored-by: Terry Jan Reedy <[email protected]>
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