From 7f9997807852bb2592d51dd9f6384f6f1212e4f6 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Wed, 28 May 2025 19:00:12 +0200 Subject: [PATCH 1/3] Apply ruff/refurb rule FURB136 FURB136 Replace `x if x >= y else y` with `max(x, y)` --- xarray/core/accessor_str.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/core/accessor_str.py b/xarray/core/accessor_str.py index f35f7dbed6f..06570ceba3a 100644 --- a/xarray/core/accessor_str.py +++ b/xarray/core/accessor_str.py @@ -1944,7 +1944,7 @@ def replace( if regex: pat = self._re_compile(pat=pat, flags=flags, case=case) func = lambda x, ipat, irepl, i_n: ipat.sub( - repl=irepl, string=x, count=i_n if i_n >= 0 else 0 + repl=irepl, string=x, count=max(i_n, 0) ) else: pat = self._stringify(pat) From 2709e818f4e75314296fc9488e39452329ab4802 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Wed, 28 May 2025 19:02:01 +0200 Subject: [PATCH 2/3] Apply ruff/refurb rule FURB167 FURB167 Use of regular expression alias The abbreviated flags seem less readable, although experienced programers might know the abbreviated flags match inline flags. --- xarray/tests/test_accessor_str.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/xarray/tests/test_accessor_str.py b/xarray/tests/test_accessor_str.py index d7838ff0667..e2360380df7 100644 --- a/xarray/tests/test_accessor_str.py +++ b/xarray/tests/test_accessor_str.py @@ -906,7 +906,7 @@ def test_extractall_single_single_nocase(dtype) -> None: pat_re: str | bytes = ( pat_str if dtype == np.str_ else bytes(pat_str, encoding="UTF-8") ) - pat_compiled = re.compile(pat_re, flags=re.I) + pat_compiled = re.compile(pat_re, flags=re.IGNORECASE) value = xr.DataArray( [["a_Xy_0", "ab_xY_10", "abc_Xy_01"], ["abcd_Xy_", "", "abcdef_Xy_101"]], @@ -981,7 +981,7 @@ def test_extractall_single_multi_nocase(dtype) -> None: pat_re: str | bytes = ( pat_str if dtype == np.str_ else bytes(pat_str, encoding="UTF-8") ) - pat_compiled = re.compile(pat_re, flags=re.I) + pat_compiled = re.compile(pat_re, flags=re.IGNORECASE) value = xr.DataArray( [ @@ -1063,7 +1063,7 @@ def test_extractall_multi_single_nocase(dtype) -> None: pat_re: str | bytes = ( pat_str if dtype == np.str_ else bytes(pat_str, encoding="UTF-8") ) - pat_compiled = re.compile(pat_re, flags=re.I) + pat_compiled = re.compile(pat_re, flags=re.IGNORECASE) value = xr.DataArray( [["a_Xy_0", "ab_xY_10", "abc_Xy_01"], ["abcd_Xy_", "", "abcdef_Xy_101"]], @@ -1145,7 +1145,7 @@ def test_extractall_multi_multi_nocase(dtype) -> None: pat_re: str | bytes = ( pat_str if dtype == np.str_ else bytes(pat_str, encoding="UTF-8") ) - pat_compiled = re.compile(pat_re, flags=re.I) + pat_compiled = re.compile(pat_re, flags=re.IGNORECASE) value = xr.DataArray( [ @@ -1245,7 +1245,7 @@ def test_findall_single_single_case(dtype) -> None: def test_findall_single_single_nocase(dtype) -> None: pat_str = r"(\w+)_Xy_\d*" - pat_re = re.compile(dtype(pat_str), flags=re.I) + pat_re = re.compile(dtype(pat_str), flags=re.IGNORECASE) value = xr.DataArray( [["a_Xy_0", "ab_xY_10", "abc_Xy_01"], ["abcd_Xy_", "", "abcdef_Xy_101"]], @@ -1313,7 +1313,7 @@ def test_findall_single_multi_case(dtype) -> None: def test_findall_single_multi_nocase(dtype) -> None: pat_str = r"(\w+)_Xy_\d*" - pat_re = re.compile(dtype(pat_str), flags=re.I) + pat_re = re.compile(dtype(pat_str), flags=re.IGNORECASE) value = xr.DataArray( [ @@ -1387,7 +1387,7 @@ def test_findall_multi_single_case(dtype) -> None: def test_findall_multi_single_nocase(dtype) -> None: pat_str = r"(\w+)_Xy_(\d*)" - pat_re = re.compile(dtype(pat_str), flags=re.I) + pat_re = re.compile(dtype(pat_str), flags=re.IGNORECASE) value = xr.DataArray( [["a_Xy_0", "ab_xY_10", "abc_Xy_01"], ["abcd_Xy_", "", "abcdef_Xy_101"]], @@ -1463,7 +1463,7 @@ def test_findall_multi_multi_case(dtype) -> None: def test_findall_multi_multi_nocase(dtype) -> None: pat_str = r"(\w+)_Xy_(\d*)" - pat_re = re.compile(dtype(pat_str), flags=re.I) + pat_re = re.compile(dtype(pat_str), flags=re.IGNORECASE) value = xr.DataArray( [ From 46e7f239350ecf8a8fb9362309b588cb514341f0 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Wed, 28 May 2025 19:06:45 +0200 Subject: [PATCH 3/3] Enforce ruff/refurb rules (FURB) --- pyproject.toml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index d95a7dcba5d..717580f82f2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -258,6 +258,7 @@ extend-select = [ "TID", # flake8-tidy-imports (absolute imports) "PGH", # pygrep-hooks "PERF", # Perflint + "FURB", # refurb "RUF", ] extend-safe-fixes = [ @@ -270,6 +271,7 @@ ignore = [ "C40", # unnecessary generator, comprehension, or literal "PIE790", # unnecessary pass statement "PERF203", # try-except within a loop incurs performance overhead + "FURB105", # unnecessary empty string passed to `print` "RUF001", # string contains ambiguous unicode character "RUF002", # docstring contains ambiguous acute accent unicode character "RUF003", # comment contains ambiguous no-break space unicode character