Skip to content

Commit 9e33f2e

Browse files
committed
refactor: delay import of pandas until needed
1 parent afe97d3 commit 9e33f2e

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

src/scyjava/_convert.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
import logging
88
import math
99
from bisect import insort
10+
from importlib.util import find_spec
1011
from pathlib import Path
11-
from typing import Any, Callable, Dict, List, NamedTuple
12+
from typing import Any, Callable, Dict, List, NamedTuple, reveal_type
1213

1314
from jpype import JBoolean, JByte, JChar, JDouble, JFloat, JInt, JLong, JShort
1415

@@ -677,7 +678,7 @@ def _stock_py_converters() -> List:
677678
priority=Priority.VERY_LOW,
678679
),
679680
]
680-
if _import_pandas(required=False):
681+
if find_spec("pandas"):
681682
converters.append(
682683
Converter(
683684
name="org.scijava.table.Table -> pandas.DataFrame",
@@ -716,7 +717,7 @@ def _stock_py_converters() -> List:
716717
),
717718
]
718719
)
719-
if _import_numpy(required=False):
720+
if find_spec("numpy"):
720721
converters.append(
721722
Converter(
722723
name="primitive array -> numpy.ndarray",
@@ -803,16 +804,15 @@ def _jarray_shape(jarr):
803804
return shape
804805

805806

806-
def _import_numpy(required=True):
807+
def _import_numpy():
807808
try:
808809
import numpy as np
809810

810811
return np
811812
except ImportError as e:
812-
if required:
813-
msg = "The NumPy library is missing (https://numpy.org/). "
814-
msg += "Please install it before using this function."
815-
raise RuntimeError(msg) from e
813+
msg = "The NumPy library is missing (https://numpy.org/). "
814+
msg += "Please install it before using this function."
815+
raise RuntimeError(msg) from e
816816

817817

818818
######################################
@@ -838,16 +838,15 @@ def _convert_table(obj: Any):
838838
return None
839839

840840

841-
def _import_pandas(required=True):
841+
def _import_pandas():
842842
try:
843843
import pandas as pd
844844

845845
return pd
846846
except ImportError as e:
847-
if required:
848-
msg = "The Pandas library is missing (http://pandas.pydata.org/). "
849-
msg += "Please install it before using this function."
850-
raise RuntimeError(msg) from e
847+
msg = "The Pandas library is missing (http://pandas.pydata.org/). "
848+
msg += "Please install it before using this function."
849+
raise RuntimeError(msg) from e
851850

852851

853852
def _table_to_pandas(table):

0 commit comments

Comments
 (0)