Skip to content

STYLE enable ruff PLW2901 #51797

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

Merged
merged 15 commits into from
Mar 17, 2023
Merged
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
12 changes: 6 additions & 6 deletions doc/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,20 @@
reldir = os.path.relpath(dirname, source_path)
for fname in fnames:
if os.path.splitext(fname)[-1] in (".rst", ".ipynb"):
fname = os.path.relpath(os.path.join(dirname, fname), source_path)
rel_fname = os.path.relpath(os.path.join(dirname, fname), source_path)

if fname == "index.rst" and os.path.abspath(dirname) == source_path:
if rel_fname == "index.rst" and os.path.abspath(dirname) == source_path:
continue
if pattern == "-api" and reldir.startswith("reference"):
exclude_patterns.append(fname)
exclude_patterns.append(rel_fname)
elif (
pattern == "whatsnew"
and not reldir.startswith("reference")
and reldir != "whatsnew"
):
exclude_patterns.append(fname)
elif single_doc and fname != pattern:
exclude_patterns.append(fname)
exclude_patterns.append(rel_fname)
elif single_doc and rel_fname != pattern:
exclude_patterns.append(rel_fname)

with open(os.path.join(source_path, "index.rst.template")) as f:
t = jinja2.Template(f.read())
Expand Down
4 changes: 2 additions & 2 deletions pandas/_config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ def _set_option(*args, **kwargs) -> None:
o.validator(v)

# walk the nested dict
root, k = _get_root(key)
root[k] = v
root, k_root = _get_root(key)
root[k_root] = v

if o.cb:
if silent:
Expand Down
10 changes: 5 additions & 5 deletions pandas/util/version/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,16 +253,16 @@ def is_devrelease(self) -> bool:

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

if not part or part == ".":
if not mapped_part or mapped_part == ".":
continue

if part[:1] in "0123456789":
if mapped_part[:1] in "0123456789":
# pad for numeric comparison
yield part.zfill(8)
yield mapped_part.zfill(8)
else:
yield "*" + part
yield "*" + mapped_part

# ensure that alpha/beta/candidate are before final
yield "*final"
Expand Down
9 changes: 5 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,7 @@ ignore = [
"B904",
# Magic number
"PLR2004",
# Outer loop variable overwritten by inner assignment
"PLW2901",
# Consider `elif` instead of `else` then `if` to remove indentation level
# Consider `elif` instead of `else` then `if` to remove indendation level
"PLR5501",
]

Expand All @@ -292,7 +290,10 @@ exclude = [
# relative imports allowed for asv_bench
"asv_bench/*" = ["TID"]
# to be enabled gradually
"pandas/core/*" = ["PLR5501"]
"pandas/core/*" = ["PLR5501", "PLW2901"]
"pandas/io/*" = ["PLW2901"]
"pandas/tests/*" = ["PLW2901"]
"pandas/plotting/*" = ["PLW2901"]
# TCH to be enabled gradually
"pandas/core/nanops.py" = ["TCH"]
"pandas/_libs/*" = ["TCH"]
Expand Down
9 changes: 7 additions & 2 deletions scripts/no_bool_in_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,15 @@ def replace_bool_with_bool_t(to_replace, content: str) -> str:
new_lines = []

for n, line in enumerate(content.splitlines(), start=1):
replaced_line = line
if n in to_replace:
for col_offset in reversed(to_replace[n]):
line = line[:col_offset] + "bool_t" + line[col_offset + 4 :]
new_lines.append(line)
replaced_line = (
replaced_line[:col_offset]
+ "bool_t"
+ replaced_line[col_offset + 4 :]
)
new_lines.append(replaced_line)
return "\n".join(new_lines)


Expand Down
22 changes: 11 additions & 11 deletions scripts/validate_docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,33 +103,33 @@ def get_api_items(api_doc_fd):
previous_line = current_section = current_subsection = ""
position = None
for line in api_doc_fd:
line = line.strip()
if len(line) == len(previous_line):
if set(line) == set("-"):
line_stripped = line.strip()
if len(line_stripped) == len(previous_line):
if set(line_stripped) == set("-"):
current_section = previous_line
continue
if set(line) == set("~"):
if set(line_stripped) == set("~"):
current_subsection = previous_line
continue

if line.startswith(".. currentmodule::"):
current_module = line.replace(".. currentmodule::", "").strip()
if line_stripped.startswith(".. currentmodule::"):
current_module = line_stripped.replace(".. currentmodule::", "").strip()
continue

if line == ".. autosummary::":
if line_stripped == ".. autosummary::":
position = "autosummary"
continue

if position == "autosummary":
if line == "":
if line_stripped == "":
position = "items"
continue

if position == "items":
if line == "":
if line_stripped == "":
position = None
continue
item = line.strip()
item = line_stripped.strip()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol, the improved name you've chosen actually makes it clear that this line is probably unnecessary 😄 if you wanted to clean this up in a follow-up, that'd be welcome

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will defo follow up this topic. From this Sunday I will have time to dedicate to it :)

if item in IGNORE_VALIDATION:
continue
func = importlib.import_module(current_module)
Expand All @@ -143,7 +143,7 @@ def get_api_items(api_doc_fd):
current_subsection,
)

previous_line = line
previous_line = line_stripped


class PandasDocstring(Validator):
Expand Down
8 changes: 4 additions & 4 deletions scripts/validate_min_versions_in_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,11 @@ def pin_min_versions_to_yaml_file(
data = data.replace(old_dep, new_dep, 1)
continue
toml_version = version.parse(min_dep)
yaml_versions = clean_version_list(yaml_versions, toml_version)
cleaned_yaml_versions = [x for x in yaml_versions if "-" not in x]
yaml_versions_list = clean_version_list(yaml_versions, toml_version)
cleaned_yaml_versions = [x for x in yaml_versions_list if "-" not in x]
new_dep = yaml_package
for yaml_version in cleaned_yaml_versions:
new_dep += yaml_version + ", "
for clean_yaml_version in cleaned_yaml_versions:
new_dep += clean_yaml_version + ", "
operator = get_operator_from(new_dep)
if operator != "=":
new_dep += ">=" + min_dep
Expand Down
8 changes: 4 additions & 4 deletions scripts/validate_rst_title_capitalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,15 @@ def find_titles(rst_file: str) -> Iterable[tuple[str, int]]:
with open(rst_file) as fd:
previous_line = ""
for i, line in enumerate(fd):
line = line[:-1]
line_chars = set(line)
line_no_last_elem = line[:-1]
line_chars = set(line_no_last_elem)
if (
len(line_chars) == 1
and line_chars.pop() in symbols
and len(line) == len(previous_line)
and len(line_no_last_elem) == len(previous_line)
):
yield re.sub(r"[`\*_]", "", previous_line), i
previous_line = line
previous_line = line_no_last_elem


def main(source_paths: list[str]) -> int:
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ def initialize_options(self):

# clean the generated pxi files
for pxifile in _pxifiles:
pxifile = pxifile.replace(".pxi.in", ".pxi")
self._clean_me.append(pxifile)
pxifile_replaced = pxifile.replace(".pxi.in", ".pxi")
self._clean_me.append(pxifile_replaced)

for d in ("build", "dist"):
if os.path.exists(d):
Expand Down
8 changes: 4 additions & 4 deletions web/pandas_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,9 +369,9 @@ def get_source_files(source_path: str) -> typing.Generator[str, None, None]:
Generate the list of files present in the source directory.
"""
for root, dirs, fnames in os.walk(source_path):
root = os.path.relpath(root, source_path)
root_rel_path = os.path.relpath(root, source_path)
for fname in fnames:
yield os.path.join(root, fname)
yield os.path.join(root_rel_path, fname)


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