GeezerVison is showing github.com — Improve/fix filtering SyntaxWarnings by module · Issue #135801 · python/cpython
Improve/fix filtering SyntaxWarnings by module #135801Closed 2 / 22 of 2 issues completedClosed 2 / 22 of 2 issues completed DescriptionFeature 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 suddenly importlib._bootstrap. In other cases with python alone it's often just sys.
AFAICT this applies mainly to the "is" with '...' literal and 'return' in 'finally' block warnings.
--
To reproduce
# test.py
class A:
def __init__(self):
self.var = 2
def func(self) -> bool:
return self.var is 2
./python.exe -W error::SyntaxWarning:sys test.py
File "/.../test.py", line 7
return self.var is 2
^^^^^^^^^^^^^
SyntaxError: "is" with 'int' literal. Did you mean "=="?
Note: The SyntaxWarning is changed to an error for module sys where test would have been the expected module.
--
The SyntaxWarning is triggered here:
|
const char *msg = (op == Is) |
|
? "\"is\" with '%.200s' literal. Did you mean \"==\"?" |
|
: "\"is not\" with '%.200s' literal. Did you mean \"!=\"?"; |
|
expr_ty literal = !left ? left_expr : right_expr; |
|
return _PyCompile_Warn( |
|
c, LOC(e), msg, infer_type(literal)->tp_name |
|
); |
|
_PyCompile_Warn(compiler *c, location loc, const char *format, ...) |
|
{ |
|
va_list vargs; |
|
va_start(vargs, format); |
|
PyObject *msg = PyUnicode_FromFormatV(format, vargs); |
|
va_end(vargs); |
|
if (msg == NULL) { |
|
return ERROR; |
|
} |
|
int ret = _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
Activityserhiy-storchaka commented on Oct 6, 2025 This is a difficult issue. I have spent past three days on it, and may require other three days or week. I think there will be three PRs:
serhiy-storchaka commented on Oct 16, 2025 #140151 improves heuristic if module is not passed in warn_explicit(), and #139652 adds the module argument in compile() and similar functions. serhiy-storchaka commented on Oct 17, 2025 PR #140240 adds tests for filtering by module in warn_explicit(), and #140241 is a separate issue for documentation of warn_explicit(). added a commit that references this issue on Oct 30, 2025gh-135801: Improve filtering by module in warn_explicit() without mod… cdce8p commented on Oct 30, 2025 ContributorAuthor More actions @serhiy-storchaka Thanks for your work on this one and the ast.parse issue! It's much appreciated. I just tested the changes from #140151. The issue I described in the OP is now resolved AFACIT. Both the warnings filter with the -W flag as well as the pytest warnings filter do work as I'd expect them too now.
I saw you mentioned it in #140151 (comment) already but I do agree this would be great to have in 3.14.1 as well.
I'm not sure it fully solves all issue discussed around PEP 765 but IMO it definitely improves the situations considerably.
Thanks again! serhiy-storchaka commented on Oct 31, 2025 Note that if you have modules foo.bar and bar, a filter for bar will also match foo.bar. #139652 will make filtering more accurate.
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|