Skip to content

Commit 21399f5

Browse files
authored
Enable pylint undefined-loop-variable warning (#50961)
* Enable pylint undefined-loop-variable check * Respond to review comments
1 parent 1d8b188 commit 21399f5

File tree

5 files changed

+20
-8
lines changed

5 files changed

+20
-8
lines changed

pandas/core/util/hashing.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,14 @@ def combine_hash_arrays(
7171

7272
mult = np.uint64(1000003)
7373
out = np.zeros_like(first) + np.uint64(0x345678)
74+
last_i = 0
7475
for i, a in enumerate(arrays):
7576
inverse_i = num_items - i
7677
out ^= a
7778
out *= mult
7879
mult += np.uint64(82520 + inverse_i + inverse_i)
79-
assert i + 1 == num_items, "Fed in wrong num_items"
80+
last_i = i
81+
assert last_i + 1 == num_items, "Fed in wrong num_items"
8082
out += np.uint64(97531)
8183
return out
8284

pandas/io/excel/_base.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,9 @@ def parse(
736736

737737
output = {}
738738

739+
last_sheetname = None
739740
for asheetname in sheets:
741+
last_sheetname = asheetname
740742
if verbose:
741743
print(f"Reading sheet {asheetname}")
742744

@@ -888,10 +890,13 @@ def parse(
888890
err.args = (f"{err.args[0]} (sheet: {asheetname})", *err.args[1:])
889891
raise err
890892

893+
if last_sheetname is None:
894+
raise ValueError("Sheet name is an empty list")
895+
891896
if ret_dict:
892897
return output
893898
else:
894-
return output[asheetname]
899+
return output[last_sheetname]
895900

896901

897902
@doc(storage_options=_shared_docs["storage_options"])

pandas/tests/frame/methods/test_to_dict_of_blocks.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,34 @@ def test_copy_blocks(self, float_frame):
2020
column = df.columns[0]
2121

2222
# use the default copy=True, change a column
23+
_last_df = None
2324
blocks = df._to_dict_of_blocks(copy=True)
2425
for _df in blocks.values():
26+
_last_df = _df
2527
if column in _df:
2628
_df.loc[:, column] = _df[column] + 1
2729

2830
# make sure we did not change the original DataFrame
29-
assert not _df[column].equals(df[column])
31+
assert _last_df is not None and not _last_df[column].equals(df[column])
3032

3133
def test_no_copy_blocks(self, float_frame, using_copy_on_write):
3234
# GH#9607
3335
df = DataFrame(float_frame, copy=True)
3436
column = df.columns[0]
3537

38+
_last_df = None
3639
# use the copy=False, change a column
3740
blocks = df._to_dict_of_blocks(copy=False)
3841
for _df in blocks.values():
42+
_last_df = _df
3943
if column in _df:
4044
_df.loc[:, column] = _df[column] + 1
4145

4246
if not using_copy_on_write:
4347
# make sure we did change the original DataFrame
44-
assert _df[column].equals(df[column])
48+
assert _last_df is not None and _last_df[column].equals(df[column])
4549
else:
46-
assert not _df[column].equals(df[column])
50+
assert _last_df is not None and not _last_df[column].equals(df[column])
4751

4852

4953
def test_to_dict_of_blocks_item_cache(request, using_copy_on_write):

pandas/tests/io/formats/test_printing.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,22 +122,24 @@ class TestTableSchemaRepr:
122122
def test_publishes(self, ip):
123123
ipython = ip.instance(config=ip.config)
124124
df = pd.DataFrame({"A": [1, 2]})
125-
objects = [df["A"], df, df] # dataframe / series
125+
objects = [df["A"], df] # dataframe / series
126126
expected_keys = [
127127
{"text/plain", "application/vnd.dataresource+json"},
128128
{"text/plain", "text/html", "application/vnd.dataresource+json"},
129129
]
130130

131131
opt = pd.option_context("display.html.table_schema", True)
132+
last_obj = None
132133
for obj, expected in zip(objects, expected_keys):
134+
last_obj = obj
133135
with opt:
134136
formatted = ipython.display_formatter.format(obj)
135137
assert set(formatted[0].keys()) == expected
136138

137139
with_latex = pd.option_context("styler.render.repr", "latex")
138140

139141
with opt, with_latex:
140-
formatted = ipython.display_formatter.format(obj)
142+
formatted = ipython.display_formatter.format(last_obj)
141143

142144
expected = {
143145
"text/plain",

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,6 @@ disable = [
287287
"too-many-public-methods",
288288
"too-many-return-statements",
289289
"too-many-statements",
290-
"undefined-loop-variable",
291290
"unexpected-keyword-arg",
292291
"ungrouped-imports",
293292
"unsubscriptable-object",

0 commit comments

Comments
 (0)