FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-131927: Do not emit PEP 765 warnings in ast.parse() by serhiy-storchaka · Pull Request #139642 · python/cpython · GitHub

/ cpython Public

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

Merged
serhiy-storchaka merged 4 commits into
python:mainfrom
serhiy-storchaka:compile-syntax-warnings
Oct 30, 2025
Merged

gh-131927: Do not emit PEP 765 warnings in ast.parse()#139642
serhiy-storchaka merged 4 commits into
python:mainfrom
serhiy-storchaka:compile-syntax-warnings

Conversation

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

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.

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 force-pushed the compile-syntax-warnings branch from 2fa4266 to c04d874 Compare October 6, 2025 07:34

Eclips4 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

The AST changes are LGTM.
Thank you Serhiy.

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.

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.

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?

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().

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

Copy link
Copy Markdown
Member Author

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

Copy link
Copy Markdown
Member

Could be with_warnings or something like that.

Copy link
Copy Markdown
Member Author

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

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.

Copy link
Copy Markdown
Member

Why not all warnings?

ncoghlan commented Oct 7, 2025
edited
Loading

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.

serhiy-storchaka commented Oct 8, 2025
edited
Loading

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:

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.

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

ncoghlan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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-storchaka merged commit ad0a3f7 into python:main Oct 30, 2025
46 checks passed

Copy link
Copy Markdown

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

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
…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 <storchaka@gmail.com>

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

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.

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

Copy link
Copy Markdown
Member Author

Thank you for review.

serhiy-storchaka added a commit that referenced this pull request Oct 30, 2025
) (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 <storchaka@gmail.com>
StanFromIreland pushed a commit to StanFromIreland/cpython that referenced this pull request Dec 6, 2025
…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 removed their assignment Jul 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants


Back | FazBrowse Home | New Git URL