Skip to content

Change of binder behavior whan narrowing to unrelated type #18967

Description

@JukkaL

The behavior in this example was changed in #18538:

def any():
    return 1

def f() -> None:
    x = "a"
    x = any()
    assert isinstance(x, int)
    reveal_type(x)  # "Never", but used to be unreachable (no message)

In the past, the code after isinstance was silently unchecked. Now the inferred type is Never, which will likely generate false positives. Neither behavior is great, but users may consider the switch from false negatives to false positives to be a regression. I'm not sure what we should do here. If we'd allow variables to be redefined freely, there would be no issue.

cc @ilevkivskyi who authored the PR that changed the behavior

Activity

  1. A5rocks commented on Apr 25, 2025

    @A5rocks
    Collaborator

    In #18707 I changed things such that narrowing to Never (or assigning it in any way, really) would mark a block as unreachable. That seemed to work decently well.

    I would be fine to extract that if it's a good idea.

  2. ilevkivskyi commented on Apr 26, 2025

    @ilevkivskyi
    Member

    Marking this code unreachable is a possible solution, but it may cause undesired behavior in other cases. Another possible option would be to replace Never with known restriction (i.e. int in this example), it will likely have a more limited impact. I can try this now, and see if there any adverse effects.

  3. ilevkivskyi commented on Apr 29, 2025

    @ilevkivskyi
    Member

    I will double-check (superficially good) fallout in #18972 later today or tomorrow.

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-type-narrowingConditional type narrowing / binder

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions