Skip to content

Commit

Permalink
refactor (python): apply ruff check rules UP and RUF (#2076)
Browse files Browse the repository at this point in the history
This refactor applies pyupgrade (UP) and Ruff-specific rules (RUF) to the Python code base. A few manual edits are done on top of automated fixes.
  • Loading branch information
mwtoews authored Nov 27, 2024
1 parent cc7f930 commit e2eec29
Show file tree
Hide file tree
Showing 43 changed files with 203 additions and 278 deletions.
12 changes: 7 additions & 5 deletions .build_rtd_docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
on_rtd = os.environ.get("READTHEDOCS") == "True"

# -- print current directory
print("Current Directory...'{}'".format(os.path.abspath(os.getcwd())))
print(f"Current Directory...'{os.path.abspath(os.getcwd())}'")

# -- clean up doxygen files -------------------------------------------------
dox_pths = ("_mf6io",)
for dox_pth in dox_pths:
print("cleaning....{}".format(dox_pth))
print(f"cleaning....{dox_pth}")
for root, dirs, files in os.walk(dox_pth):
for name in files:
fpth = os.path.join(root, name)
Expand Down Expand Up @@ -99,7 +99,8 @@
if stdout:
print(stdout.decode("utf-8"))
if stderr:
print("Errors:\n{}".format(stderr.decode("utf-8")))
print("Errors:")
print(stderr.decode("utf-8"))

# -- copy deprecations markdown ---------------------------------------------
print("Copy the deprecations table")
Expand All @@ -120,7 +121,8 @@
if stdout:
print(stdout.decode("utf-8"))
if stderr:
print("Errors:\n{}".format(stderr.decode("utf-8")))
print("Errors:")
print(stderr.decode("utf-8"))

# -- update the doxygen version number ---------------------------------------
print("Update the Doxyfile with the latest version number")
Expand All @@ -131,7 +133,7 @@
with open("Doxyfile", "w") as fp:
for line in lines:
if tag in line:
line = '{} = "version {}"\n'.format(tag, __version__)
line = f'{tag} = "version {__version__}"\n'
fp.write(line)

# -- Project information -----------------------------------------------------
Expand Down
8 changes: 4 additions & 4 deletions .doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
src = os.path.join(src_pth, on_dir)
dst = os.path.join(".", on_dir)
if os.path.exists(dst):
print("deleting...{}".format(dst))
print(f"deleting...{dst}")
shutil.rmtree(dst)
print("copying {} -> {}".format(src, dst))
print(f"copying {src} -> {dst}")
shutil.copytree(src, dst)

# copy files
Expand All @@ -40,9 +40,9 @@
src = os.path.join(src_pth, file_name)
dst = os.path.join(".", file_name)
if os.path.exists(dst):
print("deleting...{}".format(dst))
print(f"deleting...{dst}")
os.remove(dst)
print("copying {} -> {}".format(src, dst))
print(f"copying {src} -> {dst}")
shutil.copy(src, dst)


Expand Down
3 changes: 3 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@ d56a12e0db8ff9d052b7ef9d7157506528e1a70e

# reformat Python code with line length = 88 (#2056)
f6c3a55ea72065ba1ce8540d1fd3c051696df1f1

# reformat multi-line statements (#2070)
cc7f930b961345536529942fc57db0c515a1f7fc
2 changes: 1 addition & 1 deletion .github/common/update_compat_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
ct = (
"<!-- "
+ name
+ " compat starts -->{}<!-- ".format("\n{}\n".format(table))
+ " compat starts -->{}<!-- ".format(f"\n{table}\n")
+ name
+ " compat ends -->"
)
Expand Down
4 changes: 0 additions & 4 deletions .hpc/sstat_poll.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import argparse
import pathlib as pl
import sys
import time
from subprocess import PIPE, STDOUT, Popen

Expand Down Expand Up @@ -107,9 +106,6 @@ def _run_command(
)
slurm_args = parser.parse_args()

if sys.version_info < (3, 8):
sys.exit("Python version must be 3.8 or higher.")

print(f"SLURM command: {slurm_args.command}")
print(f"JobID: {slurm_args.jobid}")

Expand Down
8 changes: 4 additions & 4 deletions autotest/common_regression.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os
import shutil
from collections.abc import Iterator
from pathlib import Path
from typing import Iterator, List, Optional, Tuple, Union
from typing import Optional, Union
from warnings import warn

COMPARE_PROGRAMS = (
Expand Down Expand Up @@ -221,8 +222,7 @@ def get_matching_files(
extensions = [extensions]

for ext in extensions:
for file in workspace.glob(f"*.{ext}"):
yield file
yield from workspace.glob(f"*.{ext}")


def get_mf6_comparison(src):
Expand Down Expand Up @@ -475,7 +475,7 @@ def get_mf6_ftypes(namefile, ftypekeys):

def get_regression_files(
workspace: os.PathLike, extensions
) -> Tuple[List[str], List[str]]:
) -> tuple[list[str], list[str]]:
if isinstance(extensions, str):
extensions = [extensions]
files = os.listdir(workspace)
Expand Down
5 changes: 2 additions & 3 deletions autotest/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import sys
from pathlib import Path
from typing import Dict
from warnings import warn

import pytest
Expand Down Expand Up @@ -54,7 +53,7 @@ def bin_path() -> Path:


@pytest.fixture(scope="session")
def targets() -> Dict[str, Path]:
def targets() -> dict[str, Path]:
"""
Target executables for tests. These include local development builds as
well as binaries 1) downloaded from GitHub and 2) rebuilt from the last
Expand All @@ -81,7 +80,7 @@ def targets() -> Dict[str, Path]:
return d


def try_get_target(targets: Dict[str, Path], name: str) -> Path:
def try_get_target(targets: dict[str, Path], name: str) -> Path:
"""Try to retrieve the path to a binary. If the binary is a development
target and can't be found, an error is raised. Otherwise (if the binary
is downloaded or rebuilt) the test is skipped. This is to allow testing
Expand Down
17 changes: 8 additions & 9 deletions autotest/framework.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import os
import shutil
from collections.abc import Iterable
from itertools import repeat
from pathlib import Path
from subprocess import PIPE, STDOUT, Popen
from traceback import format_exc
from typing import Callable, Dict, Iterable, List, Optional, Tuple, Union
from typing import Callable, Optional, Union
from warnings import warn

import flopy
Expand Down Expand Up @@ -44,7 +45,7 @@
)


def api_return(success, model_ws) -> Tuple[bool, List[str]]:
def api_return(success, model_ws) -> tuple[bool, list[str]]:
"""
parse libmf6 stdout shared object file
"""
Expand Down Expand Up @@ -74,7 +75,7 @@ def get_workspace(sim_or_model) -> Path:
raise ValueError(f"Unsupported model type: {type(sim_or_model)}")


def run_parallel(workspace, target, ncpus) -> Tuple[bool, List[str]]:
def run_parallel(workspace, target, ncpus) -> tuple[bool, list[str]]:
if not is_in_ci() and get_ostag() in ["mac"]:
oversubscribed = ["--hostfile", "localhost"]
with open(f"{workspace}/localhost", "w") as f:
Expand Down Expand Up @@ -219,7 +220,7 @@ def __init__(
self,
name: str,
workspace: Union[str, os.PathLike],
targets: Dict[str, Path],
targets: dict[str, Path],
api_func: Optional[Callable] = None,
build: Optional[Callable] = None,
check: Optional[Callable] = None,
Expand Down Expand Up @@ -358,10 +359,8 @@ def _compare_heads(
verbose=self.verbose,
)
print(
(
f"{EXTTEXT[extension]} comparison {i + 1}"
+ f"{self.name} ({os.path.basename(fpth0)})"
)
f"{EXTTEXT[extension]} comparison {i + 1}"
+ f"{self.name} ({os.path.basename(fpth0)})"
)
if not success:
return False
Expand Down Expand Up @@ -540,7 +539,7 @@ def _run_sim_or_model(
target: Union[str, os.PathLike],
xfail: bool = False,
ncpus: int = 1,
) -> Tuple[bool, List[str]]:
) -> tuple[bool, list[str]]:
"""
Run a simulation or model with FloPy.
Expand Down
4 changes: 1 addition & 3 deletions autotest/test_gwf_ats_lak01.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,9 +400,7 @@ def check_output(idx, test):
all_passed = False
msg = (
"recharge must be zero if overlying lake is "
"active. node {} qlak {} qrch {} time {}".format(
n0, qlakleak[n0], q, t
)
f"active. node {n0} qlak {qlakleak[n0]} qrch {q} time {t}"
)
print(msg)
assert all_passed, "found recharge applied to cell beneath active lake"
Expand Down
49 changes: 17 additions & 32 deletions autotest/test_gwf_ifmod_buy.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,63 +555,48 @@ def check_output(idx, test):

# compare heads
maxdiff = np.amax(abs(heads - heads_2models))
assert maxdiff < 10 * hclose_check, "Max. head diff. {} should \
be within solver tolerance (x10): {}".format(
maxdiff, 10 * hclose_check
)
assert maxdiff < 10 * hclose_check, f"Max. head diff. {maxdiff} should \
be within solver tolerance (x10): {10 * hclose_check}"

# compare spdis_x left
maxdiff = np.amax(abs(qxb[:, :, 0:5] - qxb_left))
assert maxdiff < 10 * hclose_check, "Max. diff. in spec. discharge (x) {} \
should be within solver tolerance (x10): {}".format(
maxdiff, 10 * hclose_check
)
assert maxdiff < 10 * hclose_check, f"Max. diff. in spec. discharge (x) {maxdiff} \
should be within solver tolerance (x10): {10 * hclose_check}"

# compare spdis_y left
maxdiff = np.amax(abs(qyb[:, :, 0:5] - qyb_left))
assert maxdiff < 10 * hclose_check, "Max. diff. in spec. discharge (y) {} \
should be within solver tolerance (x10): {}".format(
maxdiff, 10 * hclose_check
)
assert maxdiff < 10 * hclose_check, f"Max. diff. in spec. discharge (y) {maxdiff} \
should be within solver tolerance (x10): {10 * hclose_check}"

# compare spdis_z left
maxdiff = np.amax(abs(qzb[:, :, 0:5] - qzb_left))
assert maxdiff < 10 * hclose_check, "Max. diff. in spec. discharge (z) {} \
should be within solver tolerance (x10): {}".format(
maxdiff, 10 * hclose_check
)
assert maxdiff < 10 * hclose_check, f"Max. diff. in spec. discharge (z) {maxdiff} \
should be within solver tolerance (x10): {10 * hclose_check}"

# compare spdis_x right
maxdiff = np.amax(abs(qxb[:, :, 5:] - qxb_right))
assert maxdiff < 10 * hclose_check, "Max. diff. in spec. discharge (x) {} \
should be within solver tolerance (x10): {}".format(
maxdiff, 10 * hclose_check
)
assert maxdiff < 10 * hclose_check, f"Max. diff. in spec. discharge (x) {maxdiff} \
should be within solver tolerance (x10): {10 * hclose_check}"

# compare spdis_y right
maxdiff = np.amax(abs(qyb[:, :, 5:] - qyb_right))
assert maxdiff < 10 * hclose_check, "Max. diff. in spec. discharge (y) {} \
should be within solver tolerance (x10): {}".format(
maxdiff, 10 * hclose_check
)
assert maxdiff < 10 * hclose_check, f"Max. diff. in spec. discharge (y) {maxdiff} \
should be within solver tolerance (x10): {10 * hclose_check}"

# compare spdis_z right
maxdiff = np.amax(abs(qzb[:, :, 5:] - qzb_right))
assert maxdiff < 10 * hclose_check, "Max. diff. in spec. discharge (z) {} \
should be within solver tolerance (x10): {}".format(
maxdiff, 10 * hclose_check
)
assert maxdiff < 10 * hclose_check, f"Max. diff. in spec. discharge (z) {maxdiff} \
should be within solver tolerance (x10): {10 * hclose_check}"

# check budget error from .lst file
for mname in [mname_ref, mname_left, mname_right]:
fpth = os.path.join(test.workspace, f"{mname}.lst")
for line in open(fpth):
if line.lstrip().startswith("PERCENT"):
cumul_balance_error = float(line.split()[3])
assert (
abs(cumul_balance_error) < 0.00001
), "Cumulative balance error = {} for {}, should equal 0.0".format(
cumul_balance_error, mname
assert abs(cumul_balance_error) < 0.00001, (
f"Cumulative balance error = {cumul_balance_error} for {mname}, "
"should equal 0.0"
)


Expand Down
13 changes: 5 additions & 8 deletions autotest/test_gwf_ifmod_idomain.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,21 +350,18 @@ def check_output(idx, test):

# compare heads
maxdiff = np.amax(abs(heads - heads_2models))
assert maxdiff < 10 * hclose_check, "Max. head diff. {} should \
be within solver tolerance (x10): {}".format(
maxdiff, 10 * hclose_check
)
assert maxdiff < 10 * hclose_check, f"Max. head diff. {maxdiff} should \
be within solver tolerance (x10): {10 * hclose_check}"

# check budget error from .lst file
for mname in [mname_ref, mname_left, mname_right]:
fpth = os.path.join(test.workspace, f"{mname}.lst")
for line in open(fpth):
if line.lstrip().startswith("PERCENT"):
cumul_balance_error = float(line.split()[3])
assert (
abs(cumul_balance_error) < 0.00001
), "Cumulative balance error = {} for {}, should equal 0.0".format(
cumul_balance_error, mname
assert abs(cumul_balance_error) < 0.00001, (
f"Cumulative balance error = {cumul_balance_error} for {mname}, "
"should equal 0.0"
)


Expand Down
13 changes: 5 additions & 8 deletions autotest/test_gwf_ifmod_newton.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,21 +362,18 @@ def check_output(idx, test):

# compare heads
maxdiff = np.amax(abs(heads - heads_2models))
assert maxdiff < 10 * hclose_check, "Max. head diff. {} should \
be within solver tolerance (x10): {}".format(
maxdiff, 10 * hclose_check
)
assert maxdiff < 10 * hclose_check, f"Max. head diff. {maxdiff} should \
be within solver tolerance (x10): {10 * hclose_check}"

# check budget error from .lst file
for mname in [mname_ref, mname_left, mname_right]:
fpth = os.path.join(test.workspace, f"{mname}.lst")
for line in open(fpth):
if line.lstrip().startswith("PERCENT"):
cumul_balance_error = float(line.split()[3])
assert (
abs(cumul_balance_error) < 0.00001
), "Cumulative balance error = {} for {}, should equal 0.0".format(
cumul_balance_error, mname
assert abs(cumul_balance_error) < 0.00001, (
f"Cumulative balance error = {cumul_balance_error} for {mname}, "
"should equal 0.0"
)


Expand Down
13 changes: 5 additions & 8 deletions autotest/test_gwf_ifmod_rewet.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,21 +383,18 @@ def check_output(idx, test):

# compare heads
maxdiff = np.amax(abs(heads - heads_2models))
assert maxdiff < 10 * hclose_check, "Max. head diff. {} should \
be within solver tolerance (x10): {}".format(
maxdiff, 10 * hclose_check
)
assert maxdiff < 10 * hclose_check, f"Max. head diff. {maxdiff} should \
be within solver tolerance (x10): {10 * hclose_check}"

# check budget error from .lst file
for mname in [mname_ref, mname_left, mname_right]:
fpth = os.path.join(test.workspace, f"{mname}.lst")
for line in open(fpth):
if line.lstrip().startswith("PERCENT"):
cumul_balance_error = float(line.split()[3])
assert (
abs(cumul_balance_error) < 0.00001
), "Cumulative balance error = {} for {}, should equal 0.0".format(
cumul_balance_error, mname
assert abs(cumul_balance_error) < 0.00001, (
f"Cumulative balance error = {cumul_balance_error} for {mname}, "
"should equal 0.0"
)


Expand Down
Loading

0 comments on commit e2eec29

Please sign in to comment.