Skip to content

Change of binder behavior whan narrowing to unrelated type #18967

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
JukkaL opened this issue Apr 25, 2025 · 2 comments · May be fixed by #18972
Open

Change of binder behavior whan narrowing to unrelated type #18967

JukkaL opened this issue Apr 25, 2025 · 2 comments · May be fixed by #18972
Labels
bug mypy got something wrong topic-type-narrowing Conditional type narrowing / binder

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Apr 25, 2025

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

@JukkaL JukkaL added the bug mypy got something wrong label Apr 25, 2025
@A5rocks
Copy link
Collaborator

A5rocks commented Apr 25, 2025

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.

@brianschubert brianschubert added the topic-type-narrowing Conditional type narrowing / binder label Apr 25, 2025
@ilevkivskyi
Copy link
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.

@ilevkivskyi ilevkivskyi linked a pull request Apr 26, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-type-narrowing Conditional type narrowing / binder
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants