Fix exception causes all over the codebase (#5787)

This commit is contained in:
Ram Rachum 2020-06-15 16:06:07 +03:00 committed by GitHub
parent ae18a094b0
commit 02884b08aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 12 deletions

View File

@ -63,13 +63,13 @@ else:
path_type = type(path) path_type = type(path)
try: try:
path_repr = path_type.__fspath__(path) path_repr = path_type.__fspath__(path)
except AttributeError: except AttributeError as e:
if hasattr(path_type, '__fspath__'): if hasattr(path_type, '__fspath__'):
raise raise
if issubclass(path_type, PurePath): if issubclass(path_type, PurePath):
return _PurePath__fspath__(path) return _PurePath__fspath__(path)
raise TypeError("expected str, bytes or os.PathLike object, " raise TypeError("expected str, bytes or os.PathLike object, "
"not " + path_type.__name__) "not " + path_type.__name__) from e
if isinstance(path_repr, (str, bytes)): if isinstance(path_repr, (str, bytes)):
return path_repr return path_repr
raise TypeError("expected {}.__fspath__() to return str or bytes, " raise TypeError("expected {}.__fspath__() to return str or bytes, "

View File

@ -263,9 +263,9 @@ def _convert_unknown_data(data, meta=None, meta_type=None):
if meta is not None: if meta is not None:
try: try:
data = np.array(data, dtype=meta_type) data = np.array(data, dtype=meta_type)
except Exception: except Exception as e:
raise TypeError('Can not handle data from {}'.format( raise TypeError('Can not handle data from {}'.format(
type(data).__name__)) type(data).__name__)) from e
else: else:
import warnings import warnings
warnings.warn( warnings.warn(
@ -273,9 +273,9 @@ def _convert_unknown_data(data, meta=None, meta_type=None):
', coverting it to csr_matrix') ', coverting it to csr_matrix')
try: try:
data = scipy.sparse.csr_matrix(data) data = scipy.sparse.csr_matrix(data)
except Exception: except Exception as e:
raise TypeError('Can not initialize DMatrix from' raise TypeError('Can not initialize DMatrix from'
' {}'.format(type(data).__name__)) ' {}'.format(type(data).__name__)) from e
return data return data

View File

@ -56,8 +56,8 @@ def plot_importance(booster, ax=None, height=0.2,
""" """
try: try:
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
except ImportError: except ImportError as e:
raise ImportError('You must install matplotlib to plot importance') raise ImportError('You must install matplotlib to plot importance') from e
if isinstance(booster, XGBModel): if isinstance(booster, XGBModel):
importance = booster.get_booster().get_score( importance = booster.get_booster().get_score(
@ -168,8 +168,8 @@ def to_graphviz(booster, fmap='', num_trees=0, rankdir=None,
""" """
try: try:
from graphviz import Source from graphviz import Source
except ImportError: except ImportError as e:
raise ImportError('You must install graphviz to plot tree') raise ImportError('You must install graphviz to plot tree') from e
if isinstance(booster, XGBModel): if isinstance(booster, XGBModel):
booster = booster.get_booster() booster = booster.get_booster()
@ -237,8 +237,8 @@ def plot_tree(booster, fmap='', num_trees=0, rankdir=None, ax=None, **kwargs):
try: try:
from matplotlib import pyplot as plt from matplotlib import pyplot as plt
from matplotlib import image from matplotlib import image
except ImportError: except ImportError as e:
raise ImportError('You must install matplotlib to plot tree') raise ImportError('You must install matplotlib to plot tree') from e
if ax is None: if ax is None:
_, ax = plt.subplots(1, 1) _, ax = plt.subplots(1, 1)