@@ -183,9 +183,9 @@ def unescape(self, line: str) -> str:
183183def handle_line_ending_and_magic (func ):
184184 @wraps (func )
185185 def wrapped (self , code : str , notebook : bool , ** options ) -> str :
186- if any (code . startswith ( f"% { lang } " ) for lang in INCOMPATIBLE_MAGIC_LANGUAGES ) or any (
187- code .startswith (f"%% { lang } " ) for lang in INCOMPATIBLE_MAGIC_LANGUAGES
188- ):
186+ if any (
187+ code .startswith (f"%{ lang } " ) for lang in INCOMPATIBLE_MAGIC_LANGUAGES
188+ ) or any ( code . startswith ( f"%% { lang } " ) for lang in INCOMPATIBLE_MAGIC_LANGUAGES ) :
189189 logger .info ("Non compatible magic language cell block detected, ignoring." )
190190 return code
191191
@@ -382,7 +382,9 @@ def format_code(self, code: str, notebook: bool, **options) -> str:
382382 from rpy2 .robjects import conversion , default_converter
383383
384384 with conversion .localconverter (default_converter ):
385- format_r = rpackages .importr (self .package_name , robject_translations = {".env" : "env" })
385+ format_r = rpackages .importr (
386+ self .package_name , robject_translations = {".env" : "env" }
387+ )
386388 formatted_code = format_r .tidy_source (text = code , output = False , ** options )
387389 return "\n " .join (formatted_code [0 ])
388390
@@ -398,7 +400,9 @@ def format_code(self, code: str, notebook: bool, **options) -> str:
398400
399401 with conversion .localconverter (default_converter ):
400402 styler_r = rpackages .importr (self .package_name )
401- formatted_code = styler_r .style_text (code , ** self ._transform_options (styler_r , options ))
403+ formatted_code = styler_r .style_text (
404+ code , ** self ._transform_options (styler_r , options )
405+ )
402406 return "\n " .join (formatted_code )
403407
404408 @staticmethod
@@ -418,13 +422,17 @@ def _transform_options(styler_r, options):
418422
419423 if "reindention" in transformed_options :
420424 if isinstance (options ["reindention" ], dict ):
421- transformed_options ["reindention" ] = rpy2 .robjects .ListVector (options ["reindention" ])
425+ transformed_options ["reindention" ] = rpy2 .robjects .ListVector (
426+ options ["reindention" ]
427+ )
422428 else :
423429 transformed_options ["reindention" ] = rpy2 .robjects .ListVector (
424430 getattr (styler_r , options ["reindention" ])()
425431 )
426432 return transformed_options
427433
434+ class FormatterError (Exception ):
435+ pass
428436
429437class CommandLineFormatter (BaseFormatter ):
430438 command : List [str ]
@@ -441,7 +449,9 @@ def importable(self) -> bool:
441449 return command_exist (self .command [0 ])
442450
443451 @handle_line_ending_and_magic
444- def format_code (self , code : str , notebook : bool , args : List [str ] = [], ** options ) -> str :
452+ def format_code (
453+ self , code : str , notebook : bool , args : List [str ] = [], ** options
454+ ) -> str :
445455 process = subprocess .run (
446456 self .command + args ,
447457 input = code ,
@@ -451,9 +461,14 @@ def format_code(self, code: str, notebook: bool, args: List[str] = [], **options
451461 )
452462
453463 if process .stderr :
454- logger .info (f"An error with { self .command [0 ]} has occurred:" )
455- logger .info (process .stderr )
456- return code
464+ raise FormatterError (
465+ f"Error running `{ self .command [0 ]} `:\n " + process .stderr
466+ )
467+ elif process .returncode != 0 :
468+ raise FormatterError (
469+ f"Formatter `{ self .command [0 ]} ` exited with status { process .returncode } "
470+ + process .stderr
471+ )
457472 else :
458473 return process .stdout
459474
0 commit comments