Skip to content

Commit

Permalink
Add pyupgrade UP rule to ruff
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Eivind Jahren <[email protected]>
  • Loading branch information
larsevj and eivindjahren authored Dec 10, 2024
1 parent 82972b0 commit c9d43bc
Show file tree
Hide file tree
Showing 369 changed files with 1,980 additions and 2,301 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
42 changes: 21 additions & 21 deletions docs/everest/config_generated.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Type: *Optional[ModelConfig]*
Configuration of the Everest model

**realizations (optional)**
Type: *List[NonNegativeInt]*
Type: *list[NonNegativeInt]*

List of realizations to use in optimization ensemble.

Expand All @@ -22,7 +22,7 @@ Configuration of the Everest model


**realizations_weights (optional)**
Type: *Optional[List[float]]*
Type: *Optional[list[float]]*

List of weights, one per realization.

Expand All @@ -32,7 +32,7 @@ Configuration of the Everest model

controls (required)
-------------------
Type: *List[ControlConfig]*
Type: *list[ControlConfig]*

Defines a list of controls.
Controls should have unique names each control defines
Expand All @@ -57,7 +57,7 @@ a group of control variables


**variables (required)**
Type: *Union[List[ControlVariableConfig], List[ControlVariableGuessListConfig]]*
Type: *Union[list[ControlVariableConfig], list[ControlVariableGuessListConfig]]*

List of control variables

Expand Down Expand Up @@ -92,7 +92,7 @@ a group of control variables


**scaled_range (optional)**
Type: *Optional[Tuple[float, float]]*
Type: *Optional[tuple[float, float]]*


Can be used to set the range of the variable values
Expand Down Expand Up @@ -171,7 +171,7 @@ a group of control variables


**initial_guess (optional)**
Type: *List[float]*
Type: *list[float]*

List of Starting values for the control variable

Expand Down Expand Up @@ -271,7 +271,7 @@ a group of control variables


**scaled_range (optional)**
Type: *Optional[Tuple[float, float]]*
Type: *Optional[tuple[float, float]]*


Can be used to set the range of the control values
Expand Down Expand Up @@ -521,7 +521,7 @@ Optimizer options


**options (optional)**
Type: *Optional[List[str]]*
Type: *Optional[list[str]]*

specifies non-validated, optional
passthrough parameters for the optimizer
Expand Down Expand Up @@ -574,7 +574,7 @@ Optimizer options

objective_functions (required)
------------------------------
Type: *List[ObjectiveFunctionConfig]*
Type: *list[ObjectiveFunctionConfig]*

List of objective function specifications

Expand Down Expand Up @@ -697,7 +697,7 @@ The environment of Everest, specifies which folders are used for simulation and

wells (optional)
----------------
Type: *List[WellConfig]*
Type: *list[WellConfig]*

A list of well configurations, all with unique names.

Expand Down Expand Up @@ -726,7 +726,7 @@ A list of well configurations, all with unique names.

input_constraints (optional)
----------------------------
Type: *Optional[List[InputConstraintConfig]]*
Type: *Optional[list[InputConstraintConfig]]*

List of input constraints

Expand Down Expand Up @@ -795,7 +795,7 @@ List of input constraints

output_constraints (optional)
-----------------------------
Type: *Optional[List[OutputConstraintConfig]]*
Type: *Optional[list[OutputConstraintConfig]]*

A list of output constraints with unique names.

Expand Down Expand Up @@ -1100,7 +1100,7 @@ Simulation settings

install_jobs (optional)
-----------------------
Type: *Optional[List[InstallJobConfig]]*
Type: *Optional[list[InstallJobConfig]]*

A list of jobs to install

Expand All @@ -1119,7 +1119,7 @@ A list of jobs to install

install_workflow_jobs (optional)
--------------------------------
Type: *Optional[List[InstallJobConfig]]*
Type: *Optional[list[InstallJobConfig]]*

A list of workflow jobs to install

Expand All @@ -1138,7 +1138,7 @@ A list of workflow jobs to install

install_data (optional)
-----------------------
Type: *Optional[List[InstallDataConfig]]*
Type: *Optional[list[InstallDataConfig]]*

A list of install data elements from the install_data config
section. Each item marks what folders or paths need to be copied or linked
Expand Down Expand Up @@ -1173,7 +1173,7 @@ in order for the evaluation jobs to run.

install_templates (optional)
----------------------------
Type: *Optional[List[InstallTemplateConfig]]*
Type: *Optional[list[InstallTemplateConfig]]*

Allow the user to define the workflow establishing the model
chain for the purpose of sensitivity analysis, enabling the relationship
Expand Down Expand Up @@ -1201,7 +1201,7 @@ evaluated.

forward_model (optional)
------------------------
Type: *Optional[List[str]]*
Type: *Optional[list[str]]*

List of jobs to run

Expand All @@ -1213,13 +1213,13 @@ Type: *Optional[WorkflowConfig]*
Workflows to run during optimization

**pre_simulation (optional)**
Type: *Optional[List[str]]*
Type: *Optional[list[str]]*

List of workflow jobs triggered pre-simulation


**post_simulation (optional)**
Type: *Optional[List[str]]*
Type: *Optional[list[str]]*

List of workflow jobs triggered post-simulation

Expand Down Expand Up @@ -1314,13 +1314,13 @@ Settings to control the exports of a optimization run by everest.


**keywords (optional)**
Type: *Optional[List[str]]*
Type: *Optional[list[str]]*

List of eclipse keywords to be exported into csv.


**batches (optional)**
Type: *Optional[List[int]]*
Type: *Optional[list[int]]*

list of batches to be exported, default is all batches.

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
Loading

0 comments on commit c9d43bc

Please sign in to comment.