Skip to content

Commit

Permalink
Add pyupgrade UP rule to ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
larsevj committed Dec 10, 2024
1 parent 82972b0 commit 66cda31
Show file tree
Hide file tree
Showing 349 changed files with 1,929 additions and 2,107 deletions.
1 change: 0 additions & 1 deletion docs/ert/conf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
#
# Configuration file for the Sphinx documentation builder.
#
Expand Down
1 change: 0 additions & 1 deletion docs/everest/conf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
#
# Configuration file for the Sphinx documentation builder.
#
Expand Down
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ select = [
"C4", # flake8-comprehensions
"ASYNC", # flake8-async
"RUF", # ruff specific rules
"UP", # pyupgrade
]
preview = true
ignore = [
Expand All @@ -210,6 +211,8 @@ ignore = [
"PLR0904", # too-many-public-methods
"PLR1702", # too-many-nested-blocks
"PLW3201", # bad-dunder-method-name
"UP032", # f-string
"UP031", # printf-string-formatting
]

# Allow EN DASH (U+2013)
Expand All @@ -221,6 +224,7 @@ allowed-confusables = ["–"]
"RUF029", # unused-async
"RUF018", # assignment-in-assert
"RUF006", # asyncio-dangling-task
"PLW1508", # Invalid type of environment variable default
]
"src/ert/dark_storage/json_schema/__init__.py" = ["F401"]
"src/ert/dark_storage/*" = ["RUF029"] # unused-async
Expand Down
11 changes: 5 additions & 6 deletions src/_ert/async_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import asyncio
import logging
import traceback
from collections.abc import Coroutine, Generator
from contextlib import suppress
from typing import Any, Coroutine, Generator, TypeVar, Union
from typing import Any, TypeVar

logger = logging.getLogger(__name__)

Expand All @@ -29,7 +30,7 @@ def get_running_loop() -> asyncio.AbstractEventLoop:

def _create_task(
loop: asyncio.AbstractEventLoop,
coro: Union[Coroutine[Any, Any, _T], Generator[Any, None, _T]],
coro: Coroutine[Any, Any, _T] | Generator[Any, None, _T],
) -> asyncio.Task[_T]:
assert asyncio.iscoroutine(coro)
task = asyncio.Task(coro, loop=loop)
Expand All @@ -47,9 +48,7 @@ def _done_callback(task: asyncio.Task[_T_co]) -> None:
traceback.format_exception(None, exc, exc.__traceback__)
)
logger.error(
(
f"Exception in scheduler task {task.get_name()}: {exc}\n"
f"Traceback: {exc_traceback}"
)
f"Exception in scheduler task {task.get_name()}: {exc}\n"
f"Traceback: {exc_traceback}"
)
raise exc
8 changes: 4 additions & 4 deletions src/_ert/forward_model_runner/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ def _setup_reporters(
ee_token=None,
ee_cert_path=None,
experiment_id=None,
) -> typing.List[reporting.Reporter]:
reporters: typing.List[reporting.Reporter] = []
) -> list[reporting.Reporter]:
reporters: list[reporting.Reporter] = []
if is_interactive_run:
reporters.append(reporting.Interactive())
elif ens_id and experiment_id is None:
Expand Down Expand Up @@ -77,10 +77,10 @@ def _wait_for_retry():

def _read_jobs_file(retry=True):
try:
with open(JOBS_FILE, "r", encoding="utf-8") as json_file:
with open(JOBS_FILE, encoding="utf-8") as json_file:
return json.load(json_file)
except json.JSONDecodeError as e:
raise IOError("Job Runner cli failed to load JSON-file.") from e
raise OSError("Job Runner cli failed to load JSON-file.") from e
except FileNotFoundError as e:
if retry:
logger.error(f"Could not find file {JOBS_FILE}, retrying")
Expand Down
33 changes: 14 additions & 19 deletions src/_ert/forward_model_runner/client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
import logging
import ssl
from typing import Any, AnyStr, Optional, Self, Union
from typing import Any, AnyStr, Self

from websockets.asyncio.client import ClientConnection, connect
from websockets.datastructures import Headers
Expand Down Expand Up @@ -50,10 +50,10 @@ async def __aexit__(
def __init__(
self,
url: str,
token: Optional[str] = None,
cert: Optional[Union[str, bytes]] = None,
max_retries: Optional[int] = None,
timeout_multiplier: Optional[int] = None,
token: str | None = None,
cert: str | bytes | None = None,
max_retries: int | None = None,
timeout_multiplier: int | None = None,
) -> None:
if max_retries is None:
max_retries = self.DEFAULT_MAX_RETRIES
Expand All @@ -72,7 +72,7 @@ def __init__(
# if True it will enforce TLS, and if you want to use self signed
# certificates you need to pass an ssl_context with the certificate
# loaded.
self._ssl_context: Optional[Union[bool, ssl.SSLContext]] = None
self._ssl_context: bool | ssl.SSLContext | None = None
if cert is not None:
self._ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
self._ssl_context.load_verify_locations(cadata=cert)
Expand All @@ -81,7 +81,7 @@ def __init__(

self._max_retries = max_retries
self._timeout_multiplier = timeout_multiplier
self.websocket: Optional[ClientConnection] = None
self.websocket: ClientConnection | None = None
self.loop = new_event_loop()

async def get_websocket(self) -> ClientConnection:
Expand All @@ -103,33 +103,28 @@ async def _send(self, msg: AnyStr) -> None:
await self.websocket.send(msg)
return
except ConnectionClosedOK as exception:
_error_msg = (
error_msg = (
f"Connection closed received from the server {self.url}! "
f" Exception from {type(exception)}: {exception!s}"
)
raise ClientConnectionClosedOK(_error_msg) from exception
except (
InvalidHandshake,
InvalidURI,
OSError,
asyncio.TimeoutError,
) as exception:
raise ClientConnectionClosedOK(error_msg) from exception
except (TimeoutError, InvalidHandshake, InvalidURI, OSError) as exception:
if retry == self._max_retries:
_error_msg = (
error_msg = (
f"Not able to establish the "
f"websocket connection {self.url}! Max retries reached!"
" Check for firewall issues."
f" Exception from {type(exception)}: {exception!s}"
)
raise ClientConnectionError(_error_msg) from exception
raise ClientConnectionError(error_msg) from exception
except ConnectionClosedError as exception:
if retry == self._max_retries:
_error_msg = (
error_msg = (
f"Not been able to send the event"
f" to {self.url}! Max retries reached!"
f" Exception from {type(exception)}: {exception!s}"
)
raise ClientConnectionError(_error_msg) from exception
raise ClientConnectionError(error_msg) from exception
await asyncio.sleep(0.2 + self._timeout_multiplier * retry)
self.websocket = None

Expand Down
20 changes: 8 additions & 12 deletions src/_ert/forward_model_runner/forward_model_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
import socket
import sys
import time
from collections.abc import Generator, Sequence
from datetime import datetime as dt
from pathlib import Path
from subprocess import Popen, run
from typing import TYPE_CHECKING, Generator, Sequence, cast
from typing import TYPE_CHECKING, cast

from psutil import AccessDenied, NoSuchProcess, Process, TimeoutExpired, ZombieProcess

Expand Down Expand Up @@ -91,8 +92,7 @@ def __init__(

def run(self) -> Generator[Start | Exited | Running | None]:
try:
for msg in self._run():
yield msg
yield from self._run()
except Exception as e:
yield Exited(self, exit_code=1).with_error(str(e))

Expand Down Expand Up @@ -293,11 +293,9 @@ def handle_process_timeout_and_create_exited_msg(
os.killpg(process_group_id, signal.SIGKILL)

return Exited(self, exit_code).with_error(
(
f"Job:{self.name()} has been running "
f"for more than {max_running_minutes} "
"minutes - explicitly killed."
)
f"Job:{self.name()} has been running "
f"for more than {max_running_minutes} "
"minutes - explicitly killed."
)

def _handle_process_io_error_and_create_exited_message(
Expand Down Expand Up @@ -403,10 +401,8 @@ def _assert_arg_list(self):
int(arg_list[index])
except ValueError:
errors.append(
(
f"In job {self.name()}: argument with index {index} "
"is of incorrect type, should be integer."
)
f"In job {self.name()}: argument with index {index} "
"is of incorrect type, should be integer."
)
return errors

Expand Down
4 changes: 2 additions & 2 deletions src/_ert/forward_model_runner/reporting/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import threading
from datetime import datetime, timedelta
from pathlib import Path
from typing import Final, Union
from typing import Final

from _ert import events
from _ert.events import (
Expand Down Expand Up @@ -138,7 +138,7 @@ def _init_handler(self, msg: Init):
self._real_id = str(msg.real_id)
self._event_publisher_thread.start()

def _job_handler(self, msg: Union[Start, Running, Exited]):
def _job_handler(self, msg: Start | Running | Exited):
assert msg.job
job_name = msg.job.name()
job_msg = {
Expand Down
4 changes: 1 addition & 3 deletions src/_ert/forward_model_runner/reporting/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,7 @@ def _dump_error_file(fm_step, error_msg):
stderr_file = None
if fm_step.std_err:
if os.path.exists(fm_step.std_err):
with open(
fm_step.std_err, "r", encoding="utf-8"
) as error_file_handler:
with open(fm_step.std_err, encoding="utf-8") as error_file_handler:
stderr = error_file_handler.read()
if stderr:
stderr_file = os.path.join(os.getcwd(), fm_step.std_err)
Expand Down
12 changes: 5 additions & 7 deletions src/_ert/forward_model_runner/reporting/interactive.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Optional

from _ert.forward_model_runner.reporting.base import Reporter
from _ert.forward_model_runner.reporting.message import (
_JOB_EXIT_FAILED_STRING,
Expand All @@ -11,8 +9,8 @@

class Interactive(Reporter):
@staticmethod
def _report(msg: Message) -> Optional[str]:
if not isinstance(msg, (Start, Finish)):
def _report(msg: Message) -> str | None:
if not isinstance(msg, Start | Finish):
return None
if isinstance(msg, Finish):
return (
Expand All @@ -27,6 +25,6 @@ def _report(msg: Message) -> Optional[str]:
return f"Running job: {msg.job.name()} ... "

def report(self, msg: Message):
_msg = self._report(msg)
if _msg is not None:
print(_msg)
msg_ = self._report(msg)
if msg_ is not None:
print(msg_)
20 changes: 10 additions & 10 deletions src/_ert/forward_model_runner/reporting/message.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import dataclasses
from datetime import datetime as dt
from typing import TYPE_CHECKING, Dict, Literal, Optional
from typing import TYPE_CHECKING, Literal

import psutil
from typing_extensions import TypedDict
Expand Down Expand Up @@ -39,17 +39,17 @@ class ProcessTreeStatus:
"""Holds processtree information that can be represented as a line of CSV data"""

timestamp: str = ""
fm_step_id: Optional[int] = None
fm_step_name: Optional[str] = None
fm_step_id: int | None = None
fm_step_name: str | None = None

# Memory unit is bytes
rss: Optional[int] = None
max_rss: Optional[int] = None
free: Optional[int] = None
rss: int | None = None
max_rss: int | None = None
free: int | None = None

cpu_seconds: float = 0.0

oom_score: Optional[int] = None
oom_score: int | None = None

def __post_init__(self):
self.timestamp = dt.now().isoformat()
Expand All @@ -72,8 +72,8 @@ def __repr__(cls):
class Message(metaclass=_MetaMessage):
def __init__(self, job=None):
self.timestamp = dt.now()
self.job: Optional[ForwardModelStep] = job
self.error_message: Optional[str] = None
self.job: ForwardModelStep | None = job
self.error_message: str | None = None

def __repr__(self):
return type(self).__name__
Expand Down Expand Up @@ -134,7 +134,7 @@ def __init__(self, fm_step, exit_code: int):


class Checksum(Message):
def __init__(self, checksum_dict: Dict[str, "ChecksumDict"], run_path: str):
def __init__(self, checksum_dict: dict[str, "ChecksumDict"], run_path: str):
super().__init__()
self.data = checksum_dict
self.run_path = run_path
6 changes: 3 additions & 3 deletions src/_ert/forward_model_runner/reporting/statemachine.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from typing import Callable, Dict, Tuple, Type
from collections.abc import Callable

from _ert.forward_model_runner.reporting.message import (
Checksum,
Expand All @@ -25,7 +25,7 @@ def __init__(self) -> None:
jobs = (Start, Running, Exited)
checksum = (Checksum,)
finished = (Finish,)
self._handler: Dict[Message, Callable[[Message], None]] = {}
self._handler: dict[Message, Callable[[Message], None]] = {}
self._transitions = {
None: initialized,
initialized: jobs + checksum + finished,
Expand All @@ -35,7 +35,7 @@ def __init__(self) -> None:
self._state = None

def add_handler(
self, states: Tuple[Type[Message], ...], handler: Callable[[Message], None]
self, states: tuple[type[Message], ...], handler: Callable[[Message], None]
) -> None:
if states in self._handler:
raise ValueError(f"{states} already handled by {self._handler[states]}")
Expand Down
Loading

0 comments on commit 66cda31

Please sign in to comment.