@@ -100,6 +100,21 @@ def format_text(*, text, config, lines):
100100 magic_trailing_comma = not config ["skip_magic_trailing_comma" ],
101101 preview = config ["preview" ],
102102 )
103+ handled_errors = (
104+ # raised when the file has syntax errors
105+ ValueError ,
106+ # raised when the file being formatted has an indentation error
107+ IndentationError ,
108+ # raised when black produces invalid Python code or formats the file
109+ # differently on the second pass
110+ black .parsing .ASTSafetyError ,
111+ )
112+ # SourceASTParseError was split out from ASTSafetyError in black 26.5.0,
113+ # https://github.com/psf/black/pull/5080; expect it if it exists.
114+ try :
115+ handled_errors += (black .parsing .SourceASTParseError ,)
116+ except AttributeError :
117+ pass
103118 try :
104119 # Black's format_file_contents only works reliably when eols are '\n'. It gives
105120 # an error for '\r' and produces wrong formatting for '\r\n'. So we replace
@@ -120,15 +135,7 @@ def format_text(*, text, config, lines):
120135 formatted_text = formatted_text .replace ("\n " , eol_chars )
121136
122137 return formatted_text
123- except (
124- # raised when the file has syntax errors
125- ValueError ,
126- # raised when the file being formatted has an indentation error
127- IndentationError ,
128- # raised when black produces invalid Python code or formats the file
129- # differently on the second pass
130- black .parsing .ASTSafetyError ,
131- ) as e :
138+ except handled_errors as e :
132139 # errors will show on lsp stderr stream
133140 logger .error ("Error formatting with black: %s" , e )
134141 raise black .NothingChanged from e
0 commit comments