Skip to content

mypy duplicates reveal_type notes with partial types #18511

Closed

Description

@A5rocks

Bug Report

There's two notes from a single reveal_type with a partial type in a loop.

To Reproduce

def main() -> None:
    x = None
    while True:
        reveal_type(x)
        x = ""

(the outer function is only necessary to make sure a transition to --local-partial-types doesn't render this test case useless)

Expected Behavior

main.py:4: note: Revealed type is "Union[builtins.str, None]"
Success: no issues found in 1 source file

Actual Behavior

main.py:4: note: Revealed type is "None"
main.py:4: note: Revealed type is "Union[builtins.str, None]"
Success: no issues found in 1 source file

Your Environment

I assume this was caused by #18180. Checked on mypy playground.

  • Mypy version used: master
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.12

Activity

  1. tyralla commented on Jan 23, 2025

    @tyralla
    Collaborator

    This could also happen before #18180 (when loops were repeatedly checked for other reasons), but you are right that #18180 increases the number of cases where this can happen. I realised this when working on it but was unsure whether this behaviour was a feature or a bug. The current behaviour tells us that the type must be None in the first and can be either None or str in subsequent iterations. If there is consent that this is more confusing than helpful, we could likely extend the logic of #18433 to remove "early" results.

  2. A5rocks commented on Jan 23, 2025

    @A5rocks
    CollaboratorAuthor

    I think it's worth it to stop these weird results by preventing early outputs from showing. (I think it's surprising more than confusing but surprising isn't a fun place to be.)

    That is unless #18516 includes redoing partial types.

  3. tyralla commented on Jan 24, 2025

    @tyralla
    Collaborator

    I guess #18516 would not remove the need to check some loops repeatedly and, hence, would not fix this issue automatically. Additionally, I suppose, with --allow-redefinition disabled, things could generally stay as they are. But @JukkaL should know this more certainly.

  4. added a commit that references this issue on Jun 6, 2025
    55c4067
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

    bugmypy got something wrongtopic-reveal-typereveal_type() and reveal_locals()

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions