Skip to content

Commit

Permalink
Add RUF (ruff specific rules) to linting rules
Browse files Browse the repository at this point in the history
  • Loading branch information
larsevj committed Sep 13, 2024
1 parent d8b17e4 commit 7ec4753
Show file tree
Hide file tree
Showing 72 changed files with 186 additions and 181 deletions.
7 changes: 6 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ select = [
"NPY", # numpy specific rules
"C4", # flake8-comprehensions
"ASYNC", # flake8-async
"RUF", # ruff specific rules
]
preview = true
ignore = ["PLW2901", # redefined-loop-name
Expand All @@ -187,9 +188,13 @@ ignore = ["PLW2901", # redefined-loop-name

[tool.ruff.lint.extend-per-file-ignores]
"tests/*" = [
"PLW0603" # global-statement
"PLW0603", # global-statement
"RUF029", # unused-async
"RUF018", # assignment-in-assert
"RUF006", # asyncio-dangling-task
]
"src/ert/dark_storage/json_schema/__init__.py" = ["F401"]
"src/ert/dark_storage/*" = ["RUF029"] # unused-async

[tool.ruff.lint.pylint]
max-args = 20
2 changes: 1 addition & 1 deletion src/_ert/threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def _raise_on_main_thread(exception: BaseException) -> None:


def _handler(signum: int, frametype: FrameType | None) -> None:
global _current_exception # noqa: PLW0603
global _current_exception
if not _current_exception:
return
current_exception, _current_exception = _current_exception, None
Expand Down
6 changes: 3 additions & 3 deletions src/_ert_forward_model_runner/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ async def _send(self, msg: AnyStr) -> None:
except ConnectionClosedOK as exception:
_error_msg = (
f"Connection closed received from the server {self.url}! "
f" Exception from {type(exception)}: {str(exception)}"
f" Exception from {type(exception)}: {exception!s}"
)
raise ClientConnectionClosedOK(_error_msg) from exception
except (
Expand All @@ -112,15 +112,15 @@ async def _send(self, msg: AnyStr) -> None:
f"Not able to establish the "
f"websocket connection {self.url}! Max retries reached!"
" Check for firewall issues."
f" Exception from {type(exception)}: {str(exception)}"
f" Exception from {type(exception)}: {exception!s}"
)
raise ClientConnectionError(_error_msg) from exception
except ConnectionClosedError as exception:
if retry == self._max_retries:
_error_msg = (
f"Not been able to send the event"
f" to {self.url}! Max retries reached!"
f" Exception from {type(exception)}: {str(exception)}"
f" Exception from {type(exception)}: {exception!s}"
)
raise ClientConnectionError(_error_msg) from exception
await asyncio.sleep(0.2 + self._timeout_multiplier * retry)
Expand Down
2 changes: 1 addition & 1 deletion src/_ert_forward_model_runner/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def __init__(self, job_data, index, sleep_interval=1):
self.std_err = job_data.get("stderr")
self.std_out = job_data.get("stdout")

def run(self): # noqa: PLR0912, PLR0915
def run(self):
start_message = Start(self)

errors = self._check_job_files()
Expand Down
4 changes: 1 addition & 3 deletions src/ert/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,7 @@ def run_cli(args: Namespace, plugin_manager: Optional[ErtPluginManager] = None)
with contextlib.ExitStack() as exit_stack:
out: TextIO
if args.disable_monitoring:
out = exit_stack.enter_context(
open(os.devnull, "w", encoding="utf-8") # noqa
)
out = exit_stack.enter_context(open(os.devnull, "w", encoding="utf-8"))
else:
out = sys.stderr
monitor = Monitor(out=out, color_always=args.color_always)
Expand Down
13 changes: 1 addition & 12 deletions src/ert/config/_read_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,18 +334,7 @@ def _read_spec(

with open(spec, mode) as fp:
for entry in resfo.lazy_read(fp, format):
if all(
p is not None
for p in (
[
date,
n,
nx,
ny,
]
+ list(arrays.values())
)
):
if all(p is not None for p in [date, n, nx, ny, *arrays.values()]):
break
kw = entry.read_keyword()
if kw in arrays:
Expand Down
6 changes: 3 additions & 3 deletions src/ert/config/analysis_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ class IESSettings(BaseSettings):
] = "subspace_exact"
ies_max_steplength: Annotated[
float,
Field(ge=0.1, le=1.0, title="GaussNewton maximum steplength"),
Field(ge=0.1, le=1.0, title="Gauss-Newton maximum steplength"),
] = DEFAULT_IES_MAX_STEPLENGTH
ies_min_steplength: Annotated[
float,
Field(ge=0.1, le=1.0, title="GaussNewton minimum steplength"),
Field(ge=0.1, le=1.0, title="Gauss-Newton minimum steplength"),
] = DEFAULT_IES_MIN_STEPLENGTH
ies_dec_steplength: Annotated[
float,
Field(ge=1.1, le=10.0, title="GaussNewton steplength decline"),
Field(ge=1.1, le=10.0, title="Gauss-Newton steplength decline"),
] = DEFAULT_IES_DEC_STEPLENGTH


Expand Down
4 changes: 2 additions & 2 deletions src/ert/config/ert_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ def _create_list_of_forward_model_steps_to_run(
except ForwardModelStepValidationError as err:
errors.append(
ConfigValidationError.with_context(
f"Forward model step pre-experiment validation failed: {str(err)}",
f"Forward model step pre-experiment validation failed: {err!s}",
context=fm_step.name,
),
)
Expand Down Expand Up @@ -665,7 +665,7 @@ def handle_default(fm_step: ForwardModelStep, arg: str) -> str:
job_list_errors.append(
ErrorInfo(
message=f"Validation failed for "
f"forward model step {fm_step.name}: {str(exc)}"
f"forward model step {fm_step.name}: {exc!s}"
).set_context(fm_step.name)
)

Expand Down
4 changes: 2 additions & 2 deletions src/ert/config/ert_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ class CancelPluginException(Exception):


class ErtPlugin(ErtScript, ABC):
def getArguments(self, args: List[Any]) -> List[Any]: # noqa: PLR6301
def getArguments(self, args: List[Any]) -> List[Any]:
return []

def getName(self) -> str:
return str(self.__class__)

def getDescription(self) -> str: # noqa: PLR6301
def getDescription(self) -> str:
return "No description provided!"
2 changes: 1 addition & 1 deletion src/ert/config/ert_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def initializeAndRun(
return uw.args[0]
except Exception as e:
full_trace = "".join(traceback.format_exception(*sys.exc_info()))
self.output_stack_trace(f"{str(e)}\n{full_trace}")
self.output_stack_trace(f"{e!s}\n{full_trace}")
return None
finally:
self.cleanup()
Expand Down
3 changes: 2 additions & 1 deletion src/ert/config/forward_model_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from abc import abstractmethod
from dataclasses import dataclass, field
from typing import (
ClassVar,
Dict,
List,
Literal,
Expand Down Expand Up @@ -162,7 +163,7 @@ class ForwardModelStep:
private_args: SubstitutionList = field(default_factory=SubstitutionList)
help_text: str = ""

default_env = {
default_env: ClassVar[set[str]] = {

Check failure on line 166 in src/ert/config/forward_model_step.py

View workflow job for this annotation

GitHub Actions / type-checking (3.12)

Incompatible types in assignment (expression has type "dict[str, str]", variable has type "set[str]")
"_ERT_ITERATION_NUMBER": "<ITER>",
"_ERT_REALIZATION_NUMBER": "<IENS>",
"_ERT_RUNPATH": "<RUNPATH>",
Expand Down
2 changes: 1 addition & 1 deletion src/ert/config/gen_data_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def _read_file(filename: Path, report_step: int) -> xr.Dataset:
errors.append(str(err))
else:
for report_step in report_steps:
filename = input_file % report_step # noqa
filename = input_file % report_step
try:
datasets_per_report_step.append(
_read_file(_run_path / filename, report_step)
Expand Down
2 changes: 1 addition & 1 deletion src/ert/config/gen_kw_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ def transform(self, array: npt.ArrayLike) -> npt.NDArray[np.float64]:
def _values_from_file(
realization: int, name_format: str, keys: List[str]
) -> npt.NDArray[np.double]:
file_name = name_format % realization # noqa
file_name = name_format % realization
df = pd.read_csv(file_name, sep=r"\s+", header=None)
# This means we have a key: value mapping in the
# file otherwise it is just a list of values
Expand Down
2 changes: 1 addition & 1 deletion src/ert/config/observations.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

def history_key(key: str) -> str:
keyword, *rest = key.split(":")
return ":".join([keyword + "H"] + rest)
return ":".join([keyword + "H", *rest])


@dataclass
Expand Down
2 changes: 1 addition & 1 deletion src/ert/config/refcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def __eq__(self, other: object) -> bool:

@property
def all_dates(self) -> List[datetime]:
return [self.start_date] + list(self.dates)
return [self.start_date, *self.dates]

@classmethod
def from_config_dict(cls, config_dict: ConfigDict) -> Optional["Refcase"]:
Expand Down
2 changes: 1 addition & 1 deletion src/ert/config/workflow_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def __post_init__(self) -> None:

# Bare Exception here as we have no control
# of exceptions in the loaded ErtScript
except Exception as err: # noqa
except Exception as err:
raise ErtScriptLoadFailure(
f"Failed to load {self.name}: {err}"
) from err
Expand Down
4 changes: 2 additions & 2 deletions src/ert/dark_storage/enkf.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@


def get_storage() -> Storage:
global _storage # noqa: PLW0603e
global _storage # e
if _storage is None:
try:
return (_storage := open_storage(os.environ["ERT_STORAGE_ENS_PATH"]))
except RuntimeError as err:
raise InternalServerError(f"{str(err)}") from err
raise InternalServerError(f"{err!s}") from err
_storage.refresh()
return _storage
4 changes: 2 additions & 2 deletions src/ert/data/_measured_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def remove_failed_realizations(self) -> None:
standard deviations as-is."""
pre_index = self.data.index
post_index = list(self.data.dropna(axis=0, how="all").index)
drop_index = set(pre_index) - set(post_index + ["STD", "OBS"])
drop_index = set(pre_index) - {*post_index, "STD", "OBS"}
self._set_data(self.data.drop(index=drop_index))

def get_simulated_data(self) -> pd.DataFrame:
Expand Down Expand Up @@ -157,7 +157,7 @@ def _get_data(
measured_data.append(
pd.DataFrame(
data,
index=("OBS", "STD") + tuple(ds.realization.values),
index=("OBS", "STD", *tuple(ds.realization.values)),
columns=pd.MultiIndex.from_tuples(
index_vals,
names=[None, "key_index", "data_index"],
Expand Down
2 changes: 1 addition & 1 deletion src/ert/ensemble_evaluator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
"EndEvent",
"Ensemble",
"EnsembleEvaluator",
"EnsembleSnapshot",
"EvaluatorServerConfig",
"FMStepSnapshot",
"FullSnapshotEvent",
"Monitor",
"Realization",
"RealizationSnapshot",
"EnsembleSnapshot",
"SnapshotUpdateEvent",
"wait_for_evaluator",
]
2 changes: 1 addition & 1 deletion src/ert/ensemble_evaluator/_ensemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def update_snapshot(self, events: Sequence[Event]) -> EnsembleSnapshot:
self.status = self._status_tracker.update_state(self.snapshot.status)
return snapshot_mutate_event

async def send_event( # noqa: PLR6301
async def send_event(
self,
url: str,
event: Event,
Expand Down
2 changes: 1 addition & 1 deletion src/ert/ensemble_evaluator/evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ async def handle_dispatch(self, websocket: WebSocketServerProtocol) -> None:
# * job being killed due to MAX_RUNTIME
# * job being killed by user
logger.error(
f"a dispatcher abruptly closed a websocket: {str(connection_error)}"
f"a dispatcher abruptly closed a websocket: {connection_error!s}"
)

async def forward_checksum(self, event: Event) -> None:
Expand Down
4 changes: 2 additions & 2 deletions src/ert/ensemble_evaluator/snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def get_real(self, real_id: RealId) -> "RealizationSnapshot":
return self._realization_snapshots[real_id]

def get_fm_step(self, real_id: RealId, fm_step_id: FmStepId) -> "FMStepSnapshot":
return self._fm_step_snapshots[(real_id, fm_step_id)].copy()
return self._fm_step_snapshots[real_id, fm_step_id].copy()

def get_successful_realizations(self) -> typing.List[int]:
return [
Expand Down Expand Up @@ -373,7 +373,7 @@ def update_fm_step(
fm_step_id: str,
fm_step: "FMStepSnapshot",
) -> "EnsembleSnapshot":
self._fm_step_snapshots[(real_id, fm_step_id)].update(fm_step)
self._fm_step_snapshots[real_id, fm_step_id].update(fm_step)
return self


Expand Down
12 changes: 6 additions & 6 deletions src/ert/gui/ertwidgets/analysismodulevariablespanel.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ def __init__(self, analysis_module: AnalysisModule, ensemble_size: int):
analysis_module.__getattribute__(variable_name),
cast(
float,
[v for v in metadata.metadata if isinstance(v, Ge)][0].ge,
next(v for v in metadata.metadata if isinstance(v, Ge)).ge,
),
cast(
float,
[v for v in metadata.metadata if isinstance(v, Le)][0].le,
next(v for v in metadata.metadata if isinstance(v, Le)).le,
),
0.1,
),
Expand Down Expand Up @@ -92,9 +92,9 @@ def __init__(self, analysis_module: AnalysisModule, ensemble_size: int):
self.truncation_spinner = self.createDoubleSpinBox(
var_name,
analysis_module.enkf_truncation,
cast(float, [v for v in metadata.metadata if isinstance(v, Gt)][0].gt)
cast(float, next(v for v in metadata.metadata if isinstance(v, Gt)).gt)
+ 0.001,
cast(float, [v for v in metadata.metadata if isinstance(v, Le)][0].le),
cast(float, next(v for v in metadata.metadata if isinstance(v, Le)).le),
0.01,
)
self.truncation_spinner.setEnabled(False)
Expand Down Expand Up @@ -127,8 +127,8 @@ def __init__(self, analysis_module: AnalysisModule, ensemble_size: int):
self.local_spinner = self.createDoubleSpinBox(
var_name,
analysis_module.correlation_threshold(ensemble_size),
cast(float, [v for v in metadata.metadata if isinstance(v, Ge)][0].ge),
cast(float, [v for v in metadata.metadata if isinstance(v, Le)][0].le),
cast(float, next(v for v in metadata.metadata if isinstance(v, Ge)).ge),
cast(float, next(v for v in metadata.metadata if isinstance(v, Le)).le),
0.1,
)
self.local_spinner.setObjectName("localization_threshold")
Expand Down
2 changes: 1 addition & 1 deletion src/ert/gui/model/snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ def _fm_step_data(
delta = _estimate_duration(
start_time, end_time=node.data.get(ids.END_TIME)
)
tt_text = f"Start time: {str(start_time)}\nDuration: {str(delta)}"
tt_text = f"Start time: {start_time!s}\nDuration: {delta!s}"
if tt_text is not None:
return str(tt_text)

Expand Down
4 changes: 2 additions & 2 deletions src/ert/gui/plottery/plot_context.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import TYPE_CHECKING, List, Optional
from typing import TYPE_CHECKING, ClassVar, List, Optional

from ert.gui.tools.plot.plot_api import EnsembleObject

Expand All @@ -17,7 +17,7 @@ class PlotContext:
INDEX_AXIS = "INDEX"
COUNT_AXIS = "COUNT"
DENSITY_AXIS = "DENSITY"
AXIS_TYPES = [
AXIS_TYPES: ClassVar[list[str | None]] = [
UNKNOWN_AXIS,
COUNT_AXIS,
DATE_AXIS,
Expand Down
2 changes: 1 addition & 1 deletion src/ert/gui/plottery/plots/cesp.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def plotCrossEnsembleStatistics(

_addStatisticsLegends(config)

axes.set_xticks([-1] + ensemble_indexes + [len(ensemble_indexes)])
axes.set_xticks([-1, *ensemble_indexes, len(ensemble_indexes)])

rotation = 0
if len(ensemble_list) > 3:
Expand Down
2 changes: 1 addition & 1 deletion src/ert/gui/plottery/plots/distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def plotDistribution(

previous_data = data

axes.set_xticks([-1] + ensemble_indexes + [len(ensemble_indexes)])
axes.set_xticks([-1, *ensemble_indexes, len(ensemble_indexes)])

rotation = 0
if len(ensemble_list) > 3:
Expand Down
4 changes: 2 additions & 2 deletions src/ert/gui/simulation/ensemble_smoother_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ def __init__(

self.setLayout(layout)

self._ensemble_format_field.getValidationSupport().validationChanged.connect( # noqa
self._ensemble_format_field.getValidationSupport().validationChanged.connect(
self.simulationConfigurationChanged
)
self._active_realizations_field.getValidationSupport().validationChanged.connect( # noqa
self._active_realizations_field.getValidationSupport().validationChanged.connect(
self.simulationConfigurationChanged
)

Expand Down
Loading

0 comments on commit 7ec4753

Please sign in to comment.