pyrefly icon indicating copy to clipboard operation
pyrefly copied to clipboard

Never/NoReturn narrowing

Open Zaczero opened this issue 5 months ago • 1 comments
trafficstars

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

Zaczero avatar May 26 '25 08:05 Zaczero

I believe this is a gap in our control flow handling, where calls to NoReturn functions are not handled properly.

yangdanny97 avatar May 27 '25 13:05 yangdanny97

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:

  • return
  • raise
  • assert with a false condition
  • calling sys.exit(), exit(), quit() or os.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.

fangyi-zhou avatar Aug 12 '25 23:08 fangyi-zhou

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

stroxler avatar Sep 19 '25 18:09 stroxler

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!

github-actions[bot] avatar Oct 04 '25 00:10 github-actions[bot]

Another example: https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeS4ATrgLYAEALqcROgOZ0Q3G6UN0AqADroRmGGDphcuABQBKRCLorGlUkvSrtdOHQC8dISFRxMx5apj4AxjGL8AorfsMIudJp2rsqSgpFLFWJKVgZZOHlArAk6X395OgBaAD46ADlcACUYBgBXSk8gukpUCDgYOmc7B3d0APQQABoQMkoJKFJCBlooCgBiOgAFUnawTt0MHAI6Gw9INgLUNw9CEUGAZRhKgAsGBmI4RAB6Y7aOrt42Y5h0Y8xcGzhjufQFpZW7qV46VAA3MpQVDYWCzeYQRalT50XC1DxwNboMgMHYeJJ-GCUOB1AxGEAAZkIAEYAEwWRoAXxaqBsbgxADFoDAKGgsHgiGQQBSgA

yangdanny97 avatar Oct 15 '25 15:10 yangdanny97

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!

github-actions[bot] avatar Nov 15 '25 00:11 github-actions[bot]