Skip to content

Commit 44c07c8

Browse files
committed
Delay rpy2 import until the formatter is requested
Restore `rpy2.robjects` import
1 parent 8e3ed8a commit 44c07c8

File tree

1 file changed

+19
-26
lines changed

1 file changed

+19
-26
lines changed

jupyterlab_code_formatter/formatters.py

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,11 @@
99
from functools import wraps
1010
from typing import List, Type
1111

12-
try:
13-
import rpy2
14-
import rpy2.robjects
15-
except ImportError:
16-
pass
1712
if sys.version_info >= (3, 9):
1813
from functools import cache
1914
else:
2015
from functools import lru_cache
16+
2117
cache = lru_cache(maxsize=None)
2218

2319
from packaging import version
@@ -357,45 +353,41 @@ def format_code(self, code: str, notebook: bool, **options) -> str:
357353
return isort.code(code=code, **options)
358354

359355

360-
class FormatRFormatter(BaseFormatter):
361-
label = "Apply FormatR Formatter"
362-
package_name = "formatR"
356+
class RFormatter(BaseFormatter):
357+
@property
358+
@abc.abstractmethod
359+
def package_name(self) -> str:
360+
pass
363361

364362
@property
365363
def importable(self) -> bool:
366-
try:
367-
import rpy2.robjects.packages as rpackages
364+
package_location = subprocess.run(
365+
["Rscript", "-e", f"cat(system.file(package='{self.package_name}'))"],
366+
capture_output=True,
367+
text=True,
368+
)
369+
return package_location != ""
368370

369-
rpackages.importr(self.package_name, robject_translations={".env": "env"})
370371

371-
return True
372-
except Exception:
373-
return False
372+
class FormatRFormatter(RFormatter):
373+
label = "Apply FormatR Formatter"
374+
package_name = "formatR"
374375

375376
@handle_line_ending_and_magic
376377
def format_code(self, code: str, notebook: bool, **options) -> str:
378+
# while not used, this import is needed to load conversion rules
379+
import rpy2.robjects
377380
import rpy2.robjects.packages as rpackages
378381

379382
format_r = rpackages.importr(self.package_name, robject_translations={".env": "env"})
380383
formatted_code = format_r.tidy_source(text=code, output=False, **options)
381384
return "\n".join(formatted_code[0])
382385

383386

384-
class StylerFormatter(BaseFormatter):
387+
class StylerFormatter(RFormatter):
385388
label = "Apply Styler Formatter"
386389
package_name = "styler"
387390

388-
@property
389-
def importable(self) -> bool:
390-
try:
391-
import rpy2.robjects.packages as rpackages
392-
393-
rpackages.importr(self.package_name)
394-
395-
return True
396-
except Exception:
397-
return False
398-
399391
@handle_line_ending_and_magic
400392
def format_code(self, code: str, notebook: bool, **options) -> str:
401393
import rpy2.robjects.packages as rpackages
@@ -407,6 +399,7 @@ def format_code(self, code: str, notebook: bool, **options) -> str:
407399
@staticmethod
408400
def _transform_options(styler_r, options):
409401
transformed_options = copy.deepcopy(options)
402+
import rpy2.robjects
410403

411404
if "math_token_spacing" in transformed_options:
412405
if isinstance(options["math_token_spacing"], dict):

0 commit comments

Comments
 (0)