Skip to content

Commit 6f0cb1d

Browse files
authored
Merge pull request #108 from xylar/fix_mypy
Various fixes to do with linting
2 parents ce7c9f1 + 1e782a1 commit 6f0cb1d

File tree

9 files changed

+44
-32
lines changed

9 files changed

+44
-32
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ repos:
3333
- id: mypy
3434
args: ["--config=setup.cfg", "--show-error-codes"]
3535
verbose: true
36-
additional_dependencies: ['types-requests']
36+
additional_dependencies: ['types-requests', 'types-PyYAML']

mache/__init__.py

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

mache/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import sys
21
import argparse
2+
import sys
33

44
import mache.version
55
from mache import sync

mache/discover.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import os
12
import socket
23
import warnings
3-
import os
44

55

66
def discover_machine(quiet=False):

mache/machine_info.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1-
from lxml import etree
2-
try:
3-
from importlib import resources as importlib_resources
4-
except ImportError:
5-
# python<=3.8
6-
import importlib_resources
71
import configparser
82
import os
93
import pwd
4+
import sys
5+
from typing import TYPE_CHECKING
6+
7+
from lxml import etree
8+
9+
if TYPE_CHECKING or sys.version_info > (3, 8, 0):
10+
from importlib import resources as importlib_resources
11+
else:
12+
# python <= 3.8
13+
import importlib_resources
14+
1015

1116
from mache.discover import discover_machine
1217

@@ -120,7 +125,8 @@ def __str__(self):
120125
info = f'Machine: {self.machine}\n' \
121126
f' E3SM Supported Machine: {self.e3sm_supported}'
122127

123-
if self.e3sm_supported:
128+
if self.e3sm_supported and self.compilers is not None and \
129+
self.mpilibs is not None and self.os is not None:
124130
info = f'{info}\n' \
125131
f' Compilers: {", ".join(self.compilers)}\n' \
126132
f' MPI libraries: {", ".join(self.mpilibs)}\n' \
@@ -231,12 +237,12 @@ def _get_config(self):
231237
try:
232238
cfg_path = \
233239
importlib_resources.files('mache.machines') / f'{machine}.cfg'
234-
config.read(cfg_path)
240+
config.read(str(cfg_path))
235241
except FileNotFoundError:
236242
# this isn't a known machine so use the default
237243
cfg_path = \
238244
importlib_resources.files('mache.machines') / 'default.cfg'
239-
config.read(cfg_path)
245+
config.read(str(cfg_path))
240246

241247
return config
242248

mache/permissions.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import grp
12
import os
23
import stat
3-
import grp
4+
45
import progressbar
56

67

7-
def update_permissions(base_paths, group, show_progress=True,
8+
def update_permissions(base_paths, group, show_progress=True, # noqa: C901
89
group_writable=False, other_readable=True):
910
"""
1011
Update the group that a directory belongs to along with the "group" and

mache/spack/__init__.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
import os
22
import subprocess
3+
import sys
4+
from typing import TYPE_CHECKING
5+
36
from jinja2 import Template
4-
try:
7+
8+
if TYPE_CHECKING or sys.version_info > (3, 8, 0):
59
from importlib import resources as importlib_resources
6-
except ImportError:
7-
# python<=3.8
10+
else:
11+
# python <= 3.8
812
import importlib_resources
13+
914
import yaml
1015

11-
from mache.machine_info import discover_machine, MachineInfo
16+
from mache.machine_info import MachineInfo, discover_machine
1217
from mache.version import __version__
1318

1419

@@ -99,7 +104,7 @@ def make_spack_env(spack_path, env_name, spack_specs, compiler, mpi,
99104
path = \
100105
importlib_resources.files('mache.spack') / shell_filename
101106
try:
102-
with open(path) as fp:
107+
with open(str(path)) as fp:
103108
template = Template(fp.read())
104109
except FileNotFoundError:
105110
# there's nothing to add, which is fine
@@ -112,7 +117,7 @@ def make_spack_env(spack_path, env_name, spack_specs, compiler, mpi,
112117

113118
path = \
114119
importlib_resources.files('mache.spack') / 'build_spack_env.template'
115-
with open(path) as fp:
120+
with open(str(path)) as fp:
116121
template = Template(fp.read())
117122
if tmpdir is not None:
118123
modules = f'{modules}\n' \
@@ -219,7 +224,7 @@ def get_spack_script(spack_path, env_name, compiler, mpi, shell, machine=None,
219224
path = \
220225
importlib_resources.files('mache.spack') / shell_filename
221226
try:
222-
with open(path) as fp:
227+
with open(str(path)) as fp:
223228
template = Template(fp.read())
224229
except FileNotFoundError:
225230
# there's nothing to add, which is fine
@@ -321,7 +326,7 @@ def get_modules_env_vars_and_mpi_compilers(machine, compiler, mpi, shell,
321326
path = \
322327
importlib_resources.files('mache.spack') / shell_filename
323328
try:
324-
with open(path) as fp:
329+
with open(str(path)) as fp:
325330
template = Template(fp.read())
326331
except FileNotFoundError:
327332
# there's nothing to add, which is fine
@@ -345,7 +350,7 @@ def _get_yaml_data(machine, compiler, mpi, include_e3sm_lapack,
345350
path = \
346351
importlib_resources.files('mache.spack') / template_filename
347352
try:
348-
with open(path) as fp:
353+
with open(str(path)) as fp:
349354
template = Template(fp.read())
350355
except FileNotFoundError:
351356
raise ValueError(f'Spack template not available for {compiler} '
@@ -373,9 +378,9 @@ def _get_modules(yaml_string):
373378
for mod in item['modules']:
374379
mods.append(f'module load {mod}')
375380

376-
mods = '\n'.join(mods)
381+
mods_str = '\n'.join(mods)
377382

378-
return mods
383+
return mods_str
379384

380385

381386
def _get_mpi_compilers(machine, compiler, mpi, cray_compilers):

mache/sync/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import sys
21
import argparse
2+
import sys
33

44
from mache.sync import diags
55

6+
67
def main():
78
"""
89
Defines the ``mache sync`` command
@@ -33,4 +34,4 @@ def main():
3334
exit(1)
3435

3536
# call the function associated with the requested command
36-
commands[args.command]()
37+
commands[args.command]()

mache/sync/diags.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import sys
21
import argparse
32
import subprocess
3+
import sys
44

55
from mache.machine_info import MachineInfo
66
from mache.permissions import update_permissions
77

88

9-
def sync_diags(other, direction='to', machine=None, username=None,
10-
config_filename=None):
9+
def sync_diags(other, direction='to', machine=None, # noqa: C901
10+
username=None, config_filename=None):
1111
"""
1212
Synchronize diagnostics files between supported machines
1313

0 commit comments

Comments
 (0)