Skip to content

Commit

Permalink
Propagate Pandas/Polars import errors when the module exists but fail…
Browse files Browse the repository at this point in the history
…ed to be imported.

Broadly speaking, there are two categories of `ImportError`: the module does not exist, or the module exists but failed to be imported. Currently, both are silently ignored; however, the latter category of error should be propagated up the stack so that the developer can investigate and resolve the issue.
  • Loading branch information
fumoboy007 authored and mrjbq7 committed Sep 16, 2024
1 parent d9ee641 commit bf92064
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
20 changes: 18 additions & 2 deletions talib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,31 @@
# polars.Series input
try:
from polars import Series as _pl_Series
except ImportError:
except ImportError as import_error:
try:
if not isinstance(import_error, ModuleNotFoundError) or import_error.name != 'polars':
# Propagate the error when the module exists but failed to be imported.
raise import_error
# `ModuleNotFoundError` was introduced in Python 3.6.
except NameError:
pass

# polars not available, nothing to wrap
_pl_Series = None

# If pandas is available, wrap talib functions so that they support
# pandas.Series input
try:
from pandas import Series as _pd_Series
except ImportError:
except ImportError as import_error:
try:
if not isinstance(import_error, ModuleNotFoundError) or import_error.name != 'pandas':
# Propagate the error when the module exists but failed to be imported.
raise import_error
# `ModuleNotFoundError` was introduced in Python 3.6.
except NameError:
pass

# pandas not available, nothing to wrap
_pd_Series = None

Expand Down
20 changes: 18 additions & 2 deletions talib/_abstract.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,15 @@ try:
__ARRAY_TYPES.append(pandas.Series)
__PANDAS_DATAFRAME = pandas.DataFrame
__PANDAS_SERIES = pandas.Series
except ImportError:
except ImportError as import_error:
try:
if not isinstance(import_error, ModuleNotFoundError) or import_error.name != 'pandas':
# Propagate the error when the module exists but failed to be imported.
raise import_error
# `ModuleNotFoundError` was introduced in Python 3.6.
except NameError:
pass

__PANDAS_DATAFRAME = None
__PANDAS_SERIES = None

Expand All @@ -45,7 +53,15 @@ try:
__ARRAY_TYPES.append(polars.Series)
__POLARS_DATAFRAME = polars.DataFrame
__POLARS_SERIES = polars.Series
except ImportError:
except ImportError as import_error:
try:
if not isinstance(import_error, ModuleNotFoundError) or import_error.name != 'polars':
# Propagate the error when the module exists but failed to be imported.
raise import_error
# `ModuleNotFoundError` was introduced in Python 3.6.
except NameError:
pass

__POLARS_DATAFRAME = None
__POLARS_SERIES = None

Expand Down

0 comments on commit bf92064

Please sign in to comment.