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

Improve/fix filtering SyntaxWarnings by module #135801

Closed
2 of 2 issues completed
Closed
2 of 2 issues completed
Assignees
serhiy-storchaka
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement
@cdce8p

Description

@cdce8p

Feature or enhancement

Proposal:

Syntax warnings during AST parsing use_PyCompile_Warn to setup their warning context automatically. Though the filename is correct, the module is inferred and often not helpful. This is especially an issue when trying to ignore specific errors in a CI environment with pytest where the module is suddenlyimportlib._bootstrap. In other cases with python alone it's often justsys.

AFAICT this applies mainly to the"is" with '...' literal and'return' in 'finally' block warnings.

--
To reproduce

# test.pyclassA:def__init__(self):self.var=2deffunc(self)->bool:returnself.varis2
./python.exe -W error::SyntaxWarning:sys test.py
File"/.../test.py",line7returnself.varis2^^^^^^^^^^^^^SyntaxError:"is"with'int'literal.Didyoumean"=="?

Note: The SyntaxWarning is changed to an error for modulesys wheretest would have been the expected module.

--
The SyntaxWarning is triggered here:

cpython/Python/codegen.c

Lines 1831 to 1837 inb14986c

constchar*msg= (op==Is)
?"\"is\" with '%.200s' literal. Did you mean \"==\"?"
:"\"is not\" with '%.200s' literal. Did you mean \"!=\"?";
expr_tyliteral= !left ?left_expr :right_expr;
return_PyCompile_Warn(
c,LOC(e),msg,infer_type(literal)->tp_name
);

cpython/Python/compile.c

Lines 1204 to 1214 inb14986c

_PyCompile_Warn(compiler*c,locationloc,constchar*format, ...)
{
va_listvargs;
va_start(vargs,format);
PyObject*msg=PyUnicode_FromFormatV(format,vargs);
va_end(vargs);
if (msg==NULL) {
returnERROR;
}
intret=_PyErr_EmitSyntaxWarning(msg,c->c_filename,loc.lineno,loc.col_offset+1,
loc.end_lineno,loc.end_col_offset+1);

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

Sub-issues

Metadata

Metadata

Labels

interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions


    [8]ページ先頭

    ©2009-2025 Movatter.jp