GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Improve/fix filtering SyntaxWarnings by module · Issue #135801 · python/cpython

Improve/fix filtering SyntaxWarnings by module #135801

New issue
Copy link
New issue
Copy link
Closed
2 / 22 of 2 issues completed
Closed
Improve/fix filtering SyntaxWarnings by module#135801
2 / 22 of 2 issues completed
Copy link
Assignees
serhiy-storchaka
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancementA feature request or enhancement

Description

@cdce8p
cdce8p
opened on Jun 21, 2025
Issue body actions

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 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:

cpython/Python/codegen.c

Lines 1831 to 1837 in b14986c

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
);

cpython/Python/compile.c

Lines 1204 to 1214 in b14986c

_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

  • gh-135801: Fix inaccurate module info for SyntaxWarnings during AST parsing #135829
  • gh-135801: Add the module parameter to compile() etc #139652
  • gh-135801: Improve filtering by module in warn_explicit() without module argument #140151
  • gh-135801: Add tests for filtering warnings by module #140240
  • [3.14] gh-135801: Add tests for filtering warnings by module (GH-140240) #140246
  • [3.13] gh-135801: Add tests for filtering warnings by module (GH-140240) #140247

Activity

Next
  1. cdce8p
    added
    type-featureA feature request or enhancementA feature request or enhancement
    on Jun 21, 2025
  2. picnixz
    added
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    on Jun 21, 2025
  3. serhiy-storchaka
    self-assigned this
    on Oct 3, 2025
  4. serhiy-storchaka commented on Oct 6, 2025

    @serhiy-storchaka
    serhiy-storchaka
    on Oct 6, 2025
    Member
    More actions

    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:

    • Reverting PR gh-131927: Prevent emitting optimizer warnings twice in the REPL #131993 and applying an alternative fix (see Syntax warnings can be swallowed in import #139640)
    • 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
  5. serhiy-storchaka
    added a commit that references this issue on Oct 6, 2025

    pythongh-135801: Add the module parameter to compile() etc

    f6bd00b
  6. serhiy-storchaka
    added 5 commits that reference this issue on Oct 14, 2025

    pythongh-135801: Add the module parameter to compile() etc

    d1592f2

    pythongh-135801: Improve filtering by module in warn_explicit() witho…

    a0aeb4f

    pythongh-135801: Improve filtering by module in warn_explicit() witho…

    e7c94d2

    pythongh-135801: Add the module parameter to compile() etc

    9a927b3

    pythongh-135801: Improve filtering by module in warn_explicit() witho…

    f37f14c
  7. serhiy-storchaka commented on Oct 16, 2025

    @serhiy-storchaka
    serhiy-storchaka
    on Oct 16, 2025
    Member
    More actions

    #140151 improves heuristic if module is not passed in warn_explicit(), and #139652 adds the module argument in compile() and similar functions.

  8. serhiy-storchaka
    added a commit that references this issue on Oct 17, 2025

    pythongh-135801: Add tests for filtering warnings by module

    b12c64c
  9. serhiy-storchaka
    added sub-issues on Oct 17, 2025
    • Syntax warnings can be swallowed in import #139640
    • Incorrect documentation for the registry parameter in warnings.warn_explicit() #140241
  10. serhiy-storchaka commented on Oct 17, 2025

    @serhiy-storchaka
    serhiy-storchaka
    on Oct 17, 2025
    Member
    More actions

    PR #140240 adds tests for filtering by module in warn_explicit(), and #140241 is a separate issue for documentation of warn_explicit().

  11. 2 remaining items

  12. miss-islington
    added 2 commits that reference this issue on Oct 17, 2025

    pythongh-135801: Add tests for filtering warnings by module (pythonGH…

    29a8ae8

    pythongh-135801: Add tests for filtering warnings by module (pythonGH…

    c945156
  13. serhiy-storchaka
    added 2 commits that reference this issue on Oct 17, 2025

    [3.14] gh-135801: Add tests for filtering warnings by module (GH-140240…

    889e918

    [3.13] gh-135801: Add tests for filtering warnings by module (GH-140240…

    cd6b272
  14. serhiy-storchaka
    added a commit that references this issue on Oct 30, 2025

    gh-135801: Improve filtering by module in warn_explicit() without mod…

    6826166
  15. cdce8p commented on Oct 30, 2025

    @cdce8p
    cdce8p
    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!

  16. serhiy-storchaka commented on Oct 31, 2025

    @serhiy-storchaka
    serhiy-storchaka
    on Oct 31, 2025
    Member
    More actions

    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.

    Reacted by Marc Mueller
  17. serhiy-storchaka
    added a commit that references this issue on Nov 13, 2025

    gh-135801: Add the module parameter to compile() etc (GH-139652)

    d8e6bdc
  18. serhiy-storchaka
    closed this as completedon Nov 13, 2025
  19. CuriousLearner
    added 3 commits that reference this issue on Nov 13, 2025

    Merge branch 'main' into fix-issue21914

    b9f473c

    Merge branch 'main' of github.com:python/cpython into fix-Issue18387

    c46ecc5

    Merge branch 'main' of github.com:python/cpython into fix-issue24255

    c240289
  20. StanFromIreland
    added 3 commits that reference this issue on Dec 6, 2025

    pythongh-135801: Add tests for filtering warnings by module (pythonGH…

    31256a3

    pythongh-135801: Improve filtering by module in warn_explicit() witho…

    27c5268

    pythongh-135801: Add the module parameter to compile() etc (pythonGH-…

    84bc85b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @serhiy-storchaka
    serhiy-storchaka

Labels

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

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.