Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-131927: Do not emit PEP 765 warnings in ast.parse()#139642

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged

Conversation

@serhiy-storchaka
Copy link
Member

@serhiy-storchakaserhiy-storchaka commentedOct 6, 2025
edited by bedevere-appbot
Loading

RevertGH-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 (seePEP-765) -- they are only emitted during compilation.

RevertpythonGH-131993.Fix swallowing some syntax warnings in different modules if they accidentallyhave the same message and are emitted from the same line.ast.parse() no longer emits syntax warnings forreturn/break/continue in finally (see PEP-765) -- they are onlyemitted during compilation.
Copy link
Member

@Eclips4Eclips4 left a 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.

@ncoghlan
Copy link
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.

@serhiy-storchaka
Copy link
MemberAuthor

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

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, inast.parse() andcompile(). Currently, this is only the PEP 765 warnings.
  • Emitted in the code generator. They are only emitted if you usecompile().

So, you already see not all warnings if you only useast.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.

@iritkatriel
Copy link
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?

@serhiy-storchaka
Copy link
MemberAuthor

We can, but

  • This is a new feature. It can perhaps be backported to 3.14.1 as a special exception.
  • Every user ofast.parse() andcompile() 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 inast.parse().

@iritkatriel
Copy link
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).

@serhiy-storchaka
Copy link
MemberAuthor

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

@iritkatriel
Copy link
Member

Could bewith_warnings or something like that.

@serhiy-storchaka
Copy link
MemberAuthor

No, because it should not control all warnings. Maybe_enable_pep_765_warnings?

@serhiy-storchaka
Copy link
MemberAuthor

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.

@iritkatriel
Copy link
Member

Why not all warnings?

@ncoghlan
Copy link
Contributor

ncoghlan commentedOct 7, 2025
edited
Loading

The early PEP 765 warnings were indeed specified in the last paragraph ofhttps://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.

@serhiy-storchaka
Copy link
MemberAuthor

serhiy-storchaka commentedOct 8, 2025
edited
Loading

Why not all warnings?

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

If we silenceall warnings inast.parse() orcompile(), 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:

@serhiy-storchaka
Copy link
MemberAuthor

The early PEP 765 warnings were indeed specified in the last paragraph ofhttps://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.

@serhiy-storchakaserhiy-storchaka changed the titlegh-139640: Fix swallowing syntax warnings in different modulesgh-139640: Fix swallowing syntax warnings in different modules (no PEP 765 warnings in ast.parse())Oct 8, 2025
@serhiy-storchakaserhiy-storchaka changed the titlegh-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-storchakaserhiy-storchaka changed the titlegh-139640: Do not emit PEP 765 warnings in ast.parse()gh-131927: Do not emit PEP 765 warnings in ast.parse()Oct 14, 2025
Copy link
Contributor

@ncoghlanncoghlan left a 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.

@serhiy-storchakaserhiy-storchaka merged commitad0a3f7 intopython:mainOct 30, 2025
46 checks passed
@miss-islington-app
Copy link

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

@serhiy-storchakaserhiy-storchaka deleted the compile-syntax-warnings branchOctober 30, 2025 11:00
miss-islington pushed a commit to miss-islington/cpython that referenced this pull requestOct 30, 2025
…H-139642)ast.parse() no longer emits syntax warnings forreturn/break/continue in finally (see PEP-765) -- they are onlyemitted during compilation.(cherry picked from commitad0a3f7)Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
@miss-islington-app
Copy link

Sorry,@serhiy-storchaka, I could not cleanly backport this to3.13 due to a conflict.
Please backport usingcherry_picker on command line.

cherry_picker ad0a3f733b23e7fc69aff13055c7fac8ab9dcd66 3.13

@bedevere-app
Copy link

GH-140786 is a backport of this pull request to the3.14 branch.

@bedevere-appbedevere-appbot removed the needs backport to 3.14bugs and security fixes labelOct 30, 2025
@serhiy-storchakaserhiy-storchaka removed the needs backport to 3.13bugs and security fixes labelOct 30, 2025
@serhiy-storchaka
Copy link
MemberAuthor

Thank you for review.

serhiy-storchaka added a commit that referenced this pull requestOct 30, 2025
) (GH-140786)ast.parse() no longer emits syntax warnings forreturn/break/continue in finally (seePEP-765) -- they are onlyemitted during compilation.(cherry picked from commitad0a3f7)Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
StanFromIreland pushed a commit to StanFromIreland/cpython that referenced this pull requestDec 6, 2025
…H-139642)ast.parse() no longer emits syntax warnings forreturn/break/continue in finally (see PEP-765) -- they are onlyemitted during compilation.
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@ncoghlanncoghlanncoghlan approved these changes

@Eclips4Eclips4Eclips4 approved these changes

@pablogsalpablogsalAwaiting requested review from pablogsalpablogsal is a code owner

@lysnikolaoulysnikolaouAwaiting requested review from lysnikolaoulysnikolaou is a code owner

@ambvambvAwaiting requested review from ambvambv is a code owner

@markshannonmarkshannonAwaiting requested review from markshannonmarkshannon is a code owner

@iritkatrieliritkatrielAwaiting requested review from iritkatrieliritkatriel is a code owner

@isidenticalisidenticalAwaiting requested review from isidenticalisidentical is a code owner

@tomasr8tomasr8Awaiting requested review from tomasr8tomasr8 is a code owner

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

4 participants

@serhiy-storchaka@ncoghlan@iritkatriel@Eclips4

[8]ページ先頭

©2009-2025 Movatter.jp