Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitae25855

Browse files
authored
gh-103492: Clarify SyntaxWarning with literal comparison (#103493)
1 parent79ae019 commitae25855

File tree

4 files changed

+34
-16
lines changed

4 files changed

+34
-16
lines changed

‎Lib/test/test_codeop.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ def test_filename(self):
277277
deftest_warning(self):
278278
# Test that the warning is only returned once.
279279
withwarnings_helper.check_warnings(
280-
('"is" witha literal',SyntaxWarning),
280+
('"is" with\'str\' literal',SyntaxWarning),
281281
("invalid escape sequence",SyntaxWarning),
282282
)asw:
283283
compile_command(r"'\e' is 0")

‎Lib/test/test_grammar.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,9 @@ def check(test, error=False):
236236
check(f"[{num}for x in ()]")
237237
check(f"{num}spam",error=True)
238238

239+
withself.assertWarnsRegex(SyntaxWarning,r'invalid \w+ literal'):
240+
compile(f"{num}is x","<testcase>","eval")
239241
withwarnings.catch_warnings():
240-
warnings.filterwarnings('ignore','"is" with a literal',
241-
SyntaxWarning)
242-
withself.assertWarnsRegex(SyntaxWarning,
243-
r'invalid \w+ literal'):
244-
compile(f"{num}is x","<testcase>","eval")
245242
warnings.simplefilter('error',SyntaxWarning)
246243
withself.assertRaisesRegex(SyntaxError,
247244
r'invalid \w+ literal'):
@@ -1467,21 +1464,33 @@ def test_comparison(self):
14671464
if1<1>1==1>=1<=1!=1in1notinxisxisnotx:pass
14681465

14691466
deftest_comparison_is_literal(self):
1470-
defcheck(test,msg='"is" with a literal'):
1467+
defcheck(test,msg):
14711468
self.check_syntax_warning(test,msg)
14721469

1473-
check('x is 1')
1474-
check('x is "thing"')
1475-
check('1 is x')
1476-
check('x is y is 1')
1477-
check('x is not 1','"is not" with a literal')
1470+
check('x is 1','"is" with\'int\' literal')
1471+
check('x is "thing"','"is" with\'str\' literal')
1472+
check('1 is x','"is" with\'int\' literal')
1473+
check('x is y is 1','"is" with\'int\' literal')
1474+
check('x is not 1','"is not" with\'int\' literal')
1475+
check('x is not (1, 2)','"is not" with\'tuple\' literal')
1476+
check('(1, 2) is not x','"is not" with\'tuple\' literal')
1477+
1478+
check('None is 1','"is" with\'int\' literal')
1479+
check('1 is None','"is" with\'int\' literal')
1480+
1481+
check('x == 3 is y','"is" with\'int\' literal')
1482+
check('x == "thing" is y','"is" with\'str\' literal')
14781483

14791484
withwarnings.catch_warnings():
14801485
warnings.simplefilter('error',SyntaxWarning)
14811486
compile('x is None','<testcase>','exec')
14821487
compile('x is False','<testcase>','exec')
14831488
compile('x is True','<testcase>','exec')
14841489
compile('x is ...','<testcase>','exec')
1490+
compile('None is x','<testcase>','exec')
1491+
compile('False is x','<testcase>','exec')
1492+
compile('True is x','<testcase>','exec')
1493+
compile('... is x','<testcase>','exec')
14851494

14861495
deftest_warn_missed_comma(self):
14871496
defcheck(test):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Clarify:exc:`SyntaxWarning` with literal ``is`` comparison by specifying which literal is problematic, since comparisons using ``is`` with e.g. None and bool literals are idiomatic.

‎Python/compile.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2269,6 +2269,8 @@ check_is_arg(expr_ty e)
22692269
||value==Py_Ellipsis);
22702270
}
22712271

2272+
staticPyTypeObject*infer_type(expr_tye);
2273+
22722274
/* Check operands of identity checks ("is" and "is not").
22732275
Emit a warning if any operand is a constant except named singletons.
22742276
*/
@@ -2277,19 +2279,25 @@ check_compare(struct compiler *c, expr_ty e)
22772279
{
22782280
Py_ssize_ti,n;
22792281
boolleft=check_is_arg(e->v.Compare.left);
2282+
expr_tyleft_expr=e->v.Compare.left;
22802283
n=asdl_seq_LEN(e->v.Compare.ops);
22812284
for (i=0;i<n;i++) {
22822285
cmpop_tyop= (cmpop_ty)asdl_seq_GET(e->v.Compare.ops,i);
2283-
boolright=check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators,i));
2286+
expr_tyright_expr= (expr_ty)asdl_seq_GET(e->v.Compare.comparators,i);
2287+
boolright=check_is_arg(right_expr);
22842288
if (op==Is||op==IsNot) {
22852289
if (!right|| !left) {
22862290
constchar*msg= (op==Is)
2287-
?"\"is\" with a literal. Did you mean \"==\"?"
2288-
:"\"is not\" with a literal. Did you mean \"!=\"?";
2289-
returncompiler_warn(c,LOC(e),msg);
2291+
?"\"is\" with '%.200s' literal. Did you mean \"==\"?"
2292+
:"\"is not\" with '%.200s' literal. Did you mean \"!=\"?";
2293+
expr_tyliteral= !left ?left_expr :right_expr;
2294+
returncompiler_warn(
2295+
c,LOC(e),msg,infer_type(literal)->tp_name
2296+
);
22902297
}
22912298
}
22922299
left=right;
2300+
left_expr=right_expr;
22932301
}
22942302
returnSUCCESS;
22952303
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp