Skip to content

Commit 1bdc98c

Browse files
authored
STYLE enable ruff PLW2901 (#51797)
* Removed "PLW2901" and added in main folders where needs to be enabled gradually. Currently removed 10 errors generated outside the main folders. * Variables renamed in a more intuitive manner. * Renamed fname_splitted to fname_html in web/pandas_web.py * Removed duplicate pandas/core/* * Adjusted replace_bool_with_bool_t from previously introduced bug.
1 parent 48e600b commit 1bdc98c

10 files changed

+50
-44
lines changed

doc/source/conf.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -101,20 +101,20 @@
101101
reldir = os.path.relpath(dirname, source_path)
102102
for fname in fnames:
103103
if os.path.splitext(fname)[-1] in (".rst", ".ipynb"):
104-
fname = os.path.relpath(os.path.join(dirname, fname), source_path)
104+
rel_fname = os.path.relpath(os.path.join(dirname, fname), source_path)
105105

106-
if fname == "index.rst" and os.path.abspath(dirname) == source_path:
106+
if rel_fname == "index.rst" and os.path.abspath(dirname) == source_path:
107107
continue
108108
if pattern == "-api" and reldir.startswith("reference"):
109-
exclude_patterns.append(fname)
109+
exclude_patterns.append(rel_fname)
110110
elif (
111111
pattern == "whatsnew"
112112
and not reldir.startswith("reference")
113113
and reldir != "whatsnew"
114114
):
115-
exclude_patterns.append(fname)
116-
elif single_doc and fname != pattern:
117-
exclude_patterns.append(fname)
115+
exclude_patterns.append(rel_fname)
116+
elif single_doc and rel_fname != pattern:
117+
exclude_patterns.append(rel_fname)
118118

119119
with open(os.path.join(source_path, "index.rst.template")) as f:
120120
t = jinja2.Template(f.read())

pandas/_config/config.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ def _set_option(*args, **kwargs) -> None:
158158
o.validator(v)
159159

160160
# walk the nested dict
161-
root, k = _get_root(key)
162-
root[k] = v
161+
root, k_root = _get_root(key)
162+
root[k_root] = v
163163

164164
if o.cb:
165165
if silent:

pandas/util/version/__init__.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -253,16 +253,16 @@ def is_devrelease(self) -> bool:
253253

254254
def _parse_version_parts(s: str) -> Iterator[str]:
255255
for part in _legacy_version_component_re.split(s):
256-
part = _legacy_version_replacement_map.get(part, part)
256+
mapped_part = _legacy_version_replacement_map.get(part, part)
257257

258-
if not part or part == ".":
258+
if not mapped_part or mapped_part == ".":
259259
continue
260260

261-
if part[:1] in "0123456789":
261+
if mapped_part[:1] in "0123456789":
262262
# pad for numeric comparison
263-
yield part.zfill(8)
263+
yield mapped_part.zfill(8)
264264
else:
265-
yield "*" + part
265+
yield "*" + mapped_part
266266

267267
# ensure that alpha/beta/candidate are before final
268268
yield "*final"

pyproject.toml

+5-4
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,7 @@ ignore = [
272272
"B904",
273273
# Magic number
274274
"PLR2004",
275-
# Outer loop variable overwritten by inner assignment
276-
"PLW2901",
277-
# Consider `elif` instead of `else` then `if` to remove indentation level
275+
# Consider `elif` instead of `else` then `if` to remove indendation level
278276
"PLR5501",
279277
]
280278

@@ -292,7 +290,10 @@ exclude = [
292290
# relative imports allowed for asv_bench
293291
"asv_bench/*" = ["TID"]
294292
# to be enabled gradually
295-
"pandas/core/*" = ["PLR5501"]
293+
"pandas/core/*" = ["PLR5501", "PLW2901"]
294+
"pandas/io/*" = ["PLW2901"]
295+
"pandas/tests/*" = ["PLW2901"]
296+
"pandas/plotting/*" = ["PLW2901"]
296297
# TCH to be enabled gradually
297298
"pandas/core/nanops.py" = ["TCH"]
298299
"pandas/_libs/*" = ["TCH"]

scripts/no_bool_in_generic.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,15 @@ def replace_bool_with_bool_t(to_replace, content: str) -> str:
5050
new_lines = []
5151

5252
for n, line in enumerate(content.splitlines(), start=1):
53+
replaced_line = line
5354
if n in to_replace:
5455
for col_offset in reversed(to_replace[n]):
55-
line = line[:col_offset] + "bool_t" + line[col_offset + 4 :]
56-
new_lines.append(line)
56+
replaced_line = (
57+
replaced_line[:col_offset]
58+
+ "bool_t"
59+
+ replaced_line[col_offset + 4 :]
60+
)
61+
new_lines.append(replaced_line)
5762
return "\n".join(new_lines)
5863

5964

scripts/validate_docstrings.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -103,33 +103,33 @@ def get_api_items(api_doc_fd):
103103
previous_line = current_section = current_subsection = ""
104104
position = None
105105
for line in api_doc_fd:
106-
line = line.strip()
107-
if len(line) == len(previous_line):
108-
if set(line) == set("-"):
106+
line_stripped = line.strip()
107+
if len(line_stripped) == len(previous_line):
108+
if set(line_stripped) == set("-"):
109109
current_section = previous_line
110110
continue
111-
if set(line) == set("~"):
111+
if set(line_stripped) == set("~"):
112112
current_subsection = previous_line
113113
continue
114114

115-
if line.startswith(".. currentmodule::"):
116-
current_module = line.replace(".. currentmodule::", "").strip()
115+
if line_stripped.startswith(".. currentmodule::"):
116+
current_module = line_stripped.replace(".. currentmodule::", "").strip()
117117
continue
118118

119-
if line == ".. autosummary::":
119+
if line_stripped == ".. autosummary::":
120120
position = "autosummary"
121121
continue
122122

123123
if position == "autosummary":
124-
if line == "":
124+
if line_stripped == "":
125125
position = "items"
126126
continue
127127

128128
if position == "items":
129-
if line == "":
129+
if line_stripped == "":
130130
position = None
131131
continue
132-
item = line.strip()
132+
item = line_stripped.strip()
133133
if item in IGNORE_VALIDATION:
134134
continue
135135
func = importlib.import_module(current_module)
@@ -143,7 +143,7 @@ def get_api_items(api_doc_fd):
143143
current_subsection,
144144
)
145145

146-
previous_line = line
146+
previous_line = line_stripped
147147

148148

149149
class PandasDocstring(Validator):

scripts/validate_min_versions_in_sync.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,11 @@ def pin_min_versions_to_yaml_file(
180180
data = data.replace(old_dep, new_dep, 1)
181181
continue
182182
toml_version = version.parse(min_dep)
183-
yaml_versions = clean_version_list(yaml_versions, toml_version)
184-
cleaned_yaml_versions = [x for x in yaml_versions if "-" not in x]
183+
yaml_versions_list = clean_version_list(yaml_versions, toml_version)
184+
cleaned_yaml_versions = [x for x in yaml_versions_list if "-" not in x]
185185
new_dep = yaml_package
186-
for yaml_version in cleaned_yaml_versions:
187-
new_dep += yaml_version + ", "
186+
for clean_yaml_version in cleaned_yaml_versions:
187+
new_dep += clean_yaml_version + ", "
188188
operator = get_operator_from(new_dep)
189189
if operator != "=":
190190
new_dep += ">=" + min_dep

scripts/validate_rst_title_capitalization.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -229,15 +229,15 @@ def find_titles(rst_file: str) -> Iterable[tuple[str, int]]:
229229
with open(rst_file) as fd:
230230
previous_line = ""
231231
for i, line in enumerate(fd):
232-
line = line[:-1]
233-
line_chars = set(line)
232+
line_no_last_elem = line[:-1]
233+
line_chars = set(line_no_last_elem)
234234
if (
235235
len(line_chars) == 1
236236
and line_chars.pop() in symbols
237-
and len(line) == len(previous_line)
237+
and len(line_no_last_elem) == len(previous_line)
238238
):
239239
yield re.sub(r"[`\*_]", "", previous_line), i
240-
previous_line = line
240+
previous_line = line_no_last_elem
241241

242242

243243
def main(source_paths: list[str]) -> int:

setup.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ def initialize_options(self):
158158

159159
# clean the generated pxi files
160160
for pxifile in _pxifiles:
161-
pxifile = pxifile.replace(".pxi.in", ".pxi")
162-
self._clean_me.append(pxifile)
161+
pxifile_replaced = pxifile.replace(".pxi.in", ".pxi")
162+
self._clean_me.append(pxifile_replaced)
163163

164164
for d in ("build", "dist"):
165165
if os.path.exists(d):

web/pandas_web.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -369,9 +369,9 @@ def get_source_files(source_path: str) -> typing.Generator[str, None, None]:
369369
Generate the list of files present in the source directory.
370370
"""
371371
for root, dirs, fnames in os.walk(source_path):
372-
root = os.path.relpath(root, source_path)
372+
root_rel_path = os.path.relpath(root, source_path)
373373
for fname in fnames:
374-
yield os.path.join(root, fname)
374+
yield os.path.join(root_rel_path, fname)
375375

376376

377377
def extend_base_template(content: str, base_template: str) -> str:
@@ -430,8 +430,8 @@ def main(
430430
content = extend_base_template(body, context["main"]["base_template"])
431431
context["base_url"] = "".join(["../"] * os.path.normpath(fname).count("/"))
432432
content = jinja_env.from_string(content).render(**context)
433-
fname = os.path.splitext(fname)[0] + ".html"
434-
with open(os.path.join(target_path, fname), "w") as f:
433+
fname_html = os.path.splitext(fname)[0] + ".html"
434+
with open(os.path.join(target_path, fname_html), "w") as f:
435435
f.write(content)
436436
else:
437437
shutil.copy(

0 commit comments

Comments
 (0)