Skip to content

Commit 07e885b

Browse files
authored
Don't suppress exception chaining for optional dependencies (#43882)
1 parent dbfcfb0 commit 07e885b

File tree

3 files changed

+6
-3
lines changed

3 files changed

+6
-3
lines changed

doc/source/whatsnew/v1.4.0.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ Other enhancements
127127
- :meth:`IntegerArray.all` , :meth:`IntegerArray.any`, :meth:`FloatingArray.any`, and :meth:`FloatingArray.all` use Kleene logic (:issue:`41967`)
128128
- Added support for nullable boolean and integer types in :meth:`DataFrame.to_stata`, :class:`~pandas.io.stata.StataWriter`, :class:`~pandas.io.stata.StataWriter117`, and :class:`~pandas.io.stata.StataWriterUTF8` (:issue:`40855`)
129129
- :meth:`DataFrame.__pos__`, :meth:`DataFrame.__neg__` now retain ``ExtensionDtype`` dtypes (:issue:`43883`)
130-
130+
- The error raised when an optional dependency can't be imported now includes the original exception, for easier investigation (:issue:`43882`)
131+
-
131132

132133
.. ---------------------------------------------------------------------------
133134

pandas/compat/_optional.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def import_optional_dependency(
115115
module = importlib.import_module(name)
116116
except ImportError:
117117
if errors == "raise":
118-
raise ImportError(msg) from None
118+
raise ImportError(msg)
119119
else:
120120
return None
121121

pandas/tests/test_optional_dependency.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313

1414
def test_import_optional():
1515
match = "Missing .*notapackage.* pip .* conda .* notapackage"
16-
with pytest.raises(ImportError, match=match):
16+
with pytest.raises(ImportError, match=match) as exc_info:
1717
import_optional_dependency("notapackage")
18+
# The original exception should be there as context:
19+
assert isinstance(exc_info.value.__context__, ImportError)
1820

1921
result = import_optional_dependency("notapackage", errors="ignore")
2022
assert result is None

0 commit comments

Comments
 (0)