Skip to content

WIP STYLE: Suppress false positives for pylint W0703 ( broad-except) #49910

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion ci/fix_wheels.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
os.path.join(base_redist_dir, "vcruntime140_1.dll"),
"pandas/_libs/window/vcruntime140_1.dll",
)
except Exception as e:
except Exception as e: # pylint: disable=broad-except
success = False
exception = e

Expand Down
2 changes: 1 addition & 1 deletion doc/make.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def _add_redirects(self):

try:
title = self._get_page_title(row[1])
except Exception:
except Exception: # pylint: disable=broad-except
# the file can be an ipynb and not an rst, or docutils
# may not be able to read the rst because it has some
# sphinx specific stuff
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ def transform_str_or_callable(self, func) -> DataFrame | Series:
# Two possible ways to use a UDF - apply or call directly
try:
return obj.apply(func, args=args, **kwargs)
except Exception:
except Exception: # pylint: disable=broad-except
return func(obj, *args, **kwargs)

def agg_list_like(self) -> DataFrame | Series:
Expand Down Expand Up @@ -706,7 +706,7 @@ def apply_empty_result(self):
r = self.f(Series([], dtype=np.float64))
else:
r = self.f(Series(index=self.columns, dtype=np.float64))
except Exception:
except Exception: # pylint: disable=broad-except
pass
else:
should_reduce = not isinstance(r, Series)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1464,7 +1464,7 @@ def _choose_path(self, fast_path: Callable, slow_path: Callable, group: DataFram
res_fast = fast_path(group)
except AssertionError:
raise # pragma: no cover
except Exception:
except Exception: # pylint: disable=broad-except
# GH#29631 For user-defined function, we can't predict what may be
# raised; see test_transform.test_transform_fastpath_raises
return path, res
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def map(self, mapper, na_action=None):
if not isinstance(result, Index):
raise TypeError("The map function must return an Index object")
return result
except Exception:
except Exception: # pylint: disable=broad-except
return self.astype(object).map(mapper)

@cache_readonly
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/parsers/base_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1134,7 +1134,7 @@ def converter(*date_cols):
if isinstance(result, datetime.datetime):
raise Exception("scalar parser")
return result
except Exception:
except Exception: # pylint: disable=broad-except
return tools.to_datetime(
parsing.try_parse_dates(
parsing.concat_date_cols(date_cols),
Expand Down
4 changes: 2 additions & 2 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,7 @@ def remove(self, key: str, where=None, start=None, stop=None) -> None:
except AssertionError:
# surface any assertion errors for e.g. debugging
raise
except Exception as err:
except Exception as err: # pylint: disable=broad-except
# In tests we get here with ClosedFileError, TypeError, and
# _table_mod.NoSuchNodeError. TODO: Catch only these?

Expand Down Expand Up @@ -1601,7 +1601,7 @@ def info(self) -> str:
except AssertionError:
# surface any assertion errors for e.g. debugging
raise
except Exception as detail:
except Exception as detail: # pylint: disable=broad-except
keys.append(k)
dstr = pprint_thing(detail)
values.append(f"[invalid_HDFStore node: {dstr}]")
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ def read_sql(

try:
_is_table_name = pandas_sql.has_table(sql)
except Exception:
except Exception: # pylint: disable=broad-except
# using generic exception to catch errors from sql drivers (GH24988)
_is_table_name = False

Expand Down
5 changes: 3 additions & 2 deletions pandas/plotting/_matplotlib/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ def _convert_1d(values, unit, axis):
def try_parse(values):
try:
return mdates.date2num(tools.to_datetime(values))
except Exception:
except Exception: # pylint: disable=broad-except
return values

if isinstance(values, (datetime, pydt.date, np.datetime64, pydt.time)):
Expand All @@ -314,7 +314,7 @@ def try_parse(values):

try:
values = tools.to_datetime(values)
except Exception:
except Exception: # pylint: disable=broad-except
pass

values = mdates.date2num(values)
Expand Down Expand Up @@ -427,6 +427,7 @@ def __call__(self):
if len(all_dates) > 0:
locs = self.raise_if_exceeds(mdates.date2num(all_dates))
return locs
# pylint: disable-next=broad-except
except Exception: # pragma: no cover
pass

Expand Down
2 changes: 1 addition & 1 deletion pandas/plotting/_matplotlib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1479,7 +1479,7 @@ def get_label(i):
i = int(i)
try:
return pprint_thing(data.index[i])
except Exception:
except Exception: # pylint: disable=broad-except
return ""

if self._need_to_set_index:
Expand Down
12 changes: 6 additions & 6 deletions pandas/tests/extension/base/dim2.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,12 @@ def test_reductions_2d_axis_none(self, data, method):
err_result = None
try:
expected = getattr(data, method)()
except Exception as err:
except Exception as err: # pylint: disable=broad-except
# if the 1D reduction is invalid, the 2D reduction should be as well
err_expected = err
try:
result = getattr(arr2d, method)(axis=None)
except Exception as err2:
except Exception as err2: # pylint: disable=broad-except
err_result = err2

else:
Expand Down Expand Up @@ -206,10 +206,10 @@ def test_reductions_2d_axis0(self, data, method):
result = getattr(arr2d, method)(axis=0, **kwargs)
else:
result = getattr(arr2d, method)(axis=0, **kwargs)
except Exception as err:
except Exception as err: # pylint: disable=broad-except
try:
getattr(data, method)()
except Exception as err2:
except Exception as err2: # pylint: disable=broad-except
assert type(err) == type(err2)
return
else:
Expand Down Expand Up @@ -255,10 +255,10 @@ def test_reductions_2d_axis1(self, data, method):

try:
result = getattr(arr2d, method)(axis=1)
except Exception as err:
except Exception as err: # pylint: disable=broad-except
try:
getattr(data, method)()
except Exception as err2:
except Exception as err2: # pylint: disable=broad-except
assert type(err) == type(err2)
return
else:
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/extension/base/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ def test_diff(self, data, periods):
try:
# does this array implement ops?
op(data, data)
except Exception:
except Exception: # pylint: disable=broad-except
pytest.skip(f"{type(data)} does not support diff")
s = pd.Series(data)
result = s.diff(periods)
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/extension/base/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def _compare_other(self, ser: pd.Series, data, op, other):
exc = None
try:
result = op(ser, other)
except Exception as err:
except Exception as err: # pylint: disable=broad-except
exc = err

if exc is None:
Expand Down Expand Up @@ -206,7 +206,7 @@ def test_unary_ufunc_dunder_equivalence(self, data, ufunc):
exc = None
try:
result = getattr(data, attr)()
except Exception as err:
except Exception as err: # pylint: disable=broad-except
exc = err

# if __pos__ raised, then so should the ufunc
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/extension/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -1257,7 +1257,7 @@ def test_compare_array(self, data, comparison_op, na_value, request):
exc = None
try:
result = comparison_op(ser, other)
except Exception as err:
except Exception as err: # pylint: disable=broad-except
exc = err

if exc is None:
Expand Down
10 changes: 5 additions & 5 deletions pandas/tests/io/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def s3_base(worker_id):
r = requests.get(endpoint_uri)
if r.ok:
break
except Exception:
except Exception: # pylint: disable=broad-except
pass
timeout -= 0.1
time.sleep(0.1)
Expand Down Expand Up @@ -154,12 +154,12 @@ def add_tips_files(bucket_name):

try:
cli.create_bucket(Bucket=bucket)
except Exception:
except Exception: # pylint: disable=broad-except
# OK is bucket already exists
pass
try:
cli.create_bucket(Bucket="cant_get_it", ACL="private")
except Exception:
except Exception: # pylint: disable=broad-except
# OK is bucket already exists
pass
timeout = 2
Expand All @@ -176,11 +176,11 @@ def add_tips_files(bucket_name):

try:
s3.rm(bucket, recursive=True)
except Exception:
except Exception: # pylint: disable=broad-except
pass
try:
s3.rm("cant_get_it", recursive=True)
except Exception:
except Exception: # pylint: disable=broad-except
pass
timeout = 2
while cli.list_buckets()["Buckets"] and timeout > 0:
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/io/parser/test_c_parser_only.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def test_buffer_rd_bytes(c_parser_only):
compression="gzip",
delim_whitespace=True,
)
except Exception:
except Exception: # pylint: disable=broad-except
pass


Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/io/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -1276,7 +1276,7 @@ class ErrorThread(threading.Thread):
def run(self):
try:
super().run()
except Exception as err:
except Exception as err: # pylint: disable=broad-except
self.err = err
else:
self.err = None
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/tslibs/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ def _compare_local_to_utc(tz_didx, naive_didx):
try:
result = tzconversion.tz_localize_to_utc(naive_didx.asi8, tz_didx.tz)
err1 = None
except Exception as err:
except Exception as err: # pylint: disable=broad-except
err1 = err

try:
expected = naive_didx.map(lambda x: x.tz_localize(tz_didx.tz)).asi8
except Exception as err:
except Exception as err: # pylint: disable=broad-except
err2 = err

if err1 is not None:
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ disable = [
"arguments-out-of-order",
"arguments-renamed",
"attribute-defined-outside-init",
"broad-except",
"comparison-with-callable",
"dangerous-default-value",
"deprecated-module",
Expand Down