Skip to content

Commit

Permalink
Merge pull request #68 from cclauss/a-bit-more-ruff
Browse files Browse the repository at this point in the history
ruff rules B904 and PLW1510
  • Loading branch information
Saransh-cpp authored Nov 27, 2024
2 parents dbf2a6b + b721186 commit 285261a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
8 changes: 3 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
18 changes: 9 additions & 9 deletions removestar/removestar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -352,24 +352,24 @@ 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


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)


Expand All @@ -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(".*"):
Expand Down
8 changes: 8 additions & 0 deletions tests/test_removestar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand All @@ -1535,6 +1536,7 @@ def test_cli(tmpdir):
[sys.executable, "-m", "removestar", directory],
capture_output=True,
encoding="utf-8",
check=False,
)
warnings = set(
f"""\
Expand Down Expand Up @@ -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:
Expand All @@ -1699,6 +1702,7 @@ def func():
[sys.executable, "-m", "removestar", "--verbose", directory],
capture_output=True,
encoding="utf-8",
check=False,
)
changes = set(
f"""\
Expand Down Expand Up @@ -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"""\
Expand Down Expand Up @@ -1765,6 +1770,7 @@ def func():
],
capture_output=True,
encoding="utf-8",
check=False,
)
assert p.stderr == ""
for d in diffs:
Expand All @@ -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 == ""
Expand Down Expand Up @@ -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 == ""

0 comments on commit 285261a

Please sign in to comment.