Fix blackd error handling: split SourceASTParseError from ASTSafetyError#5080
Open
Fix blackd error handling: split SourceASTParseError from ASTSafetyError#5080
Conversation
- Add SourceASTParseError exception for source file AST parse failures (user input issue, not a Black bug) - Keep ASTSafetyError for Black output issues (invalid code, non-equivalent) - In blackd, return 400 for SourceASTParseError, 500 for ASTSafetyError - Per @JelleZijlstra review feedback on psf#5074
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Per @JelleZijlstra's review feedback on #5074, this PR splits the error handling instead of treating all ASTSafetyError as 400.
Changes
New exception:
SourceASTParseError(src/black/parsing.py)ast.parse()but was accepted by Black's more lenient lib2to3 parserUpdated
assert_equivalent(src/black/__init__.py)SourceASTParseError(user input issue)ASTSafetyError(Black bug → 500)ASTSafetyError(Black bug → 500)Updated blackd (
src/blackd/__init__.py)SourceASTParseError→ 400 Bad RequestASTSafetyError→ 500 Internal Server Error (unchanged, caught by generic Exception handler)Closes
Fixes #4332
Analysis of all ASTSafetyError uses
assert_equivalentsrc AST parse failSourceASTParseErrorassert_equivalentdest AST parse failASTSafetyErrorassert_equivalentsrc ≠ dstASTSafetyErrorcheck_stability_and_equivalencere-raise