From 9ee9f3dc92ceb803eaf89c4504600f39e5e9f38d Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sat, 28 Jun 2025 10:10:43 +0200 Subject: [PATCH 1/3] Ignore ruff/Pylint rule PLR0124 PLR0124 Name compared with itself --- pyproject.toml | 2 ++ xarray/core/duck_array_ops.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 36170540135..a18161e88ee 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -293,6 +293,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"] diff --git a/xarray/core/duck_array_ops.py b/xarray/core/duck_array_ops.py index e98ac0f36a1..35f3b425697 100644 --- a/xarray/core/duck_array_ops.py +++ b/xarray/core/duck_array_ops.py @@ -193,7 +193,7 @@ def isnull(data): # 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): From b34c22a9eafc9348e967a6ea16df0616c6ebfe8e Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sat, 28 Jun 2025 10:14:00 +0200 Subject: [PATCH 2/3] Apply ruff/Pylint rule PLR5501 PLR5501 Use `elif` instead of `else` then `if`, to reduce indentation --- xarray/coding/strings.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/xarray/coding/strings.py b/xarray/coding/strings.py index c917faaf383..8fba967240c 100644 --- a/xarray/coding/strings.py +++ b/xarray/coding/strings.py @@ -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 ( + 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}" From 927ff721dac37914c87572f9bc10b21282e35425 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sat, 28 Jun 2025 10:17:15 +0200 Subject: [PATCH 3/3] Enforce ruff/Pylint Refactor rules (PLR) --- pyproject.toml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index a18161e88ee..f303623d553 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 @@ -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