pyrefly
pyrefly copied to clipboard
Never/NoReturn narrowing
Describe the Bug
from typing import *
def raise_error() -> NoReturn:
raise Exception()
a = cast(int | None, 1)
if a is None:
raise_error()
reveal_type(a) # revealed type: int | None
I expect a to be narrowed to int.
Sandbox Link
https://pyrefly.org/sandbox/?code=GYJw9gtgBALgngBwJYDsDmUkQWEMoBUAUEQCYCmwUIAhkgM7kD65I4IAFAJRQC0AfFAByYAErkYAVxAoAXESiLqdRlACiADwDG5BDCRgU3EjSgBeKFpr0YHVPgA+ww+QA0UAIxciSKqYbOKOTySsoMzKzsxkQg5ABu5DQANkzwCOQcNFxAA
(Only applicable for extension issues) IDE Information
No response
I believe this is a gap in our control flow handling, where calls to NoReturn functions are not handled properly.
I've taken a brief look at the issue.
Currently, we are having control flow in binding builder, and we mark a flow as terminated using self.scopes.mark_flow_termination(); in the following scenarios:
returnraiseassertwith a false condition- calling
sys.exit(),exit(),quit()oros.exit()
The tricky part of implementing control flow termination for functions with return types Never and NoReturn is that typing information is not available during binding building.
We have some ideas about an approach that might work
I'll tentatively claim this: I'm looking at unrelated flow merging changes related to narrowing, and this would be a logical thing to do soon after. I don't think there's much chance I'll get to it for at least a week, but it probably doesn't make sense for anyone else to work on it right now
This issue has someone assigned, but has not had recent activity for more than 2 weeks.
If you are still working on this issue, please add a comment so everyone knows. Otherwise, please unassign yourself and allow someone else to take over.
Thank you for your contributions!
Another example: https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeS4ATrgLYAEALqcROgOZ0Q3G6UN0AqADroRmGGDphcuABQBKRCLorGlUkvSrtdOHQC8dISFRxMx5apj4AxjGL8AorfsMIudJp2rsqSgpFLFWJKVgZZOHlArAk6X395OgBaAD46ADlcACUYBgBXSk8gukpUCDgYOmc7B3d0APQQABoQMkoJKFJCBlooCgBiOgAFUnawTt0MHAI6Gw9INgLUNw9CEUGAZRhKgAsGBmI4RAB6Y7aOrt42Y5h0Y8xcGzhjufQFpZW7qV46VAA3MpQVDYWCzeYQRalT50XC1DxwNboMgMHYeJJ-GCUOB1AxGEAAZkIAEYAEwWRoAXxaqBsbgxADFoDAKGgsHgiGQQBSgA
This issue has someone assigned, but has not had recent activity for more than 2 weeks.
If you are still working on this issue, please add a comment so everyone knows. Otherwise, please unassign yourself and allow someone else to take over.
Thank you for your contributions!