Skip to content

Commit f533434

Browse files
authored
Merge pull request #237 from altheaden/lint-with-ruff
Switch to `ruff` for linting with `pre-commit`
2 parents b3945e6 + e693234 commit f533434

File tree

13 files changed

+382
-265
lines changed

13 files changed

+382
-265
lines changed

.flake8

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

.pre-commit-config.yaml

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@ repos:
99
- id: trailing-whitespace
1010
- id: end-of-file-fixer
1111

12-
# Can run individually with `pre-commit run isort --all-files`
13-
- repo: https://github.com/PyCQA/isort
14-
rev: 6.0.0
15-
hooks:
16-
- id: isort
17-
1812
# Can run individually with `flynt [file]` or `flynt [source]`
1913
- repo: https://github.com/ikamensh/flynt
2014
rev: '1.0.1'
@@ -23,16 +17,6 @@ repos:
2317
args: ["--fail-on-change", "--verbose"]
2418
require_serial: true
2519

26-
# Can run individually with `pre-commit run flake8 --all-files`
27-
# Need to use flake8 GitHub mirror due to CentOS git issue with GitLab
28-
# https://github.com/pre-commit/pre-commit/issues/1206
29-
- repo: https://github.com/pycqa/flake8
30-
rev: 7.1.2
31-
hooks:
32-
- id: flake8
33-
args: ["--config=.flake8"]
34-
additional_dependencies: [flake8-isort]
35-
3620
# Can run individually with `pre-commit run mypy --all-files`
3721
- repo: https://github.com/pre-commit/mirrors-mypy
3822
rev: v1.15.0
@@ -42,7 +26,15 @@ repos:
4226
verbose: true
4327
additional_dependencies: ['types-requests', 'types-PyYAML']
4428

45-
# https://pre-commit.ci/#configuration
46-
ci:
47-
autofix_prs: false
48-
autoupdate_schedule: weekly
29+
- repo: https://github.com/astral-sh/ruff-pre-commit
30+
rev: v0.9.7
31+
hooks:
32+
# Sort the imports
33+
- id: ruff
34+
name: ruff-sort-imports
35+
args: [--select, I, --fix]
36+
# Run the linter.
37+
- id: ruff
38+
args: [--fix]
39+
# Run the formatter.
40+
- id: ruff-format

docs/developers_guide/quick_start.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ process until your commit is successful. Some changes need to be made manually,
6262
such as inconsistent variable types. When this happens, you must update the
6363
file to `pre-commit`'s standards, and then attempt to re-commit the file.
6464

65-
Internally, `pre-commit` uses [flake8](https://flake8.pycqa.org/en/latest/) to
66-
check PEP8 compliance, [isort](https://pycqa.github.io/isort/) to sort, check
67-
and format imports, [flynt](https://github.com/ikamensh/flynt) to change any
68-
format strings to f-strings, and [mypy](https://mypy-lang.org/) to check for
69-
consistent variable types. An example error might be:
65+
Internally, `pre-commit` uses [ruff](https://docs.astral.sh/ruff/) to check
66+
PEP8 compliance, as well as sort, check and format imports,
67+
[flynt](https://github.com/ikamensh/flynt) to change any format strings to
68+
f-strings, and [mypy](https://mypy-lang.org/) to check for consistent variable
69+
types. An example error might be:
7070

7171
```bash
7272
example.py:77:1: E302 expected 2 blank lines, found 1

mache/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
from mache.discover import discover_machine
2-
from mache.machine_info import MachineInfo
3-
from mache.version import __version__, __version_info__
1+
from mache.discover import discover_machine as discover_machine
2+
from mache.machine_info import MachineInfo as MachineInfo
3+
from mache.version import __version__ as __version__
4+
from mache.version import __version_info__ as __version_info__

mache/__main__.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,24 @@ def main():
1111
"""
1212

1313
parser = argparse.ArgumentParser(
14-
description="Perform mache commands",
15-
usage='''
14+
description='Perform mache commands',
15+
usage="""
1616
mache <command> [<args>]
1717
The available mache commands are:
1818
sync Sync files between supported machines
1919
To get help on an individual command, run:
2020
mache <command> --help
21-
''')
21+
""",
22+
)
2223

2324
parser.add_argument('command', help='command to run')
24-
parser.add_argument('-v', '--version',
25-
action='version',
26-
version=f'mache {mache.version.__version__}',
27-
help="Show version number and exit")
25+
parser.add_argument(
26+
'-v',
27+
'--version',
28+
action='version',
29+
version=f'mache {mache.version.__version__}',
30+
help='Show version number and exit',
31+
)
2832
if len(sys.argv) == 1:
2933
parser.print_help()
3034
sys.exit(0)

mache/discover.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,16 @@ def discover_machine(quiet=False):
5959
'either delete or \n'
6060
'edit that file so it no longer defines $NERSC_HOST, log out, '
6161
'log back in, \n'
62-
'and try again.')
62+
'and try again.'
63+
)
6364

6465
# As a last resort (e.g. on a compute node), try getting the machine from
6566
# a file created on install
6667
if machine is None and 'CONDA_PREFIX' in os.environ:
6768
prefix = os.environ['CONDA_PREFIX']
6869
machine_filename = os.path.join(
69-
prefix, 'share', 'mache', 'machine.txt')
70+
prefix, 'share', 'mache', 'machine.txt'
71+
)
7072
if os.path.exists(machine_filename):
7173
with open(machine_filename) as fp:
7274
machine = fp.read().replace('\n', '').strip()

mache/machine_info.py

Lines changed: 50 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -114,69 +114,57 @@ def __str__(self):
114114
The contents as a string for printing to the terminal
115115
"""
116116

117-
info = \
118-
f'Machine: {self.machine}\n' \
117+
info = (
118+
f'Machine: {self.machine}\n'
119119
f' E3SM Supported Machine: {self.e3sm_supported}'
120-
121-
if self.e3sm_supported and self.compilers is not None and \
122-
self.mpilibs is not None and self.os is not None:
123-
info = \
124-
f'{info}\n' \
125-
f' Compilers: {", ".join(self.compilers)}\n' \
126-
f' MPI libraries: {", ".join(self.mpilibs)}\n' \
120+
)
121+
122+
if (
123+
self.e3sm_supported
124+
and self.compilers is not None
125+
and self.mpilibs is not None
126+
and self.os is not None
127+
):
128+
info = (
129+
f'{info}\n'
130+
f' Compilers: {", ".join(self.compilers)}\n'
131+
f' MPI libraries: {", ".join(self.mpilibs)}\n'
127132
f' OS: {self.os}'
133+
)
128134

129135
info = f'{info}\n'
130136

131-
print_unified = (self.e3sm_unified_activation is not None or
132-
self.e3sm_unified_base is not None or
133-
self.e3sm_unified_mpi is not None)
137+
print_unified = (
138+
self.e3sm_unified_activation is not None
139+
or self.e3sm_unified_base is not None
140+
or self.e3sm_unified_mpi is not None
141+
)
134142
if print_unified:
135-
info = \
136-
f'{info}\n' \
137-
f'E3SM-Unified: '
143+
info = f'{info}\nE3SM-Unified: '
138144

139145
if self.e3sm_unified_activation is None:
140-
info = \
141-
f'{info}\n' \
142-
f' E3SM-Unified is not currently loaded'
146+
info = f'{info}\n E3SM-Unified is not currently loaded'
143147
else:
144-
info = \
145-
f'{info}\n' \
146-
f' Activation: {self.e3sm_unified_activation}'
148+
info = f'{info}\n Activation: {self.e3sm_unified_activation}'
147149
if self.e3sm_unified_base is not None:
148-
info = \
149-
f'{info}\n' \
150-
f' Base path: {self.e3sm_unified_base}'
150+
info = f'{info}\n Base path: {self.e3sm_unified_base}'
151151
if self.e3sm_unified_mpi is not None:
152-
info = \
153-
f'{info}\n' \
154-
f' MPI type: {self.e3sm_unified_mpi}'
152+
info = f'{info}\n MPI type: {self.e3sm_unified_mpi}'
155153
info = f'{info}\n'
156154

157155
print_diags = self.diagnostics_base is not None
158156
if print_diags:
159-
info = \
160-
f'{info}\n' \
161-
f'Diagnostics: '
157+
info = f'{info}\nDiagnostics: '
162158

163159
if self.diagnostics_base is not None:
164-
info = \
165-
f'{info}\n' \
166-
f' Base path: {self.diagnostics_base}'
160+
info = f'{info}\n Base path: {self.diagnostics_base}'
167161
info = f'{info}\n'
168162

169-
info = \
170-
f'{info}\n' \
171-
f'Config options: '
163+
info = f'{info}\nConfig options: '
172164
for section in self.config.sections():
173-
info = \
174-
f'{info}\n' \
175-
f' [{section}]'
165+
info = f'{info}\n [{section}]'
176166
for key, value in self.config.items(section):
177-
info = \
178-
f'{info}\n' \
179-
f' {key} = {value}'
167+
info = f'{info}\n {key} = {value}'
180168
info = f'{info}\n'
181169
return info
182170

@@ -232,30 +220,35 @@ def get_account_defaults(self):
232220
return account, partition, constraint, qos
233221

234222
def _get_config(self):
235-
""" get a parser for config options """
223+
"""get a parser for config options"""
236224

237225
config = configparser.ConfigParser(
238-
interpolation=configparser.ExtendedInterpolation())
226+
interpolation=configparser.ExtendedInterpolation()
227+
)
239228

240229
machine = self.machine
241230
try:
242-
cfg_path = \
231+
cfg_path = (
243232
importlib_resources.files('mache.machines') / f'{machine}.cfg'
233+
)
244234
config.read(str(cfg_path))
245235
except FileNotFoundError:
246236
# this isn't a known machine so use the default
247-
cfg_path = \
237+
cfg_path = (
248238
importlib_resources.files('mache.machines') / 'default.cfg'
239+
)
249240
config.read(str(cfg_path))
250241

251242
return config
252243

253244
def _parse_compilers_and_mpi(self):
254-
""" Parse the compilers and mpi modules from XML config files """
245+
"""Parse the compilers and mpi modules from XML config files"""
255246
machine = self.machine
256247

257-
xml_path = (importlib_resources.files('mache.cime_machine_config') /
258-
'config_machines.xml')
248+
xml_path = (
249+
importlib_resources.files('mache.cime_machine_config')
250+
/ 'config_machines.xml'
251+
)
259252

260253
root = etree.parse(str(xml_path))
261254

@@ -299,11 +292,12 @@ def _parse_compilers_and_mpi(self):
299292
self.os = machine_os
300293

301294
def _detect_e3sm_unified(self):
302-
""" Read E3SM-Unified base path and detect whether it is running """
295+
"""Read E3SM-Unified base path and detect whether it is running"""
303296
config = self.config
304297

305-
if config is not None and \
306-
config.has_option('e3sm_unified', 'base_path'):
298+
if config is not None and config.has_option(
299+
'e3sm_unified', 'base_path'
300+
):
307301
self.e3sm_unified_base = config.get('e3sm_unified', 'base_path')
308302

309303
if 'E3SMU_SCRIPT' in os.environ:
@@ -313,10 +307,11 @@ def _detect_e3sm_unified(self):
313307
self.e3sm_unified_mpi = os.environ['E3SMU_MPI'].lower()
314308

315309
def _get_diagnostics_info(self):
316-
""" Get config options related to diagnostics data """
310+
"""Get config options related to diagnostics data"""
317311

318312
config = self.config
319313

320-
if config is not None and \
321-
config.has_option('diagnostics', 'base_path'):
314+
if config is not None and config.has_option(
315+
'diagnostics', 'base_path'
316+
):
322317
self.diagnostics_base = config.get('diagnostics', 'base_path')

0 commit comments

Comments
 (0)