Skip to content

Commit 3b0576b

Browse files
authored
Migrate to ruff (#475)
* Switched from flakeheaven to ruff as linter * Changed docs to now use ruff instead of flakeheaven * Skip other jobs in CI (revert once change is finished) * Corrected linting error about not chaning exceptions (B904) * Corrected linting error about assigning names to lambdas (E731) * Corrected issue with true-fals-comparisons * Corrected tests if not member (E713) * Corrected accessing attributes without assigning them (B018) * Readded all Tests from CI * Corrected empty lines with whitespaces (W293) * Removed trailing whitespaces (W291) * Corrected comparison to None (E711) * Forgot to add linebreaks when corrected lambdas
1 parent 453d024 commit 3b0576b

File tree

23 files changed

+81
-73
lines changed

23 files changed

+81
-73
lines changed

.github/workflows/ci_pipeline.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ jobs:
2525
with:
2626
environment-file: ${{ env.YML }}
2727
create-args: >-
28-
python=3.10
28+
python=3.12
2929
- name: Code reformatting with black
3030
run: |
3131
black pySDC --check --diff --color
32-
- name: Linting with flakeheaven
32+
- name: Linting with ruff
3333
run: |
34-
flakeheaven lint --benchmark pySDC
34+
ruff check pySDC
3535
3636
user_cpu_tests_linux:
3737
runs-on: ubuntu-latest

docs/contrib/02_continuous_integration.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ Finally, the CI also build artifacts that are used to generate the documentation
66

77
## Code linting
88

9-
Code style linting is performed using [black](https://black.readthedocs.io/en/stable/) and [flakeheaven](https://flakeheaven.readthedocs.io/en/latest/) for code syntax checking. In particular, `black` is used to check compliance with (most of) [PEP-8 guidelines](https://peps.python.org/pep-0008/).
9+
Code style linting is performed using [black](https://black.readthedocs.io/en/stable/) and [ruff](https://docs.astral.sh/ruff/) for code syntax checking. In particular, `black` is used to check compliance with (most of) [PEP-8 guidelines](https://peps.python.org/pep-0008/).
1010

1111
Those tests are conducted for each commit (even for forks), but you can also run it locally in the root folder of `pySDC` before pushing any commit :
1212

1313
```bash
1414
# Install required packages (works also with conda/mamba)
15-
pip install black flakeheaven flake8-comprehensions flake8-bugbear
15+
pip install black ruff
1616
# First : test code style linting with black
1717
black pySDC --check --diff --color
18-
# Second : test code syntax with flakeheaven
19-
flakeheaven lint --benchmark pySDC
18+
# Second : test code syntax with ruff
19+
ruff check pySDC
2020
```
2121

2222
> :bell: To avoid any error about formatting (`black`), you can simply use this program to reformat your code directly using the command :
@@ -59,7 +59,7 @@ fi
5959
You may need to run `chmod +x` on the file to allow it to be executed.
6060
Be aware that the hook will alter files you may have opened in an editor whenever you make a commit, which may confuse you(r editor).
6161

62-
To automate flakeheaven, we want to write a hook that alters the commit message in case any errors are detected. This gives us the choice of aborting the commit and fixing the issues, or we can go ahead and commit them and worry about flakeheaven only when the time comes to do a pull request.
62+
To automate ruff, we want to write a hook that alters the commit message in case any errors are detected. This gives us the choice of aborting the commit and fixing the issues, or we can go ahead and commit them and worry about ruff only when the time comes to do a pull request.
6363
To obtain this functionality, add the following to `<pySDC-root-directory>/.git/hooks/prepare-commit-msg`:
6464

6565
```bash
@@ -71,11 +71,11 @@ export files=$(git diff --staged --name-only HEAD | grep .py | sed -e "s,^,$(git
7171

7272
if [[ $files != "" ]]
7373
then
74-
export flakeheaven_output=$(flakeheaven lint --format default $files)
75-
if [[ "$flakeheaven_output" != 0 ]]
74+
export ruff_output=$(ruff check --quiet $files)
75+
if [[ "$ruff_output" != 0 ]]
7676
then
77-
git interpret-trailers --in-place --trailer "$(echo "$flakeheaven_output" | sed -e 's/^/#/')" "$COMMIT_MSG_FILE"
78-
git interpret-trailers --in-place --trailer "#!!!!!!!!!! WARNING: FLAKEHEAVEN FAILED !!!!!!!!!!" "$COMMIT_MSG_FILE"
77+
git interpret-trailers --in-place --trailer "$(echo "$ruff_output" | sed -e 's/^/#/')" "$COMMIT_MSG_FILE"
78+
git interpret-trailers --in-place --trailer "#!!!!!!!!!! WARNING: RUFF FAILED !!!!!!!!!!" "$COMMIT_MSG_FILE"
7979
fi
8080
fi
8181

etc/environment-lint.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,4 @@ channels:
66
- defaults
77
dependencies:
88
- black
9-
- flakeheaven
10-
- flake8-comprehensions
11-
- flake8-bugbear
9+
- ruff

pySDC/core/collocation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def __init__(self, num_nodes=None, tleft=0, tright=1, node_type='LEGENDRE', quad
6565
nNodes=num_nodes, nodeType=node_type, quadType=quad_type, tLeft=tleft, tRight=tright
6666
)
6767
except Exception as e:
68-
raise CollocationError(f"could not instantiate qmat generator, got error: {e}")
68+
raise CollocationError(f"could not instantiate qmat generator, got error: {e}") from e
6969

7070
# Set base attributes
7171
self.num_nodes = num_nodes

pySDC/core/common.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ def _makeAttributeAndRegister(self, *names, localVars=None, readOnly=False):
5656
for name in names:
5757
try:
5858
super().__setattr__(name, localVars[name])
59-
except KeyError: # pragma: no cover
60-
raise ValueError(f'value for {name} not given in localVars')
59+
except KeyError as e: # pragma: no cover
60+
raise ValueError(f'value for {name} not given in localVars') from e
6161
# Register as class parameter
6262
if readOnly:
6363
self._parNamesReadOnly = self._parNamesReadOnly.union(names)

pySDC/core/sweeper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def setupGenerator(self, qd_type):
104104
tLeft=coll.tleft,
105105
)
106106
except Exception as e:
107-
raise ValueError(f"could not generate {qd_type=!r} with qmat, got error : {e}")
107+
raise ValueError(f"could not generate {qd_type=!r} with qmat, got error : {e}") from e
108108

109109
def get_Qdelta_implicit(self, qd_type, k=None):
110110
QDmat = np.zeros_like(self.coll.Qmat)

pySDC/implementations/hooks/log_solution.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class LogToFile(Hooks):
8484
index associated with individual files by giving a different lambda function ``format_index`` class attribute. This
8585
lambda should accept one index and return one string.
8686
87-
You can also give a custom ``logging_condition`` lambda, accepting the current level if you want to log selectively.
87+
You can also give a custom ``logging_condition`` function, accepting the current level if you want to log selectively.
8888
8989
Importantly, you may need to change ``process_solution``. By default, this will return a numpy view of the solution.
9090
Of course, if you are not using numpy, you need to change this. Again, this is a lambda accepting the level.
@@ -99,9 +99,15 @@ class LogToFile(Hooks):
9999

100100
path = None
101101
file_name = 'solution'
102-
logging_condition = lambda L: True
103-
process_solution = lambda L: {'t': L.time + L.dt, 'u': L.uend.view(np.ndarray)}
104-
format_index = lambda index: f'{index:06d}'
102+
103+
def logging_condition(L):
104+
return True
105+
106+
def process_solution(L):
107+
return {'t': L.time + L.dt, 'u': L.uend.view(np.ndarray)}
108+
109+
def format_index(index):
110+
return f'{index:06d}'
105111

106112
def __init__(self):
107113
super().__init__()

pySDC/implementations/problem_classes/Battery.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def eval_f(self, u, t):
163163
else:
164164
# proof all switching conditions and find largest index where it drops below V_ref
165165
switch = [True if u[k] <= self.V_ref[k - 1] else False for k in range(1, len(u))]
166-
max_index = max([k if switch[k] == True else -1 for k in range(len(switch))])
166+
max_index = max([k if switch[k] else -1 for k in range(len(switch))])
167167

168168
if max_index == -1:
169169
f.expl[:] = self.switch_f[0]
@@ -200,7 +200,7 @@ def solve_system(self, rhs, factor, u0, t):
200200
else:
201201
# proof all switching conditions and find largest index where it drops below V_ref
202202
switch = [True if rhs[k] <= self.V_ref[k - 1] else False for k in range(1, len(rhs))]
203-
max_index = max([k if switch[k] == True else -1 for k in range(len(switch))])
203+
max_index = max([k if switch[k] else -1 for k in range(len(switch))])
204204
if max_index == -1:
205205
self.A = self.switch_A[0]
206206

pySDC/implementations/problem_classes/HeatEquation_ND_FD_CuPy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ def __init__(
8686
"""Initialization routine"""
8787

8888
# make sure parameters have the correct types
89-
if not type(nvars) in [int, tuple]:
89+
if type(nvars) not in [int, tuple]:
9090
raise ProblemError('nvars should be either tuple or int')
91-
if not type(freq) in [int, tuple]:
91+
if type(freq) not in [int, tuple]:
9292
raise ProblemError('freq should be either tuple or int')
9393

9494
# transforms nvars into a tuple

pySDC/implementations/problem_classes/PenningTrap_3D.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def eval_f(self, part, t):
172172

173173
self.work_counters['rhs']()
174174
try:
175-
penningtrap.Harmonic_oscillator
175+
_unused = penningtrap.Harmonic_oscillator
176176
Emat = np.diag([0, 0, -1])
177177
except AttributeError:
178178
Emat = np.diag([1, 1, -2])

0 commit comments

Comments
 (0)