-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
Changes from 2 commits
97593cc
b4a5ca1
4f906c3
880e671
a7d4628
0598770
05e81bd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
The compiler will emit a syntax warning when the ``is`` or ``is not`` | ||
operator is used with a literal (except singletons ``None``, ``False``, | ||
``True`` and ``...``). If :exc:`SyntaxWarning` is raised as error, it will | ||
be replaced with a :exc:`SyntaxError`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -174,6 +174,7 @@ static int compiler_addop(struct compiler *, int); | |
static int compiler_addop_i(struct compiler *, int, Py_ssize_t); | ||
static int compiler_addop_j(struct compiler *, int, basicblock *, int); | ||
static int compiler_error(struct compiler *, const char *); | ||
static int compiler_warn(struct compiler *, const char *); | ||
static int compiler_nameop(struct compiler *, identifier, expr_context_ty); | ||
|
||
static PyCodeObject *compiler_mod(struct compiler *, mod_ty); | ||
|
@@ -2155,6 +2156,41 @@ compiler_class(struct compiler *c, stmt_ty s) | |
return 1; | ||
} | ||
|
||
static int | ||
check_is_arg(expr_ty e) | ||
{ | ||
if (e->kind != Constant_kind) { | ||
return 1; | ||
} | ||
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 commentThe 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. |
||
|| value == Py_True | ||
|| value == Py_Ellipsis); | ||
} | ||
|
||
static int | ||
check_compare(struct compiler *c, expr_ty e) | ||
{ | ||
Py_ssize_t i, n; | ||
int left = check_is_arg(e->v.Compare.left); | ||
n = asdl_seq_LEN(e->v.Compare.ops); | ||
for (i = 0; i < n; i++) { | ||
cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i); | ||
int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); | ||
if (op == Is || op == IsNot) { | ||
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 commentThe 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. How about:
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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". |
||
return compiler_warn(c, msg); | ||
} | ||
} | ||
left = right; | ||
} | ||
return 1; | ||
} | ||
|
||
static int | ||
cmpop(cmpop_ty op) | ||
{ | ||
|
@@ -2234,6 +2270,9 @@ compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond) | |
return 1; | ||
} | ||
case Compare_kind: { | ||
if (!check_compare(c, e)) { | ||
return 0; | ||
} | ||
Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1; | ||
if (n > 0) { | ||
basicblock *cleanup = compiler_new_block(c); | ||
|
@@ -2970,7 +3009,6 @@ compiler_assert(struct compiler *c, stmt_ty s) | |
{ | ||
static PyObject *assertion_error = NULL; | ||
basicblock *end; | ||
PyObject* msg; | ||
|
||
if (c->c_optimize) | ||
return 1; | ||
|
@@ -2980,18 +3018,13 @@ compiler_assert(struct compiler *c, stmt_ty s) | |
return 0; | ||
} | ||
if (s->v.Assert.test->kind == Tuple_kind && | ||
asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) { | ||
msg = PyUnicode_FromString("assertion is always true, " | ||
"perhaps remove parentheses?"); | ||
if (msg == NULL) | ||
return 0; | ||
if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, | ||
c->c_filename, c->u->u_lineno, | ||
NULL, NULL) == -1) { | ||
Py_DECREF(msg); | ||
asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) | ||
{ | ||
if (!compiler_warn(c, "assertion is always true, " | ||
"perhaps remove parentheses?")) | ||
{ | ||
return 0; | ||
} | ||
Py_DECREF(msg); | ||
} | ||
end = compiler_new_block(c); | ||
if (end == NULL) | ||
|
@@ -3546,6 +3579,9 @@ compiler_compare(struct compiler *c, expr_ty e) | |
{ | ||
Py_ssize_t i, n; | ||
|
||
if (!check_compare(c, e)) { | ||
return 0; | ||
} | ||
VISIT(c, expr, e->v.Compare.left); | ||
assert(asdl_seq_LEN(e->v.Compare.ops) > 0); | ||
n = asdl_seq_LEN(e->v.Compare.ops) - 1; | ||
|
@@ -4792,6 +4828,32 @@ compiler_error(struct compiler *c, const char *errstr) | |
return 0; | ||
} | ||
|
||
/* Emits a SyntaxWarning and returns 1 on success. | ||
If a SyntaxWarning is raised as error, replaces it with a SyntaxError | ||
and returns 0. | ||
*/ | ||
|
||
static int | ||
compiler_warn(struct compiler *c, const char *errstr) | ||
{ | ||
PyObject *msg = PyUnicode_FromString(errstr); | ||
if (msg == NULL) { | ||
return 0; | ||
} | ||
if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename, | ||
c->u->u_lineno, NULL, NULL) < 0) | ||
{ | ||
Py_DECREF(msg); | ||
if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { | ||
PyErr_Clear(); | ||
return compiler_error(c, errstr); | ||
} | ||
return 0; | ||
} | ||
Py_DECREF(msg); | ||
return 1; | ||
} | ||
|
||
static int | ||
compiler_handle_subscr(struct compiler *c, const char *kind, | ||
expr_context_ty ctx) | ||
|
Uh oh!
There was an error while loading. Please reload this page.