From b721186038118aa351b793f13f1a64c1d2535eaa Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 27 Nov 2024 14:43:01 +0100 Subject: [PATCH] ruff rules B904 and PLW1510 --- pyproject.toml | 8 +++----- removestar/removestar.py | 18 +++++++++--------- tests/test_removestar.py | 8 ++++++++ 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 7872c6d..b3fad8c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -88,14 +88,12 @@ extend-select = [ ] ignore = [ "B023", # Function definition does not bind loop variable - "B904", # Raise exception with `raise ... from err` - "PLW1510", # `subprocess.run` without explicit `check` argument "T201", # Print statements - "ISC001", # Conflicts with the formatter in 0.1.2 + "ISC001", # Conflicts with ruff format ] unfixable = [ - "F841", # Removes unused variables - "T20", # Removes print statements + "F841", # Removes unused variables + "T20", # Removes print statements ] [tool.ruff.lint.mccabe] diff --git a/removestar/removestar.py b/removestar/removestar.py index ad325db..30baed8 100644 --- a/removestar/removestar.py +++ b/removestar/removestar.py @@ -61,7 +61,7 @@ def fix_code( try: tree = ast.parse(code, filename=file) except SyntaxError as e: - raise RuntimeError(f"SyntaxError: {e}") + raise RuntimeError(f"SyntaxError: {e}") from e checker = Checker(tree) @@ -352,13 +352,13 @@ def get_module_names(mod, directory, *, allow_dynamic=True, _found=()): """ try: names = get_names_from_dir(mod, directory, allow_dynamic=allow_dynamic, _found=_found) - except ExternalModuleError: + except ExternalModuleError as e: if allow_dynamic: names = get_names_dynamically(mod) else: raise NotImplementedError( "Static determination of external module imports is not supported." - ) + ) from e return names @@ -366,10 +366,10 @@ def get_names_dynamically(mod): d = {} try: exec(f"from {mod} import *", d) - except ImportError: - raise RuntimeError(f"Could not import {mod}") + except ImportError as import_e: + raise RuntimeError(f"Could not import {mod}") from import_e except Exception as e: - raise RuntimeError(f"Error importing {mod}: {e}") + raise RuntimeError(f"Error importing {mod}: {e}") from e return d.keys() - set(MAGIC_GLOBALS) @@ -382,9 +382,9 @@ def get_names_from_dir(mod, directory, *, allow_dynamic=True, _found=()): try: names = get_names(code, filename) except SyntaxError as e: - raise RuntimeError(f"Could not parse {filename}: {e}") - except RuntimeError: - raise RuntimeError(f"Could not parse the names from {filename}") + raise RuntimeError(f"Could not parse {filename}: {e}") from e + except RuntimeError as runtime_e: + raise RuntimeError(f"Could not parse the names from {filename}") from runtime_e for name in names.copy(): if name.endswith(".*"): diff --git a/tests/test_removestar.py b/tests/test_removestar.py index 7da5660..dec9cc4 100644 --- a/tests/test_removestar.py +++ b/tests/test_removestar.py @@ -1527,6 +1527,7 @@ def test_cli(tmpdir): [sys.executable, "-m", "removestar", "--_this-file", "none"], capture_output=True, encoding="utf-8", + check=False, ) assert p.stderr == "" assert p.stdout == __file__ @@ -1535,6 +1536,7 @@ def test_cli(tmpdir): [sys.executable, "-m", "removestar", directory], capture_output=True, encoding="utf-8", + check=False, ) warnings = set( f"""\ @@ -1688,6 +1690,7 @@ def func(): [sys.executable, "-m", "removestar", "--quiet", directory], capture_output=True, encoding="utf-8", + check=False, ) assert p.stderr == "" for d in diffs: @@ -1699,6 +1702,7 @@ def func(): [sys.executable, "-m", "removestar", "--verbose", directory], capture_output=True, encoding="utf-8", + check=False, ) changes = set( f"""\ @@ -1736,6 +1740,7 @@ def func(): [sys.executable, "-m", "removestar", "--no-dynamic-importing", directory], capture_output=True, encoding="utf-8", + check=False, ) static_error = set( f"""\ @@ -1765,6 +1770,7 @@ def func(): ], capture_output=True, encoding="utf-8", + check=False, ) assert p.stderr == "" for d in diffs: @@ -1780,6 +1786,7 @@ def func(): [sys.executable, "-m", "removestar", "--quiet", "-i", directory], capture_output=True, encoding="utf-8", + check=False, ) assert p.stderr == "" assert p.stdout == "" @@ -1832,6 +1839,7 @@ def func(): [sys.executable, "-m", "removestar", directory / "notarealfile.py"], capture_output=True, encoding="utf-8", + check=False, ) assert p.stderr == red(f"Error: {directory}/notarealfile.py: no such file or directory") + "\n" assert p.stdout == ""