From 92989c2b836ded82c5d0e1b24971f1b73efdaf48 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sat, 29 Jun 2024 14:21:38 +0200 Subject: [PATCH] Enforce ruff/tryceratops rule TRY300 TRY300 Consider moving this statement to an `else` block --- pkg_resources/extern/__init__.py | 10 +++++----- ruff.toml | 2 ++ setuptools/__init__.py | 2 +- setuptools/command/easy_install.py | 2 +- setuptools/config/_validate_pyproject/formats.py | 2 +- setuptools/depends.py | 6 +++--- setuptools/extension.py | 5 ++--- setuptools/extern/__init__.py | 10 +++++----- setuptools/tests/test_sdist.py | 2 +- 9 files changed, 21 insertions(+), 20 deletions(-) diff --git a/pkg_resources/extern/__init__.py b/pkg_resources/extern/__init__.py index 9b9ac10aa9..daa978ff72 100644 --- a/pkg_resources/extern/__init__.py +++ b/pkg_resources/extern/__init__.py @@ -41,14 +41,14 @@ def load_module(self, fullname: str): """ root, base, target = fullname.partition(self.root_name + '.') for prefix in self.search_path: + extant = prefix + target try: - extant = prefix + target __import__(extant) - mod = sys.modules[extant] - sys.modules[fullname] = mod - return mod except ImportError: - pass + continue + mod = sys.modules[extant] + sys.modules[fullname] = mod + return mod else: raise ImportError( "The '{target}' package is required; " diff --git a/ruff.toml b/ruff.toml index 8828fe61a5..2effe696ea 100644 --- a/ruff.toml +++ b/ruff.toml @@ -14,9 +14,11 @@ extend-select = [ "F404", # late-future-import "PYI", # flake8-pyi "UP", # pyupgrade + "TRY", "YTT", # flake8-2020 ] ignore = [ + "TRY301", # raise-within-try, it's handy "UP015", # redundant-open-modes, explicit is preferred "UP030", # temporarily disabled "UP031", # temporarily disabled diff --git a/setuptools/__init__.py b/setuptools/__init__.py index 5ef0f7dbd8..bf03f37b77 100644 --- a/setuptools/__init__.py +++ b/setuptools/__init__.py @@ -57,9 +57,9 @@ def _get_project_config_files(self, filenames=None): """Ignore ``pyproject.toml``, they are not related to setup_requires""" try: cfg, toml = super()._split_standard_project_metadata(filenames) - return cfg, () except Exception: return filenames, () + return cfg, () def finalize_options(self): """ diff --git a/setuptools/command/easy_install.py b/setuptools/command/easy_install.py index 5ec5080131..7b00801ca6 100644 --- a/setuptools/command/easy_install.py +++ b/setuptools/command/easy_install.py @@ -102,9 +102,9 @@ def _to_bytes(s): def isascii(s): try: s.encode('ascii') - return True except UnicodeError: return False + return True def _one_liner(text): diff --git a/setuptools/config/_validate_pyproject/formats.py b/setuptools/config/_validate_pyproject/formats.py index 5a0599cbb5..aacf4092b0 100644 --- a/setuptools/config/_validate_pyproject/formats.py +++ b/setuptools/config/_validate_pyproject/formats.py @@ -91,9 +91,9 @@ def pep508(value: str) -> bool: """ try: _req.Requirement(value) - return True except _req.InvalidRequirement: return False + return True except ImportError: # pragma: no cover _logger.warning( diff --git a/setuptools/depends.py b/setuptools/depends.py index b6af51c410..2226b6784a 100644 --- a/setuptools/depends.py +++ b/setuptools/depends.py @@ -58,11 +58,11 @@ def get_version(self, paths=None, default="unknown"): if self.attribute is None: try: f, p, i = find_module(self.module, paths) - if f: - f.close() - return default except ImportError: return None + if f: + f.close() + return default v = get_module_constant(self.module, self.attribute, default, paths) diff --git a/setuptools/extension.py b/setuptools/extension.py index 8caad78d4b..25420f42de 100644 --- a/setuptools/extension.py +++ b/setuptools/extension.py @@ -16,10 +16,9 @@ def _have_cython(): try: # from (cython_impl) import build_ext __import__(cython_impl, fromlist=['build_ext']).build_ext - return True except Exception: - pass - return False + return False + return True # for compatibility diff --git a/setuptools/extern/__init__.py b/setuptools/extern/__init__.py index 5ad7169e3b..f9b6eea70d 100644 --- a/setuptools/extern/__init__.py +++ b/setuptools/extern/__init__.py @@ -32,14 +32,14 @@ def load_module(self, fullname): """ root, base, target = fullname.partition(self.root_name + '.') for prefix in self.search_path: + extant = prefix + target try: - extant = prefix + target __import__(extant) - mod = sys.modules[extant] - sys.modules[fullname] = mod - return mod except ImportError: - pass + continue + mod = sys.modules[extant] + sys.modules[fullname] = mod + return mod else: raise ImportError( "The '{target}' package is required; " diff --git a/setuptools/tests/test_sdist.py b/setuptools/tests/test_sdist.py index 387ec3bebf..1be568d3fa 100644 --- a/setuptools/tests/test_sdist.py +++ b/setuptools/tests/test_sdist.py @@ -122,9 +122,9 @@ def touch(path): def symlink_or_skip_test(src, dst): try: os.symlink(src, dst) - return dst except (OSError, NotImplementedError): pytest.skip("symlink not supported in OS") + return dst class TestSdistTest: