Skip to content

Commit 285261a

Browse files
authored
Merge pull request #68 from cclauss/a-bit-more-ruff
ruff rules B904 and PLW1510
2 parents dbf2a6b + b721186 commit 285261a

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

pyproject.toml

+3-5
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,12 @@ extend-select = [
8888
]
8989
ignore = [
9090
"B023", # Function definition does not bind loop variable
91-
"B904", # Raise exception with `raise ... from err`
92-
"PLW1510", # `subprocess.run` without explicit `check` argument
9391
"T201", # Print statements
94-
"ISC001", # Conflicts with the formatter in 0.1.2
92+
"ISC001", # Conflicts with ruff format
9593
]
9694
unfixable = [
97-
"F841", # Removes unused variables
98-
"T20", # Removes print statements
95+
"F841", # Removes unused variables
96+
"T20", # Removes print statements
9997
]
10098

10199
[tool.ruff.lint.mccabe]

removestar/removestar.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def fix_code(
6161
try:
6262
tree = ast.parse(code, filename=file)
6363
except SyntaxError as e:
64-
raise RuntimeError(f"SyntaxError: {e}")
64+
raise RuntimeError(f"SyntaxError: {e}") from e
6565

6666
checker = Checker(tree)
6767

@@ -352,24 +352,24 @@ def get_module_names(mod, directory, *, allow_dynamic=True, _found=()):
352352
"""
353353
try:
354354
names = get_names_from_dir(mod, directory, allow_dynamic=allow_dynamic, _found=_found)
355-
except ExternalModuleError:
355+
except ExternalModuleError as e:
356356
if allow_dynamic:
357357
names = get_names_dynamically(mod)
358358
else:
359359
raise NotImplementedError(
360360
"Static determination of external module imports is not supported."
361-
)
361+
) from e
362362
return names
363363

364364

365365
def get_names_dynamically(mod):
366366
d = {}
367367
try:
368368
exec(f"from {mod} import *", d)
369-
except ImportError:
370-
raise RuntimeError(f"Could not import {mod}")
369+
except ImportError as import_e:
370+
raise RuntimeError(f"Could not import {mod}") from import_e
371371
except Exception as e:
372-
raise RuntimeError(f"Error importing {mod}: {e}")
372+
raise RuntimeError(f"Error importing {mod}: {e}") from e
373373
return d.keys() - set(MAGIC_GLOBALS)
374374

375375

@@ -382,9 +382,9 @@ def get_names_from_dir(mod, directory, *, allow_dynamic=True, _found=()):
382382
try:
383383
names = get_names(code, filename)
384384
except SyntaxError as e:
385-
raise RuntimeError(f"Could not parse {filename}: {e}")
386-
except RuntimeError:
387-
raise RuntimeError(f"Could not parse the names from {filename}")
385+
raise RuntimeError(f"Could not parse {filename}: {e}") from e
386+
except RuntimeError as runtime_e:
387+
raise RuntimeError(f"Could not parse the names from {filename}") from runtime_e
388388

389389
for name in names.copy():
390390
if name.endswith(".*"):

tests/test_removestar.py

+8
Original file line numberDiff line numberDiff line change
@@ -1527,6 +1527,7 @@ def test_cli(tmpdir):
15271527
[sys.executable, "-m", "removestar", "--_this-file", "none"],
15281528
capture_output=True,
15291529
encoding="utf-8",
1530+
check=False,
15301531
)
15311532
assert p.stderr == ""
15321533
assert p.stdout == __file__
@@ -1535,6 +1536,7 @@ def test_cli(tmpdir):
15351536
[sys.executable, "-m", "removestar", directory],
15361537
capture_output=True,
15371538
encoding="utf-8",
1539+
check=False,
15381540
)
15391541
warnings = set(
15401542
f"""\
@@ -1688,6 +1690,7 @@ def func():
16881690
[sys.executable, "-m", "removestar", "--quiet", directory],
16891691
capture_output=True,
16901692
encoding="utf-8",
1693+
check=False,
16911694
)
16921695
assert p.stderr == ""
16931696
for d in diffs:
@@ -1699,6 +1702,7 @@ def func():
16991702
[sys.executable, "-m", "removestar", "--verbose", directory],
17001703
capture_output=True,
17011704
encoding="utf-8",
1705+
check=False,
17021706
)
17031707
changes = set(
17041708
f"""\
@@ -1736,6 +1740,7 @@ def func():
17361740
[sys.executable, "-m", "removestar", "--no-dynamic-importing", directory],
17371741
capture_output=True,
17381742
encoding="utf-8",
1743+
check=False,
17391744
)
17401745
static_error = set(
17411746
f"""\
@@ -1765,6 +1770,7 @@ def func():
17651770
],
17661771
capture_output=True,
17671772
encoding="utf-8",
1773+
check=False,
17681774
)
17691775
assert p.stderr == ""
17701776
for d in diffs:
@@ -1780,6 +1786,7 @@ def func():
17801786
[sys.executable, "-m", "removestar", "--quiet", "-i", directory],
17811787
capture_output=True,
17821788
encoding="utf-8",
1789+
check=False,
17831790
)
17841791
assert p.stderr == ""
17851792
assert p.stdout == ""
@@ -1832,6 +1839,7 @@ def func():
18321839
[sys.executable, "-m", "removestar", directory / "notarealfile.py"],
18331840
capture_output=True,
18341841
encoding="utf-8",
1842+
check=False,
18351843
)
18361844
assert p.stderr == red(f"Error: {directory}/notarealfile.py: no such file or directory") + "\n"
18371845
assert p.stdout == ""

0 commit comments

Comments
 (0)