Skip to content

Compilation emits multiple warnings in the finally block #131927

Closed

Description

@sobolevn

Bug report

This code produces two SyntaxWarnings in the new REPL:

>>> def some():
...     try:
...         return 1
...     finally:
...         return 2
...         
<python-input-14>:5: SyntaxWarning: 'return' in a 'finally' block
<python-input-14>:5: SyntaxWarning: 'return' in a 'finally' block
Image

But, in our old REPL it produces just one:

Image

Other SyntaxWarnings also do not show the same behavior in the new REPL. Example:

Image

So, looks like only PEP-765 is affected, only in the new REPL.

CC @ambv @iritkatriel

Linked PRs

Activity

  1. iritkatriel commented on Mar 31, 2025

    @iritkatriel
    Member

    Or a way to turn off warnings coming from ast_opt? Though that also seems like overkill for such a niche use case.

    Possibly, particularly if we can add it as an internal option (i.e., not expose it via the documented API).

  2. iritkatriel commented on Mar 31, 2025

    @iritkatriel
    Member

    Yet another option is to leave ast_opt alone, and handle this in the repl. So the repl would save the warnings from the ast construction and then filter out duplicated warnings from the compilation call.

  3. tomasr8 commented on Mar 31, 2025

    @tomasr8
    Member

    I like the last option the best since I'd also like to avoid touching ast_opt for this.

  4. serhiy-storchaka commented on Apr 8, 2025

    @serhiy-storchaka
    Member

    We have the same issue in 3.12:

    >>> try:
    ...     pass
    ... finally:
    ...     assert(1, 'message')
    ... 
    <stdin>:4: SyntaxWarning: assertion is always true, perhaps remove parentheses?
    <stdin>:4: SyntaxWarning: assertion is always true, perhaps remove parentheses?
  5. serhiy-storchaka commented on Apr 8, 2025

    @serhiy-storchaka
    Member

    It has no relation to REPL, it can be reproduced with compile().

    >>> compile('''
    ... try:
    ...     pass
    ... finally:
    ...     assert(1, 'message')
    ... ''', '<string>', 'exec')
    <string>:5: SyntaxWarning: assertion is always true, perhaps remove parentheses?
    <string>:5: SyntaxWarning: assertion is always true, perhaps remove parentheses?
    <code object <module> at 0x7f15a8b5c130, file "<string>", line 1>
    
  6. changed the title [-]PEP 765 produces two warnings in the new REPL[/-] [+]Compilation emits multiple warnings in the finally block[/+] on Apr 8, 2025
  7. tomasr8 commented on Apr 8, 2025

    @tomasr8
    Member

    Just adding a link here to my comment explaining why the warning is emitted more than once: #131993 (comment)

    The tl;dr is that _PyErr_EmitSyntaxWarning bypasses the warning registry. As a consequence, the warnings are not deduplicated.

  8. removed
    stdlibStandard Library Python modules in the Lib/ directory
    topic-replRelated to the interactive shell
    on Apr 8, 2025
  9. iritkatriel commented on Apr 8, 2025

    @iritkatriel
    Member

    It has no relation to REPL, it can be reproduced with compile().

    That looks like a different issue related to assert.

    Try this:

    >>> src1 = '''def f():
    ...   try:
    ...     pass
    ...   finally:
    ...     return  42'''
    >>> 
    >>> compile(src1, "", "exec")
    :5: SyntaxWarning: 'return' in a 'finally' block
    <code object <module> at 0x10db8a440, file "", line 1>
    >>> 
    
    
  10. added a commit that references this issue on Apr 12, 2025
  11. serhiy-storchaka commented on Apr 12, 2025

    @serhiy-storchaka
    Member

    There were at least three different bugs:

    • Compiling the finally block could emit two warnings, because the code in the finally block is compiled twice.
    • Repeated compile() calls emitted repeated warnings.
    • New REPL also emits repeated PEP-765 related warnings.

    The PR fixed them all. Maybe backport it, as the first two issues are old?

  12. added 3 commits that reference this issue on Apr 13, 2025
  13. added a commit that references this issue on Apr 13, 2025
  14. serhiy-storchaka commented on Oct 6, 2025

    @serhiy-storchaka
    Member

    I realized that this was incorrect solution. See #139640.

  15. serhiy-storchaka commented on Oct 14, 2025

    @serhiy-storchaka
    Member

    #139642 and #139719 are two alternative solutions. The former makes ast.parse() not emitting warnings. The simply silences it in the REPR (the original solution by @tomasr8). It may be incomplete because we may need to silence warnings in other places of the stdlib, not mention a third-party code.

  16. ncoghlan commented on Oct 29, 2025

    @ncoghlan
    Contributor

    @serhiy-storchaka Based on the discussion in #139640, I posted an approving review on #139642, so I think we can close #139719 in favour of the more straightforward (and comprehensive) solution.

  17. added a commit that references this issue on Oct 30, 2025
  18. added a commit that references this issue on Oct 30, 2025
  19. added a commit that references this issue on Oct 30, 2025
  20. added a commit that references this issue on Dec 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.12only security fixes3.13only security fixes3.14bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions