Skip to content

Don't suppress exception chaining for optional dependencies #43882

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 6, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ Other enhancements
- Attempting to write into a file in missing parent directory with :meth:`DataFrame.to_csv`, :meth:`DataFrame.to_html`, :meth:`DataFrame.to_excel`, :meth:`DataFrame.to_feather`, :meth:`DataFrame.to_parquet`, :meth:`DataFrame.to_stata`, :meth:`DataFrame.to_json`, :meth:`DataFrame.to_pickle`, and :meth:`DataFrame.to_xml` now explicitly mentions missing parent directory, the same is true for :class:`Series` counterparts (:issue:`24306`)
- :meth:`IntegerArray.all` , :meth:`IntegerArray.any`, :meth:`FloatingArray.any`, and :meth:`FloatingArray.all` use Kleene logic (:issue:`41967`)
- 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`)
- The error raised when an optional dependency can't be imported now includes the original exception, for easier investigation.
-

.. ---------------------------------------------------------------------------
Expand All @@ -142,7 +143,8 @@ These are bug fixes that might have notable behavior changes.
Inconsistent date string parsing
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The ``dayfirst`` option of :func:`to_datetime` isn't strict, and this can lead to surprising behaviour:
The ``dayfirst`` option of :func:`to_datetime` isn't strict, and this can lead to sur
ising behaviour:

.. ipython:: python
:okwarning:
Expand Down
2 changes: 1 addition & 1 deletion pandas/compat/_optional.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def import_optional_dependency(
module = importlib.import_module(name)
except ImportError:
if errors == "raise":
raise ImportError(msg) from None
raise ImportError(msg)
else:
return None

Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/test_optional_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@

def test_import_optional():
match = "Missing .*notapackage.* pip .* conda .* notapackage"
with pytest.raises(ImportError, match=match):
with pytest.raises(ImportError, match=match) as exc_info:
import_optional_dependency("notapackage")
# The original exception should be there as context:
assert isinstance(exc_info.value.__context__, ImportError)

result = import_optional_dependency("notapackage", errors="ignore")
assert result is None
Expand Down