From bb5f936f4b73d4c10f3a0a87a63f54997c4c5740 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sat, 29 Jun 2024 12:37:17 +0200 Subject: [PATCH 1/3] Apply ruff/tryceratops rule TRY300 TRY300 Consider moving this statement to an `else` block --- distutils/cygwinccompiler.py | 5 +++-- distutils/file_util.py | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/distutils/cygwinccompiler.py b/distutils/cygwinccompiler.py index ce412e83..88b9bc65 100644 --- a/distutils/cygwinccompiler.py +++ b/distutils/cygwinccompiler.py @@ -309,6 +309,9 @@ def check_config_h(): fn = sysconfig.get_config_h_filename() try: config_h = pathlib.Path(fn).read_text(encoding='utf-8') + except OSError as exc: + return (CONFIG_H_UNCERTAIN, f"couldn't read '{fn}': {exc.strerror}") + else: substring = '__GNUC__' if substring in config_h: code = CONFIG_H_OK @@ -317,8 +320,6 @@ def check_config_h(): code = CONFIG_H_NOTOK mention_inflected = 'does not mention' return code, f"{fn!r} {mention_inflected} {substring!r}" - except OSError as exc: - return (CONFIG_H_UNCERTAIN, f"couldn't read '{fn}': {exc.strerror}") def is_cygwincc(cc): diff --git a/distutils/file_util.py b/distutils/file_util.py index b19a5dcf..85ee4daf 100644 --- a/distutils/file_util.py +++ b/distutils/file_util.py @@ -140,12 +140,13 @@ def copy_file( # noqa: C901 if not (os.path.exists(dst) and os.path.samefile(src, dst)): try: os.link(src, dst) - return (dst, 1) except OSError: # If hard linking fails, fall back on copying file # (some special filesystems don't support hard linking # even under Unix, see issue #8876). pass + else: + return (dst, 1) elif link == 'sym': if not (os.path.exists(dst) and os.path.samefile(src, dst)): os.symlink(src, dst) From 51dc58b0f6cb5d16d610caf308cc82a1e05199c3 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sat, 29 Jun 2024 12:51:47 +0200 Subject: [PATCH 2/3] Apply ruff/tryceratops rule TRY301 TRY301 Abstract `raise` to an inner function --- distutils/command/install_lib.py | 6 +++--- distutils/msvc9compiler.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/distutils/command/install_lib.py b/distutils/command/install_lib.py index 01579d46..4c1230a2 100644 --- a/distutils/command/install_lib.py +++ b/distutils/command/install_lib.py @@ -81,9 +81,9 @@ def finalize_options(self): if not isinstance(self.optimize, int): try: self.optimize = int(self.optimize) - if self.optimize not in (0, 1, 2): - raise AssertionError - except (ValueError, AssertionError): + except ValueError: + pass + if self.optimize not in (0, 1, 2): raise DistutilsOptionError("optimize must be 0, 1, or 2") def run(self): diff --git a/distutils/msvc9compiler.py b/distutils/msvc9compiler.py index 4c708487..b41f54f2 100644 --- a/distutils/msvc9compiler.py +++ b/distutils/msvc9compiler.py @@ -154,7 +154,7 @@ def load_macros(self, version): if version >= 8.0: self.set_macro("FrameworkSDKDir", NET_BASE, "sdkinstallrootv2.0") else: - raise KeyError("sdkinstallrootv2.0") + raise KeyError("sdkinstallrootv2.0") # noqa: TRY301 except KeyError: raise DistutilsPlatformError( """Python was built with Visual Studio 2008; From e1664778ccadd96d71ce0b38542202ea6b209d2a Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sat, 29 Jun 2024 12:58:03 +0200 Subject: [PATCH 3/3] Enforce ruff/tryceratops rules (TRY) --- distutils/extension.py | 2 +- ruff.toml | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/distutils/extension.py b/distutils/extension.py index b302082f..33159079 100644 --- a/distutils/extension.py +++ b/distutils/extension.py @@ -105,7 +105,7 @@ def __init__( **kw, # To catch unknown keywords ): if not isinstance(name, str): - raise AssertionError("'name' must be a string") + raise AssertionError("'name' must be a string") # noqa: TRY004 if not ( isinstance(sources, list) and all(isinstance(v, (str, os.PathLike)) for v in sources) diff --git a/ruff.toml b/ruff.toml index b7850b6d..84accd28 100644 --- a/ruff.toml +++ b/ruff.toml @@ -8,9 +8,11 @@ extend-select = [ "ISC", "RUF010", "RUF100", + "TRY", "UP", ] ignore = [ + "TRY003", # https://docs.astral.sh/ruff/formatter/#conflicting-lint-rules "W191", "E111",