-
Notifications
You must be signed in to change notification settings - Fork 282
Closed
Closed
Copy link
Labels
scoping-control-flowissues related to scoping and control flowissues related to scoping and control flowtypechecking
Description
Describe the Bug
Observed Behavior (with Pyrefly):
When analyzing Python match statements, Pyrefly currently reports no errors under the following conditions where issues are present:
- Non-exhaustive matching: When
casestatements within amatchblock do not collectively handle all possible members of anEnumor all types within aUnionthat the matched variable could be. - Incorrect syntax for Enum member case patterns: When
Enummembers used incasepatterns are incorrectly written with parentheses as if they were being called (e.g.,MyEnum.MEMBER()instead of the correct syntaxMyEnum.MEMBER).
This behavior implies Pyrefly currently considers these potentially problematic patterns as valid.
Expected Behavior:
Pyrefly should identify and report errors in the scenarios described above to help developers catch potential bugs and ensure code correctness:
- For non-exhaustive matching: Pyrefly should issue an error or warning, ideally specifying which
Enummembers orUniontypes are not handled by anycasestatement. - For incorrect Enum syntax: Pyrefly should report a syntax or type error when
Enummembers are incorrectly used with parentheses incasepatterns (e.g., similar to Pyright's"Literal[...] is not a class"report).
This would align Pyrefly's strictness with other type checkers and provide more robust static analysis for match statements.
Pyright's Output (for reference, from the original user report):
/Users/davi/gits/chief-app/test_types.py
/Users/davi/gits/chief-app/test_types.py:21:11 - error: Cases within match statement do not exhaustively handle all values
Unhandled type: "OrderStatus"
If exhaustive handling is not intended, add "case _: pass" (reportMatchNotExhaustive)
/Users/davi/gits/chief-app/test_types.py:22:14 - error: "Literal[OrderStatus.Ready]" is not a class (reportGeneralTypeIssues)
/Users/davi/gits/chief-app/test_types.py:24:14 - error: "Literal[OrderStatus.Shipped]" is not a class (reportGeneralTypeIssues)
/Users/davi/gits/chief-app/test_types.py:30:11 - error: Cases within match statement do not exhaustively handle all values
Unhandled type: "Literal[OrderStatus.Scheduled]"
If exhaustive handling is not intended, add "case _: pass" (reportMatchNotExhaustive)
/Users/davi/gits/chief-app/test_types.py:52:11 - error: Cases within match statement do not exhaustively handle all values
Unhandled type: "int"
If exhaustive handling is not intended, add "case _: pass" (reportMatchNotExhaustive)
5 errors, 0 warnings, 0 information
Steps to Reproduce:
- Prepare the code: Save the Python code snippet provided in the "Sandbox Link" section of this GitHub issue (which contains functions
handle_order_1,handle_order_2, andhandle) into a local file (e.g.,test_match_issues.py). This code demonstrates the non-exhaustive matches and incorrect Enum syntax. - Analyze with Pyrefly: Run
pyreflyon the created file:pyrefly check test_match_issues.py
- Observe Pyrefly's output: Notice that Pyrefly does not report any errors for the problematic
matchstatements within thehandle_order_1,handle_order_2, orhandlefunctions. - (Optional Comparison) Analyze with Pyright: Run
pyrighton the same file:pyright test_match_issues.py
- Observe Pyright's output: Notice that Pyright correctly identifies and reports multiple errors related to non-exhaustiveness and incorrect enum case patterns for the same functions, as detailed in the "Pyright's Output" section of this issue. This highlights the behavior expected from Pyrefly.
Sandbox Link
(Only applicable for extension issues) IDE Information
No response
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
scoping-control-flowissues related to scoping and control flowissues related to scoping and control flowtypechecking