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 — Syntax warnings can be swallowed in import · Issue #139640 · python/cpython

Syntax warnings can be swallowed in import #139640

New issue
Copy link
New issue
Copy link
Closed
Closed
Syntax warnings can be swallowed in import#139640
Copy link
Assignees
serhiy-storchaka
Labels
3.13only security fixesonly security fixes3.14bugs and security fixesbugs and security fixes3.15bugs and security fixesbugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@serhiy-storchaka
serhiy-storchaka
opened on Oct 6, 2025
Issue body actions

Bug report

As I worked on issue #135801, I realized that the solution to issue #131927 was wrong. _PyErr_WarnExplicitObjectWithContext() takes the module name and the warning registry from the current Python frame. In case of import it is importlib._bootstrap_external.

Wrong module name which makes difficult to filter syntax warnings is an old issue, but the main problem is that syntax warnings for imported modules share the same warning registry. When you import two modules why by accident emit the same syntax warning at the same line number, you will only see one warning by default. It may be argued that swallowing a warning is worse that emitting a warning twice.

The following PR reverts PR #131993 and provides an alternative solution that fixes all double warnings but keeps warnings for repeated compilation. ast.parse() no longer emits warnings that are emitted in the AST optimizer (return/break/continue in finally) -- they are only emitted during compiling.

Linked PRs

  • gh-131927: Do not emit PEP 765 warnings in ast.parse() #139642
  • gh-131927: Silence PEP 765 warnings in PyREPL #139719
  • gh-139640: Fix swallowing syntax warnings in different modules (restore duplicated PEP 765 warnings) #139755
  • [3.14] gh-139640: Fix swallowing syntax warnings in different modules (GH-139755) #140117
  • [3.13] gh-139640: Fix swallowing syntax warnings in different modules (GH-139755) #140119
  • Add mention of other fixed bug in the gh-139640 NEWS entry #140122

Activity

Next
  1. serhiy-storchaka
    self-assigned this
    on Oct 6, 2025
  2. serhiy-storchaka
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    3.13only security fixesonly security fixes
    3.14bugs and security fixesbugs and security fixes
    3.15bugs and security fixesbugs and security fixes
    on Oct 6, 2025
  3. serhiy-storchaka
    added 4 commits that reference this issue on Oct 6, 2025

    pythongh-139640: Fix swallowing syntax warnings in different modules

    a7a83fa

    pythongh-139640: Fix swallowing syntax warnings in different modules

    2fa4266

    pythongh-139640: Fix swallowing syntax warnings in different modules

    c04d874

    pythongh-139640: Fix swallowing syntax warnings in different modules

    3990961
  4. serhiy-storchaka commented on Oct 7, 2025

    @serhiy-storchaka
    serhiy-storchaka
    on Oct 7, 2025
    MemberAuthor
    More actions

    #139719 is an alternative solution that keeps the PEP 765 warnings in ast.parse(), but silence them in the REPL. It is based on the original solution proposed by @tomasr8 in #131927 (comment).

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

    pythongh-139640: Fix swallowing syntax warnings in different modules

    6cb99f0
  6. serhiy-storchaka commented on Oct 8, 2025

    @serhiy-storchaka
    serhiy-storchaka
    on Oct 8, 2025
    MemberAuthor
    More actions

    And #139755 is the third alternative that simply restores the double PEP 765 warnings.

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

    pythongh-139640: Fix swallowing syntax warnings in different modules

    c12e7ec
  8. ncoghlan commented on Oct 8, 2025

    @ncoghlan
    ncoghlan
    on Oct 8, 2025
    Contributor
    More actions

    Switching to commenting here now that there are multiple related PRs.

    The PEP text assumed that it would be feasible to emit the syntax warnings during the actual AST construction pass (the part that doesn't get reexecuted when opcodes are generated from an already parsed AST).

    Emitting the warning during the AST optimiser pass doesn't really meet the intent of that part of the PEP:

    • if AST optimisation is skipped when parsing, the warning doesn't get emitted
    • if the AST is optimised during parsing, and then optimised again during code generation, the warning gets emitted twice

    So if it isn't practical to emit the warning during the initial construction pass, I think it would be reasonable to say that part of the proposal proved harder to implement correctly than expected, flipping the effort vs reward balance, and fall back to just emitting the warning during code generation.

    (I've also personally changed my mind on the value of emitting the warning eagerly - a linter parsing that code has its own approach to reporting issues, so the early syntax warning is going to be annoying, not helpful)

    Reacted by Marc Mueller
  9. 1 remaining item

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

    gh-139640: Fix swallowing syntax warnings in different modules (GH-13…

    279db6b
  11. miss-islington
    added a commit that references this issue on Oct 14, 2025

    pythongh-139640: Fix swallowing syntax warnings in different modules (p…

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

    [3.13] pythongh-139640: Fix swallowing syntax warnings in different m…

    f84e1bd

    [3.13] pythongh-139640: Fix swallowing syntax warnings in different m…

    72c4058

    Add mention of other fixed bug in the pythongh-139640 NEWS entry

    8ef9f4f

    [3.13] gh-139640: Fix swallowing syntax warnings in different modules (…

    92f7965

    [3.14] gh-139640: Fix swallowing syntax warnings in different modules (…

    af28efd

    Add mention of other fixed bug in the gh-139640 NEWS entry (GH-140122)

    4044255
  13. ncoghlan commented on Oct 14, 2025

    @ncoghlan
    ncoghlan
    on Oct 14, 2025
    Contributor
    More actions

    I'm OK with switching to emitting the warnings during code generation due to the unforeseen consequences of trying to emit them earlier.

    @iritkatriel Are you OK with that? (I believe I was the main proponent of emitting them early, but I could easily be misremembering that)

    I'm not sure if we should file an SC issue or not - Serhiy is fixing a bug, and the fact that it's a design bug rather than purely an implementation issue doesn't change the fact that it's a bug.

  14. serhiy-storchaka commented on Oct 14, 2025

    @serhiy-storchaka
    serhiy-storchaka
    on Oct 14, 2025
    MemberAuthor
    More actions

    I have merged the smallest of PRs -- the one which simply restores the duplicate warnings. Two other PRs are now alternative solutions of the re-opened #131927.

    I thought that the previous fix also fixed duplicate warnings in the finally block -- but it was not so. So #139755 actually fixed two different warnings. If I knew, I would create two different issues for this.

  15. serhiy-storchaka
    closed this as completedon Oct 14, 2025
  16. iritkatriel commented on Oct 14, 2025

    @iritkatriel
    iritkatriel
    on Oct 14, 2025
    Member
    More actions

    I'm OK with switching to emitting the warnings during code generation due to the unforeseen consequences of trying to emit them earlier.

    @iritkatriel Are you OK with that?

    Yes.

    Reacted by Alyssa Coghlan
  17. serhiy-storchaka
    added a parent issue on Oct 17, 2025
    • Improve/fix filtering SyntaxWarnings by module #135801
  18. StanFromIreland
    added 2 commits that reference this issue on Dec 6, 2025

    pythongh-139640: Fix swallowing syntax warnings in different modules (p…

    ea16e4a

    Add mention of other fixed bug in the pythongh-139640 NEWS entry (pyt…

    1ffb263
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

3.13only security fixesonly security fixes3.14bugs and security fixesbugs and security fixes3.15bugs and security fixesbugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

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.