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 — gh-131927: Do not emit PEP 765 warnings in ast.parse() by serhiy-storchaka · Pull Request #139642 · python/cpython

Conversation

@serhiy-storchaka

@serhiy-storchaka serhiy-storchaka commented Oct 6, 2025 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Revert GH-131993.

Fix swallowing some syntax warnings in different modules if they accidentally have the same message and are emitted from the same line.

ast.parse() no longer emits syntax warnings for
return/break/continue in finally (see PEP-765) -- they are only emitted during compilation.

  • Issue: Syntax warnings can be swallowed in import #139640
  • Issue: Compilation emits multiple warnings in the finally block #131927

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@serhiy-storchaka serhiy-storchaka added the needs backport to 3.13 only security fixes label Oct 6, 2025
@serhiy-storchaka
serhiy-storchaka requested review from ambv, lysnikolaou and pablogsal as code owners October 6, 2025 07:20
@serhiy-storchaka serhiy-storchaka added the needs backport to 3.14 bugs and security fixes label Oct 6, 2025
@serhiy-storchaka
serhiy-storchaka requested review from Eclips4, iritkatriel, isidentical, markshannon and tomasr8 as code owners October 6, 2025 07:20
@bedevere-app bedevere-app Bot mentioned this pull request Oct 6, 2025
Syntax warnings can be swallowed in import #139640
Closed
@bedevere-app bedevere-app Bot added the awaiting core review label Oct 6, 2025
@serhiy-storchaka
pythongh-139640: Fix swallowing syntax warnings in different modules
c04d874
Revert pythonGH-131993.

Fix swallowing some syntax warnings in different modules if they accidentally
have the same message and are emitted from the same line.

ast.parse() no longer emits syntax warnings for
return/break/continue in finally (see PEP-765) -- they are only
emitted during compilation.
@serhiy-storchaka
serhiy-storchaka force-pushed the compile-syntax-warnings branch from 2fa4266 to c04d874 Compare October 6, 2025 07:34
@serhiy-storchaka
Fix tests for ast.parse().
07033ae
Eclips4
Eclips4 approved these changes Oct 6, 2025
View reviewed changes

@Eclips4 Eclips4 left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The AST changes are LGTM.
Thank you Serhiy.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels Oct 6, 2025
@ncoghlan

ncoghlan commented Oct 7, 2025

Copy link
Copy Markdown
Contributor

Would it be possible to avoid the side effect of delaying the PEP 765 syntax warnings to the code generation stage? Emitting those syntax warnings during AST construction is one of the key reasons I was comfortable suggesting turning them off globally in #139658 when static code analysis was in use.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka

serhiy-storchaka commented Oct 7, 2025

Copy link
Copy Markdown
Member Author

Only if you are fine with double warnings in REPL or other places that use ast.parse() + compile() instead of just compile().

There are three kinds of warnings, by the emitter:

  • Emitted in the lexer/tokenizer. They are always emitted when you parse the Python sources.
  • Emitted in the AST optimizer. They may be emitted twice, in ast.parse() and compile(). Currently, this is only the PEP 765 warnings.
  • Emitted in the code generator. They are only emitted if you use compile().

So, you already see not all warnings if you only use ast.parse(), without code generation. The fact that the PEP 765 warnings are emitted in the AST optimizer instead of the code generator is just an implementation detail. It could perhaps even be more convenient to emit them in the code generator.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@iritkatriel

iritkatriel commented Oct 7, 2025

Copy link
Copy Markdown
Member

The fact that the PEP 765 warnings are emitted in the AST optimizer instead of the code generator is just an implementation detail.

Actually I think this was a deliberate decision, we want this to happen during static analysis.

Can we add a kwarg to ast.parse to tell it whether to emit syntax warnings or not?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka

serhiy-storchaka commented Oct 7, 2025

Copy link
Copy Markdown
Member Author

We can, but

  • This is a new feature. It can perhaps be backported to 3.14.1 as a special exception.
  • Every user of ast.parse() and compile() will need to modify their code to pass the new option. And they will not get all warnings, they will only get the PEP 765 warning. Is it worth a new option?

It is safer to disable that warning by default in ast.parse().

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@iritkatriel

iritkatriel commented Oct 7, 2025

Copy link
Copy Markdown
Member

Every user of ast.parse() and compile() will need to modify their code to pass the new option. And they will not get all warnings, they will only get the PEP 765 warning.

The default should be to give the warning. Turn it off in cpython when we know that the code was just ast.parse'ed (rather than an ast given by the user - we do know that).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka

serhiy-storchaka commented Oct 7, 2025

Copy link
Copy Markdown
Member Author

What is the name of the option you propose to add?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@iritkatriel

iritkatriel commented Oct 7, 2025

Copy link
Copy Markdown
Member

Could be with_warnings or something like that.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka

serhiy-storchaka commented Oct 7, 2025

Copy link
Copy Markdown
Member Author

No, because it should not control all warnings. Maybe _enable_pep_765_warnings?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka

serhiy-storchaka commented Oct 7, 2025

Copy link
Copy Markdown
Member Author

This would complicated, and I need to revert #131993 because it is a blocker for my other changes.

See yet one option in #139719. It is based on the original solution by @tomasr8.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@iritkatriel

iritkatriel commented Oct 7, 2025

Copy link
Copy Markdown
Member

Why not all warnings?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ncoghlan

ncoghlan commented Oct 7, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

The early PEP 765 warnings were indeed specified in the last paragraph of https://peps.python.org/pep-0765/#specification

Edit: that said, I'm open to revisiting that decision and instead letting linters make the call on how to present syntax warnings in their ruleset. My suspicion is that they would end up turning off the parse-time warnings anyway, in which case we don't actually lose anything important by handling the PEP 765 warnings in the code generation step instead of the AST parsing step.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka

serhiy-storchaka commented Oct 8, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

Why not all warnings?

There are three kinds of warnings. #139642 (comment)

If we silence all warnings in ast.parse() or compile(), we will silence not only the PEP 765 warnings, but also warnings emitted in the tokenizer or in the code generator.

Anyway, adding an option for selective silencing requires complex changes will be a complex change comparable to #139652, but with less benefit. I would rather not backport it to 3.14.

So we have a choice for 3.14:

  • Silence the PEP 765 warnings in ast.parse(). This PR.
  • Silence the PEP 765 warnings in PyREPL (and all other sites that use ast.parse() + compile, including third-party code). gh-131927: Silence PEP 765 warnings in PyREPL #139719 and more.
  • Live with double PEP 765 warnings. gh-139640: Fix swallowing syntax warnings in different modules (restore duplicated PEP 765 warnings) #139755
  • Live with sporadic swallowing of syntax warnings (Syntax warnings can be swallowed in import #139640) and incorrect module for filtering (Improve/fix filtering SyntaxWarnings by module #135801). The worst option.
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka

serhiy-storchaka commented Oct 8, 2025

Copy link
Copy Markdown
Member Author

The early PEP 765 warnings were indeed specified in the last paragraph of https://peps.python.org/pep-0765/#specification

This contrasts AST construction and execution of pre-compiled code. But there is a missed step -- the code generation from AST. You cannot normally execute AST.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka serhiy-storchaka changed the title gh-139640: Fix swallowing syntax warnings in different modules gh-139640: Fix swallowing syntax warnings in different modules (no PEP 765 warnings in ast.parse()) Oct 8, 2025
@serhiy-storchaka
Merge branch 'main' into compile-syntax-warnings
c643cdd
@serhiy-storchaka serhiy-storchaka changed the title gh-139640: Fix swallowing syntax warnings in different modules (no PEP 765 warnings in ast.parse()) gh-139640: Do not emit PEP 765 warnings in ast.parse() Oct 14, 2025
@serhiy-storchaka serhiy-storchaka changed the title gh-139640: Do not emit PEP 765 warnings in ast.parse() gh-131927: Do not emit PEP 765 warnings in ast.parse() Oct 14, 2025
@bedevere-app bedevere-app Bot mentioned this pull request Oct 14, 2025
Compilation emits multiple warnings in the finally block #131927
Closed
ncoghlan
ncoghlan approved these changes Oct 29, 2025
View reviewed changes

@ncoghlan ncoghlan left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per discussion at #139640 (comment), @iritkatriel and I both agree that given the unforeseen consequences of emitting the PEP 765 syntax warnings early, it makes the most sense to resolve this problem by emitting them in the opcode generation step alongside most of the other syntax warnings.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@ncoghlan
Merge branch 'main' into compile-syntax-warnings
325c1a9
@serhiy-storchaka
serhiy-storchaka merged commit ad0a3f7 into python:main Oct 30, 2025
4 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting merge label Oct 30, 2025
@miss-islington-app

miss-islington-app Bot commented Oct 30, 2025

Copy link
Copy Markdown

Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka
serhiy-storchaka deleted the compile-syntax-warnings branch October 30, 2025 11:00
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Oct 30, 2025
@serhiy-storchaka @miss-islington
pythongh-131927: Do not emit PEP 765 warnings in ast.parse() (pythonG…
c4862da
…H-139642)

ast.parse() no longer emits syntax warnings for
return/break/continue in finally (see PEP-765) -- they are only
emitted during compilation.
(cherry picked from commit ad0a3f7)

Co-authored-by: Serhiy Storchaka <[email protected]>
@miss-islington-app

miss-islington-app Bot commented Oct 30, 2025

Copy link
Copy Markdown

Sorry, @serhiy-storchaka, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker ad0a3f733b23e7fc69aff13055c7fac8ab9dcd66 3.13
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington-app miss-islington-app Bot assigned serhiy-storchaka Oct 30, 2025
@bedevere-app

bedevere-app Bot commented Oct 30, 2025

Copy link
Copy Markdown

GH-140786 is a backport of this pull request to the 3.14 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Oct 30, 2025
@serhiy-storchaka serhiy-storchaka removed the needs backport to 3.13 only security fixes label Oct 30, 2025
@serhiy-storchaka

serhiy-storchaka commented Oct 30, 2025

Copy link
Copy Markdown
Member Author

Thank you for review.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

serhiy-storchaka added a commit that referenced this pull request Oct 30, 2025
@miss-islington @serhiy-storchaka
[3.14] gh-131927: Do not emit PEP 765 warnings in ast.parse() (GH-139642
a7cfe86
) (GH-140786)

ast.parse() no longer emits syntax warnings for
return/break/continue in finally (see PEP-765) -- they are only
emitted during compilation.
(cherry picked from commit ad0a3f7)

Co-authored-by: Serhiy Storchaka <[email protected]>
StanFromIreland pushed a commit to StanFromIreland/cpython that referenced this pull request Dec 6, 2025
@serhiy-storchaka @StanFromIreland
pythongh-131927: Do not emit PEP 765 warnings in ast.parse() (pythonG…
efb8be8
…H-139642)

ast.parse() no longer emits syntax warnings for
return/break/continue in finally (see PEP-765) -- they are only
emitted during compilation.
@serhiy-storchaka serhiy-storchaka removed their assignment Jul 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@ncoghlan ncoghlan ncoghlan approved these changes
@Eclips4 Eclips4 Eclips4 approved these changes
@pablogsal pablogsal Awaiting requested review from pablogsal pablogsal is a code owner
@lysnikolaou lysnikolaou Awaiting requested review from lysnikolaou lysnikolaou is a code owner
@ambv ambv Awaiting requested review from ambv ambv is a code owner
@markshannon markshannon Awaiting requested review from markshannon markshannon is a code owner
@iritkatriel iritkatriel Awaiting requested review from iritkatriel iritkatriel is a code owner
@isidentical isidentical Awaiting requested review from isidentical isidentical is a code owner
@tomasr8 tomasr8 Awaiting requested review from tomasr8 tomasr8 is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@serhiy-storchaka @ncoghlan @iritkatriel @Eclips4
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.