Skip to content

Commit 16c4dd3

Browse files
committed
Use ruff for all linting
1 parent 9d8d373 commit 16c4dd3

File tree

11 files changed

+123
-174
lines changed

11 files changed

+123
-174
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name: check format using ruff
2+
on: [push]
3+
jobs:
4+
ruff:
5+
runs-on: ubuntu-latest
6+
steps:
7+
- uses: actions/checkout@v4
8+
- uses: chartboost/ruff-action@v1
9+
with:
10+
args: format --check
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
name: Check Python formatting using Black and Ruff
2-
1+
name: lint code using ruff
32
on: [push]
4-
53
jobs:
6-
lint:
4+
ruff:
75
runs-on: ubuntu-latest
86
steps:
9-
- uses: actions/checkout@v3
10-
- uses: psf/black@stable
7+
- uses: actions/checkout@v4
118
- uses: chartboost/ruff-action@v1

.pre-commit-config.yaml

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@ ci:
33

44

55
repos:
6-
- repo: https://github.com/psf/black
7-
rev: 23.7.0
8-
hooks:
9-
- id: black
10-
language_version: python3.11
11-
12-
- repo: https://github.com/astral-sh/ruff-pre-commit
13-
rev: v0.3.5
14-
hooks:
6+
- repo: https://github.com/astral-sh/ruff-pre-commit
7+
# Ruff version.
8+
rev: v0.13.3
9+
hooks:
10+
# Run the linter.
1511
- id: ruff
16-
- id: ruff-format
12+
# Run the formatter.
13+
- id: ruff-format

acis_thermal_check/apps/acisfp_check.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def make_prediction_plots(
152152
textypos = [-108.0, -119.3, -115.7]
153153
fontsize = [12, 9, 9]
154154
for i in range(3):
155-
name = f"{self.name}_{i+1}"
155+
name = f"{self.name}_{i + 1}"
156156
plots[name] = PredictPlot(
157157
fig_id=i + 1,
158158
x=times,
@@ -379,7 +379,7 @@ def draw_obsids(
379379
if obsid > 60000:
380380
# ECS observations during the science orbit are colored blue
381381
color = "blue"
382-
else:
382+
else: # noqa: PLR5501
383383
# Color all ACIS-S observations green; all ACIS-I
384384
# observations red
385385
if in_fp == "ACIS-I":

acis_thermal_check/apps/copy_model_outputs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ def main():
8080
prefix = "Copied"
8181
try:
8282
shutil.copytree(location, copy_path, dirs_exist_ok=args.overwrite)
83-
except FileExistsError:
83+
except FileExistsError as exc:
8484
raise IOError(
8585
f"Files already exist in {copy_path} and --overwrite is not specified!"
86-
)
86+
) from exc
8787
print(f"{prefix} contents of {location} to {copy_path}.")
8888

8989

acis_thermal_check/state_builder.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,9 @@ def get_prediction_states(self, tbegin):
187187
ok = bs_cmds["event_type"] == "RUNNING_LOAD_TERMINATION_TIME"
188188
if np.any(ok):
189189
rltt = CxoTime(bs_dates[ok][0])
190-
else:
190+
else: # noqa: PLR5501
191191
# Handle the case of old loads (prior to backstop 6.9) where there
192-
# is no RLTT. If the first command is AOACRSTD this indicates the
192+
# is no RLTT. If the first command is AOACRSTD this indicates the
193193
# beginning of a maneuver ATS which may overlap by 3 mins with the
194194
# previous loads because of the AOACRSTD command. So move the RLTT
195195
# forward by 3 minutes (exactly 180.0 sec). If the first command is

acis_thermal_check/tests/regression_testing.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ def exception_catcher(test, old, new, data_type, **kwargs):
156156
old = old.astype("U")
157157
try:
158158
test(old, new, **kwargs)
159-
except AssertionError:
160-
raise AssertionError(f"{data_type} are not the same!")
159+
except AssertionError as exc:
160+
raise AssertionError(f"{data_type} are not the same!") from exc
161161

162162

163163
class RegressionTester:
@@ -512,7 +512,7 @@ def check_violation_reporting(
512512
viol_data["limit"] = []
513513
viol_data["obsids"] = []
514514
load_year = "20%s" % load_week[-3:-1]
515-
next_year = f"{int(load_year)+1}"
515+
next_year = f"{int(load_year) + 1}"
516516
self.run_model(
517517
load_week,
518518
run_start=viol_data["run_start"],
@@ -547,18 +547,18 @@ def check_violation_reporting(
547547
viol_data["obsids"].append(obsid)
548548
else:
549549
try:
550-
assert viol_data["datestarts"][i] in line
550+
assert viol_data["datestarts"][i] in line # noqa: S101
551551
assert viol_data["datestops"][i] in line
552552
assert viol_data["duration"][i] in line
553553
assert viol_data["temps"][i] in line
554554
if self.msid == "fptemp":
555555
assert viol_data["obsids"][i] in line
556556
assert viol_data["exposure"][i] in line
557557
assert viol_data["limit"][i] in line
558-
except AssertionError:
558+
except AssertionError as exc:
559559
raise AssertionError(
560560
"Comparison failed. Check file at %s." % index_rst,
561-
)
561+
) from exc
562562
i += 1
563563
if answer_store:
564564
with open(viol_json, "w") as f:
@@ -590,9 +590,9 @@ def check_acis_obsids(self, load_week, obsid_json, answer_store=False):
590590
obsid_data_stored = json.load(f)
591591
try:
592592
assert_array_equal(obsid_data, obsid_data_stored)
593-
except AssertionError:
593+
except AssertionError as exc:
594594
outlines = "Some entries did not match:\n"
595595
for o1, o2 in zip(obsid_data, obsid_data_stored):
596596
if not np.all(o1 == o2):
597597
outlines += f"Ref: {o1}\nTest: {o2}\n\n"
598-
raise AssertionError(outlines)
598+
raise AssertionError(outlines) from exc

pyproject.toml

Lines changed: 0 additions & 135 deletions
This file was deleted.

ruff-base.toml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
target-version = "py312"
2+
3+
# fix = true
4+
lint.unfixable = []
5+
6+
lint.select = [
7+
"I", # isort
8+
"F", # pyflakes
9+
"E", "W", # pycodestyle
10+
"YTT", # flake8-2020
11+
"B", # flake8-bugbear
12+
"Q", # flake8-quotes
13+
"T10", # flake8-debugger
14+
"INT", # flake8-gettext
15+
"PLC", "PLE", "PLR", "PLW", # pylint
16+
"PIE", # misc lints
17+
"PYI", # flake8-pyi
18+
"TID", # tidy imports
19+
"ISC", # implicit string concatenation
20+
"TCH", # type-checking imports
21+
"C4", # comprehensions
22+
"PGH" # pygrep-hooks
23+
]
24+
25+
# Some additional rules that are useful
26+
lint.extend-select = [
27+
"UP009", # UTF-8 encoding declaration is unnecessary
28+
"SIM118", # Use `key in dict` instead of `key in dict.keys()`
29+
"D205", # One blank line required between summary line and description
30+
"ARG001", # Unused function argument
31+
"RSE102", # Unnecessary parentheses on raised exception
32+
"PERF401", # Use a list comprehension to create a transformed list
33+
"S101", # Use of `assert` detected
34+
]
35+
36+
lint.ignore = [
37+
"ISC001", # Disable this for compatibility with ruff format
38+
"E402", # module level import not at top of file
39+
"E731", # do not assign a lambda expression, use a def
40+
"PLR2004", # Magic number
41+
"B028", # No explicit `stacklevel` keyword argument found
42+
"PLR0913", # Too many arguments to function call
43+
"PLR1730", # Checks for if statements that can be replaced with min() or max() calls
44+
"PLC0415", # `import` should be at the top-level of a file
45+
"PLW1641", # Class implements `__hash__` if `__eq__` is implemented,
46+
]
47+
48+
extend-exclude = [
49+
"doc",
50+
"build",
51+
]
52+
53+
[lint.pycodestyle]
54+
max-line-length = 100 # E501 reports lines that exceed the length of 100.
55+
56+
[lint.extend-per-file-ignores]
57+
"__init__.py" = ["E402", "F401", "F403"]
58+
# For tests:
59+
# - D205: Don't worry about test docstrings
60+
# - ARG001: Unused function argument false positives for some fixtures
61+
# - E501: Line-too-long
62+
# - S101: Do not use assert
63+
"**/tests/*test*.py" = ["D205", "ARG001", "E501", "S101"]

ruff.toml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
extend = "ruff-base.toml"
2+
3+
# These are files to exclude for this project.
4+
extend-exclude = [
5+
# "**/*.ipynb", # commonly not ruff-compliant
6+
]
7+
8+
# These are rules that commonly cause many ruff warnings. Code will be improved by
9+
# incrementally fixing code to adhere to these rules, but for practical purposes they
10+
# can be ignored by uncommenting each one. You can also add to this list as needed.
11+
lint.extend-ignore = [
12+
"D205", # 1 blank line required between summary line and description
13+
"B905", # `zip()` without an explicit `strict=` parameter
14+
# "PLC1901", # compare-to-empty-string
15+
# "PLR0911", # Too many returns
16+
"PLR0912", # Too many branches
17+
"PLR0915", # Too many statements
18+
# "PGH004", # Use specific rule codes when using `noqa`
19+
# "C401", # Unnecessary generator (rewrite as a `set` comprehension)
20+
# "C402", # Unnecessary generator (rewrite as a dict comprehension)
21+
# "C405", # Unnecessary `list` literal (rewrite as a `set` literal)
22+
# "C408", # Unnecessary `dict` call (rewrite as a literal)
23+
# "C416", # Unnecessary `dict` comprehension (rewrite using `dict()`)
24+
# "G010", # warn is deprecated in favor of warning
25+
# "PYI056", # Calling `.append()` on `__all__` may not be supported by all type checkers
26+
]

0 commit comments

Comments
 (0)