Skip to content

Commit 3a47936

Browse files
mroeschkenoatamir
authored andcommitted
STYLE: Add code check to avoid returning Exceptions instead of raising (pandas-dev#48948)
1 parent 6bb5a44 commit 3a47936

File tree

10 files changed

+47
-32
lines changed

10 files changed

+47
-32
lines changed

.pre-commit-config.yaml

+7
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,13 @@ repos:
226226
entry: python scripts/no_bool_in_generic.py
227227
language: python
228228
files: ^pandas/core/generic\.py$
229+
- id: no-return-exception
230+
name: Use raise instead of return for exceptions
231+
language: pygrep
232+
entry: 'return [A-Za-z]+(Error|Exit|Interrupt|Exception|Iteration)'
233+
files: ^pandas/
234+
types: [python]
235+
exclude: ^pandas/tests/
229236
- id: pandas-errors-documented
230237
name: Ensure pandas errors are documented in doc/source/reference/testing.rst
231238
entry: python scripts/pandas_errors_documented.py

pandas/core/indexes/base.py

+18-9
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ def __new__(
554554
return klass._simple_new(arr, name)
555555

556556
elif is_scalar(data):
557-
raise cls._scalar_data_error(data)
557+
raise cls._raise_scalar_data_error(data)
558558
elif hasattr(data, "__array__"):
559559
return Index(np.asarray(data), dtype=dtype, copy=copy, name=name, **kwargs)
560560
else:
@@ -4386,14 +4386,23 @@ def is_int(v):
43864386
return indexer
43874387

43884388
@final
4389-
def _invalid_indexer(self, form: str_t, key) -> TypeError:
4389+
def _raise_invalid_indexer(
4390+
self,
4391+
form: str_t,
4392+
key,
4393+
reraise: lib.NoDefault | None | Exception = lib.no_default,
4394+
) -> None:
43904395
"""
4391-
Consistent invalid indexer message.
4396+
Raise consistent invalid indexer message.
43924397
"""
4393-
return TypeError(
4398+
msg = (
43944399
f"cannot do {form} indexing on {type(self).__name__} with these "
43954400
f"indexers [{key}] of type {type(key).__name__}"
43964401
)
4402+
if reraise is not lib.no_default:
4403+
raise TypeError(msg) from reraise
4404+
else:
4405+
raise TypeError(msg)
43974406

43984407
# --------------------------------------------------------------------
43994408
# Reindex Methods
@@ -5279,10 +5288,10 @@ def where(self, cond, other=None) -> Index:
52795288
# construction helpers
52805289
@final
52815290
@classmethod
5282-
def _scalar_data_error(cls, data):
5291+
def _raise_scalar_data_error(cls, data):
52835292
# We return the TypeError so that we can raise it from the constructor
52845293
# in order to keep mypy happy
5285-
return TypeError(
5294+
raise TypeError(
52865295
f"{cls.__name__}(...) must be called with a collection of some "
52875296
f"kind, {repr(data)} was passed"
52885297
)
@@ -6674,15 +6683,15 @@ def _maybe_cast_listlike_indexer(self, target) -> Index:
66746683
return ensure_index(target)
66756684

66766685
@final
6677-
def _validate_indexer(self, form: str_t, key, kind: str_t):
6686+
def _validate_indexer(self, form: str_t, key, kind: str_t) -> None:
66786687
"""
66796688
If we are positional indexer, validate that we have appropriate
66806689
typed bounds must be an integer.
66816690
"""
66826691
assert kind in ["getitem", "iloc"]
66836692

66846693
if key is not None and not is_integer(key):
6685-
raise self._invalid_indexer(form, key)
6694+
self._raise_invalid_indexer(form, key)
66866695

66876696
def _maybe_cast_slice_bound(self, label, side: str_t, kind=no_default):
66886697
"""
@@ -6714,7 +6723,7 @@ def _maybe_cast_slice_bound(self, label, side: str_t, kind=no_default):
67146723
# datetimelike Indexes
67156724
# reject them, if index does not contain label
67166725
if (is_float(label) or is_integer(label)) and label not in self:
6717-
raise self._invalid_indexer("slice", label)
6726+
self._raise_invalid_indexer("slice", label)
67186727

67196728
return label
67206729

pandas/core/indexes/category.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ def __new__(
230230
data = []
231231

232232
if is_scalar(data):
233-
raise cls._scalar_data_error(data)
233+
cls._raise_scalar_data_error(data)
234234

235235
data = Categorical(
236236
data, categories=categories, ordered=ordered, dtype=dtype, copy=copy

pandas/core/indexes/datetimelike.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -314,12 +314,12 @@ def _maybe_cast_slice_bound(self, label, side: str, kind=lib.no_default):
314314
# DTI -> parsing.DateParseError
315315
# TDI -> 'unit abbreviation w/o a number'
316316
# PI -> string cannot be parsed as datetime-like
317-
raise self._invalid_indexer("slice", label) from err
317+
self._raise_invalid_indexer("slice", label, err)
318318

319319
lower, upper = self._parsed_string_to_bounds(reso, parsed)
320320
return lower if side == "left" else upper
321321
elif not isinstance(label, self._data._recognized_scalars):
322-
raise self._invalid_indexer("slice", label)
322+
self._raise_invalid_indexer("slice", label)
323323

324324
return label
325325

pandas/core/indexes/datetimes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ def __new__(
334334
) -> DatetimeIndex:
335335

336336
if is_scalar(data):
337-
raise cls._scalar_data_error(data)
337+
cls._raise_scalar_data_error(data)
338338

339339
# - Cases checked above all return/raise before reaching here - #
340340

pandas/core/indexes/numeric.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def _ensure_array(cls, data, dtype, copy: bool):
140140
if not isinstance(data, (np.ndarray, Index)):
141141
# Coerce to ndarray if not already ndarray or Index
142142
if is_scalar(data):
143-
raise cls._scalar_data_error(data)
143+
cls._raise_scalar_data_error(data)
144144

145145
# other iterable of some kind
146146
if not isinstance(data, (ABCSeries, list, tuple)):

pandas/core/indexes/period.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ def __new__(
240240
# range-based.
241241
if not fields:
242242
# test_pickle_compat_construction
243-
raise cls._scalar_data_error(None)
243+
cls._raise_scalar_data_error(None)
244244

245245
data, freq2 = PeriodArray._generate_range(None, None, None, freq, fields)
246246
# PeriodArray._generate range does validation that fields is

pandas/core/indexes/timedeltas.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def __new__(
128128
name = maybe_extract_name(name, data, cls)
129129

130130
if is_scalar(data):
131-
raise cls._scalar_data_error(data)
131+
cls._raise_scalar_data_error(data)
132132

133133
if unit in {"Y", "y", "M"}:
134134
raise ValueError(

pandas/core/internals/managers.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -1038,7 +1038,7 @@ def _verify_integrity(self) -> None:
10381038
tot_items = sum(len(x.mgr_locs) for x in self.blocks)
10391039
for block in self.blocks:
10401040
if block.shape[1:] != mgr_shape[1:]:
1041-
raise construction_error(tot_items, block.shape[1:], self.axes)
1041+
raise_construction_error(tot_items, block.shape[1:], self.axes)
10421042
if len(self.items) != tot_items:
10431043
raise AssertionError(
10441044
"Number of manager items must equal union of "
@@ -2145,7 +2145,7 @@ def create_block_manager_from_blocks(
21452145
except ValueError as err:
21462146
arrays = [blk.values for blk in blocks]
21472147
tot_items = sum(arr.shape[0] for arr in arrays)
2148-
raise construction_error(tot_items, arrays[0].shape[1:], axes, err)
2148+
raise_construction_error(tot_items, arrays[0].shape[1:], axes, err)
21492149

21502150
if consolidate:
21512151
mgr._consolidate_inplace()
@@ -2172,13 +2172,13 @@ def create_block_manager_from_column_arrays(
21722172
blocks = _form_blocks(arrays, consolidate)
21732173
mgr = BlockManager(blocks, axes, verify_integrity=False)
21742174
except ValueError as e:
2175-
raise construction_error(len(arrays), arrays[0].shape, axes, e)
2175+
raise_construction_error(len(arrays), arrays[0].shape, axes, e)
21762176
if consolidate:
21772177
mgr._consolidate_inplace()
21782178
return mgr
21792179

21802180

2181-
def construction_error(
2181+
def raise_construction_error(
21822182
tot_items: int,
21832183
block_shape: Shape,
21842184
axes: list[Index],
@@ -2198,10 +2198,10 @@ def construction_error(
21982198
# We return the exception object instead of raising it so that we
21992199
# can raise it in the caller; mypy plays better with that
22002200
if passed == implied and e is not None:
2201-
return e
2201+
raise e
22022202
if block_shape[0] == 0:
2203-
return ValueError("Empty data passed with indices specified.")
2204-
return ValueError(f"Shape of passed values is {passed}, indices imply {implied}")
2203+
raise ValueError("Empty data passed with indices specified.")
2204+
raise ValueError(f"Shape of passed values is {passed}, indices imply {implied}")
22052205

22062206

22072207
# -----------------------------------------------------------------------

pandas/io/pytables.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -1658,13 +1658,6 @@ def _create_storer(
16581658
if value is not None and not isinstance(value, (Series, DataFrame)):
16591659
raise TypeError("value must be None, Series, or DataFrame")
16601660

1661-
def error(t):
1662-
# return instead of raising so mypy can tell where we are raising
1663-
return TypeError(
1664-
f"cannot properly create the storer for: [{t}] [group->"
1665-
f"{group},value->{type(value)},format->{format}"
1666-
)
1667-
16681661
pt = _ensure_decoded(getattr(group._v_attrs, "pandas_type", None))
16691662
tt = _ensure_decoded(getattr(group._v_attrs, "table_type", None))
16701663

@@ -1699,7 +1692,10 @@ def error(t):
16991692
try:
17001693
cls = _STORER_MAP[pt]
17011694
except KeyError as err:
1702-
raise error("_STORER_MAP") from err
1695+
raise TypeError(
1696+
f"cannot properly create the storer for: [_STORER_MAP] [group->"
1697+
f"{group},value->{type(value)},format->{format}"
1698+
) from err
17031699
return cls(self, group, encoding=encoding, errors=errors)
17041700

17051701
# existing node (and must be a table)
@@ -1732,7 +1728,10 @@ def error(t):
17321728
try:
17331729
cls = _TABLE_MAP[tt]
17341730
except KeyError as err:
1735-
raise error("_TABLE_MAP") from err
1731+
raise TypeError(
1732+
f"cannot properly create the storer for: [_TABLE_MAP] [group->"
1733+
f"{group},value->{type(value)},format->{format}"
1734+
) from err
17361735

17371736
return cls(self, group, encoding=encoding, errors=errors)
17381737

0 commit comments

Comments
 (0)