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.
Adding the module parameter in compile() and other functions and passing the module name if possible. This is the only reliable way to get the correct module name for syntax warnings in the imported module. This is a new feature, because it adds new parameters and the caller code needs to be changed to utilize this. gh-135801: Add the module parameter to compile() etc #139652
Improve heuristic for the cases when the module name is not known (and for not yet modified third-party code). The current behavior is inconvenient on Posix and not usable on Windows (module name in PYTHONWARNINGS cannot contain :). The idea is to try to match multiple names starting from different path components. This can potentially be backported to 3.14, although we will need to change the documented behavior. But the current behavior is too poor. gh-135801: Improve filtering by module in warn_explicit() without module argument #140151
@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.
Activity
serhiy-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:
pythongh-135801: Add the module parameter to compile() etc
pythongh-135801: Add the module parameter to compile() etc
pythongh-135801: Improve filtering by module in warn_explicit() witho…
pythongh-135801: Improve filtering by module in warn_explicit() witho…
pythongh-135801: Add the module parameter to compile() etc
pythongh-135801: Improve filtering by module in warn_explicit() witho…
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.
pythongh-135801: Add tests for filtering warnings by module
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().
2 remaining items
pythongh-135801: Add tests for filtering warnings by module (pythonGH…
pythongh-135801: Add tests for filtering warnings by module (pythonGH…
[3.14] gh-135801: Add tests for filtering warnings by module (GH-140240…
[3.13] gh-135801: Add tests for filtering warnings by module (GH-140240…
gh-135801: Improve filtering by module in warn_explicit() without mod…
cdce8p commented on Oct 30, 2025
@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.
gh-135801: Add the module parameter to compile() etc (GH-139652)
Merge branch 'main' into fix-issue21914
Merge branch 'main' of github.com:python/cpython into fix-Issue18387
Merge branch 'main' of github.com:python/cpython into fix-issue24255
pythongh-135801: Add tests for filtering warnings by module (pythonGH…
pythongh-135801: Improve filtering by module in warn_explicit() witho…
pythongh-135801: Add the module parameter to compile() etc (pythonGH-…