Skip to content

Enforce ruff/Pylint Refactor rules (PLR) #10459

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 3 commits into from
Jun 28, 2025
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
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ extend-select = [
"W", # pycodestyle warnings
"PGH", # pygrep-hooks
"PLE", # Pylint Errors
"PLR", # Pylint Refactor
"PLW", # Pylint Warnings
"UP", # pyupgrade
"FURB", # refurb
Expand All @@ -277,6 +278,8 @@ ignore = [
"PERF203", # try-except within a loop incurs performance overhead
"E402", # module level import not at top of file
"E731", # do not assign a lambda expression, use a def
"PLR091", # too many arguments / branches / statements
"PLR2004", # magic value used in comparison
"PLW0603", # using the global statement to update is discouraged
"PLW0642", # reassigned `self` variable in instance method
"PLW1641", # object does not implement `__hash__` method
Expand All @@ -293,6 +296,8 @@ ignore = [
[tool.ruff.lint.per-file-ignores]
# don't enforce absolute imports
"asv_bench/**" = ["TID252"]
# comparison with itself in tests
"xarray/tests/**" = ["PLR0124"]
# looks like ruff bugs
"xarray/core/_typed_ops.py" = ["PYI034"]
"xarray/namedarray/_typing.py" = ["PYI018", "PYI046"]
Expand Down
17 changes: 8 additions & 9 deletions xarray/coding/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,14 @@ def validate_char_dim_name(strlen, encoding, name) -> str:
"To silence this warning either remove 'char_dim_name' from encoding or provide a fitting name."
)
char_dim_name = f"{new_dim_name}{strlen}"
else:
if (
original_shape := encoding.get("original_shape", [-1])[-1]
) != -1 and original_shape != strlen:
emit_user_level_warning(
f"String dimension length mismatch on variable {name!r}. '{original_shape}' provided by encoding, but data has length of '{strlen}'. Using '{char_dim_name}{strlen}' instead of {char_dim_name!r} to prevent possible naming clash.\n"
f"To silence this warning remove 'original_shape' from encoding."
)
char_dim_name = f"{char_dim_name}{strlen}"
elif (
Copy link
Contributor

Choose a reason for hiding this comment

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

@DimitriPapadopoulos Great to see this applied to the code base. One question, will this also be detected during pre-commit now?

Copy link
Contributor Author

@DimitriPapadopoulos DimitriPapadopoulos Jun 28, 2025

Choose a reason for hiding this comment

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

Yes, pre-commit runs ruff, which reads its configuration from pyproject.toml as usual.

original_shape := encoding.get("original_shape", [-1])[-1]
) != -1 and original_shape != strlen:
emit_user_level_warning(
f"String dimension length mismatch on variable {name!r}. '{original_shape}' provided by encoding, but data has length of '{strlen}'. Using '{char_dim_name}{strlen}' instead of {char_dim_name!r} to prevent possible naming clash.\n"
f"To silence this warning remove 'original_shape' from encoding."
)
char_dim_name = f"{char_dim_name}{strlen}"
else:
char_dim_name = f"string{strlen}"

Expand Down
2 changes: 1 addition & 1 deletion xarray/core/duck_array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@
# types. For full consistency with pandas, we should accept None as
# a null value as well as NaN, but it isn't clear how to do this
# with duck typing.
return data != data
return data != data # noqa: PLR0124


def notnull(data):
Expand Down Expand Up @@ -250,7 +250,7 @@

if xp == np:
# numpy currently doesn't have a astype:
return data.astype(dtype, **kwargs)

Check warning on line 253 in xarray/core/duck_array_ops.py

View workflow job for this annotation

GitHub Actions / macos-latest py3.10

invalid value encountered in cast

Check warning on line 253 in xarray/core/duck_array_ops.py

View workflow job for this annotation

GitHub Actions / macos-latest py3.10

invalid value encountered in cast

Check warning on line 253 in xarray/core/duck_array_ops.py

View workflow job for this annotation

GitHub Actions / windows-latest py3.10

invalid value encountered in cast

Check warning on line 253 in xarray/core/duck_array_ops.py

View workflow job for this annotation

GitHub Actions / windows-latest py3.10

invalid value encountered in cast
return xp.astype(data, dtype, **kwargs)


Expand Down
Loading