From 9e33f2ed350973b4e657634290e7d75824d635e4 Mon Sep 17 00:00:00 2001 From: Talley Lambert Date: Sat, 23 Aug 2025 09:07:03 -0400 Subject: [PATCH 1/2] refactor: delay import of pandas until needed --- src/scyjava/_convert.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/scyjava/_convert.py b/src/scyjava/_convert.py index af1583b5..c73f8b7a 100644 --- a/src/scyjava/_convert.py +++ b/src/scyjava/_convert.py @@ -7,8 +7,9 @@ import logging import math from bisect import insort +from importlib.util import find_spec from pathlib import Path -from typing import Any, Callable, Dict, List, NamedTuple +from typing import Any, Callable, Dict, List, NamedTuple, reveal_type from jpype import JBoolean, JByte, JChar, JDouble, JFloat, JInt, JLong, JShort @@ -677,7 +678,7 @@ def _stock_py_converters() -> List: priority=Priority.VERY_LOW, ), ] - if _import_pandas(required=False): + if find_spec("pandas"): converters.append( Converter( name="org.scijava.table.Table -> pandas.DataFrame", @@ -716,7 +717,7 @@ def _stock_py_converters() -> List: ), ] ) - if _import_numpy(required=False): + if find_spec("numpy"): converters.append( Converter( name="primitive array -> numpy.ndarray", @@ -803,16 +804,15 @@ def _jarray_shape(jarr): return shape -def _import_numpy(required=True): +def _import_numpy(): try: import numpy as np return np except ImportError as e: - if required: - msg = "The NumPy library is missing (https://numpy.org/). " - msg += "Please install it before using this function." - raise RuntimeError(msg) from e + msg = "The NumPy library is missing (https://numpy.org/). " + msg += "Please install it before using this function." + raise RuntimeError(msg) from e ###################################### @@ -838,16 +838,15 @@ def _convert_table(obj: Any): return None -def _import_pandas(required=True): +def _import_pandas(): try: import pandas as pd return pd except ImportError as e: - if required: - msg = "The Pandas library is missing (http://pandas.pydata.org/). " - msg += "Please install it before using this function." - raise RuntimeError(msg) from e + msg = "The Pandas library is missing (http://pandas.pydata.org/). " + msg += "Please install it before using this function." + raise RuntimeError(msg) from e def _table_to_pandas(table): From 6f89935091be21f25af75214d6d28de1efcd68b2 Mon Sep 17 00:00:00 2001 From: Talley Lambert Date: Sat, 23 Aug 2025 09:10:47 -0400 Subject: [PATCH 2/2] remove reveal type --- src/scyjava/_convert.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/scyjava/_convert.py b/src/scyjava/_convert.py index c73f8b7a..4a04f279 100644 --- a/src/scyjava/_convert.py +++ b/src/scyjava/_convert.py @@ -9,7 +9,7 @@ from bisect import insort from importlib.util import find_spec from pathlib import Path -from typing import Any, Callable, Dict, List, NamedTuple, reveal_type +from typing import Any, Callable, Dict, List, NamedTuple from jpype import JBoolean, JByte, JChar, JDouble, JFloat, JInt, JLong, JShort