Skip to content

Commit

Permalink
ci(framework) Use Python 3.9 format (#4199)
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesbvll authored Sep 13, 2024
1 parent c161ac2 commit 914b3da
Show file tree
Hide file tree
Showing 140 changed files with 857 additions and 882 deletions.
7 changes: 4 additions & 3 deletions examples/quickstart-monai/monaiexample/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,10 @@ def _download_and_extract_if_needed(url, dest_folder):
# Download the tar.gz file
tar_gz_filename = url.split("/")[-1]
if not os.path.isfile(tar_gz_filename):
with request.urlopen(url) as response, open(
tar_gz_filename, "wb"
) as out_file:
with (
request.urlopen(url) as response,
open(tar_gz_filename, "wb") as out_file,
):
out_file.write(response.read())

# Extract the tar.gz file
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ known_first_party = ["flwr", "flwr_tool"]

[tool.black]
line-length = 88
target-version = ["py38", "py39", "py310", "py311"]
target-version = ["py39", "py310", "py311"]

[tool.pylint."MESSAGES CONTROL"]
disable = "duplicate-code,too-few-public-methods,useless-import-alias"
Expand Down Expand Up @@ -193,7 +193,7 @@ wrap-summaries = 88
wrap-descriptions = 88

[tool.ruff]
target-version = "py38"
target-version = "py39"
line-length = 88
select = ["D", "E", "F", "W", "B", "ISC", "C4", "UP"]
fixable = ["D", "E", "F", "W", "B", "ISC", "C4", "UP"]
Expand Down
3 changes: 1 addition & 2 deletions src/py/flwr/cli/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@
import os
import zipfile
from pathlib import Path
from typing import Optional
from typing import Annotated, Optional

import pathspec
import tomli_w
import typer
from typing_extensions import Annotated

from .config_utils import load_and_validate
from .utils import get_sha256_hash, is_valid_project_name
Expand Down
20 changes: 10 additions & 10 deletions src/py/flwr/cli/config_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
import zipfile
from io import BytesIO
from pathlib import Path
from typing import IO, Any, Dict, List, Optional, Tuple, Union, get_args
from typing import IO, Any, Optional, Union, get_args

import tomli

from flwr.common import object_ref
from flwr.common.typing import UserConfigValue


def get_fab_config(fab_file: Union[Path, bytes]) -> Dict[str, Any]:
def get_fab_config(fab_file: Union[Path, bytes]) -> dict[str, Any]:
"""Extract the config from a FAB file or path.
Parameters
Expand Down Expand Up @@ -62,7 +62,7 @@ def get_fab_config(fab_file: Union[Path, bytes]) -> Dict[str, Any]:
return conf


def get_fab_metadata(fab_file: Union[Path, bytes]) -> Tuple[str, str]:
def get_fab_metadata(fab_file: Union[Path, bytes]) -> tuple[str, str]:
"""Extract the fab_id and the fab_version from a FAB file or path.
Parameters
Expand All @@ -87,7 +87,7 @@ def get_fab_metadata(fab_file: Union[Path, bytes]) -> Tuple[str, str]:
def load_and_validate(
path: Optional[Path] = None,
check_module: bool = True,
) -> Tuple[Optional[Dict[str, Any]], List[str], List[str]]:
) -> tuple[Optional[dict[str, Any]], list[str], list[str]]:
"""Load and validate pyproject.toml as dict.
Returns
Expand Down Expand Up @@ -116,7 +116,7 @@ def load_and_validate(
return (config, errors, warnings)


def load(toml_path: Path) -> Optional[Dict[str, Any]]:
def load(toml_path: Path) -> Optional[dict[str, Any]]:
"""Load pyproject.toml and return as dict."""
if not toml_path.is_file():
return None
Expand All @@ -125,7 +125,7 @@ def load(toml_path: Path) -> Optional[Dict[str, Any]]:
return load_from_string(toml_file.read())


def _validate_run_config(config_dict: Dict[str, Any], errors: List[str]) -> None:
def _validate_run_config(config_dict: dict[str, Any], errors: list[str]) -> None:
for key, value in config_dict.items():
if isinstance(value, dict):
_validate_run_config(config_dict[key], errors)
Expand All @@ -137,7 +137,7 @@ def _validate_run_config(config_dict: Dict[str, Any], errors: List[str]) -> None


# pylint: disable=too-many-branches
def validate_fields(config: Dict[str, Any]) -> Tuple[bool, List[str], List[str]]:
def validate_fields(config: dict[str, Any]) -> tuple[bool, list[str], list[str]]:
"""Validate pyproject.toml fields."""
errors = []
warnings = []
Expand Down Expand Up @@ -183,10 +183,10 @@ def validate_fields(config: Dict[str, Any]) -> Tuple[bool, List[str], List[str]]


def validate(
config: Dict[str, Any],
config: dict[str, Any],
check_module: bool = True,
project_dir: Optional[Union[str, Path]] = None,
) -> Tuple[bool, List[str], List[str]]:
) -> tuple[bool, list[str], list[str]]:
"""Validate pyproject.toml."""
is_valid, errors, warnings = validate_fields(config)

Expand All @@ -210,7 +210,7 @@ def validate(
return True, [], []


def load_from_string(toml_content: str) -> Optional[Dict[str, Any]]:
def load_from_string(toml_content: str) -> Optional[dict[str, Any]]:
"""Load TOML content from a string and return as dict."""
try:
data = tomli.loads(toml_content)
Expand Down
4 changes: 2 additions & 2 deletions src/py/flwr/cli/config_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import os
import textwrap
from pathlib import Path
from typing import Any, Dict
from typing import Any

from .config_utils import load, validate, validate_fields

Expand Down Expand Up @@ -155,7 +155,7 @@ def test_load_pyproject_toml_from_path(tmp_path: Path) -> None:
def test_validate_pyproject_toml_fields_empty() -> None:
"""Test that validate_pyproject_toml_fields fails correctly."""
# Prepare
config: Dict[str, Any] = {}
config: dict[str, Any] = {}

# Execute
is_valid, errors, warnings = validate_fields(config)
Expand Down
3 changes: 1 addition & 2 deletions src/py/flwr/cli/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@
import zipfile
from io import BytesIO
from pathlib import Path
from typing import IO, Optional, Union
from typing import IO, Annotated, Optional, Union

import typer
from typing_extensions import Annotated

from flwr.common.config import get_flwr_dir

Expand Down
7 changes: 3 additions & 4 deletions src/py/flwr/cli/new/new.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@
from enum import Enum
from pathlib import Path
from string import Template
from typing import Dict, Optional
from typing import Annotated, Optional

import typer
from typing_extensions import Annotated

from ..utils import (
is_valid_project_name,
Expand Down Expand Up @@ -70,7 +69,7 @@ def load_template(name: str) -> str:
return tpl_file.read()


def render_template(template: str, data: Dict[str, str]) -> str:
def render_template(template: str, data: dict[str, str]) -> str:
"""Render template."""
tpl_file = load_template(template)
tpl = Template(tpl_file)
Expand All @@ -85,7 +84,7 @@ def create_file(file_path: Path, content: str) -> None:
file_path.write_text(content)


def render_and_create(file_path: Path, template: str, context: Dict[str, str]) -> None:
def render_and_create(file_path: Path, template: str, context: dict[str, str]) -> None:
"""Render template and write to file."""
content = render_template(template, context)
create_file(file_path, content)
Expand Down
13 changes: 6 additions & 7 deletions src/py/flwr/cli/run/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@
import sys
from logging import DEBUG
from pathlib import Path
from typing import Any, Dict, List, Optional
from typing import Annotated, Any, Optional

import typer
from typing_extensions import Annotated

from flwr.cli.build import build
from flwr.cli.config_utils import load_and_validate
Expand Down Expand Up @@ -52,7 +51,7 @@ def run(
typer.Argument(help="Name of the federation to run the app on."),
] = None,
config_overrides: Annotated[
Optional[List[str]],
Optional[list[str]],
typer.Option(
"--run-config",
"-c",
Expand Down Expand Up @@ -125,8 +124,8 @@ def run(

def _run_with_superexec(
app: Path,
federation_config: Dict[str, Any],
config_overrides: Optional[List[str]],
federation_config: dict[str, Any],
config_overrides: Optional[list[str]],
) -> None:

insecure_str = federation_config.get("insecure")
Expand Down Expand Up @@ -187,8 +186,8 @@ def _run_with_superexec(

def _run_without_superexec(
app: Optional[Path],
federation_config: Dict[str, Any],
config_overrides: Optional[List[str]],
federation_config: dict[str, Any],
config_overrides: Optional[list[str]],
federation: str,
) -> None:
try:
Expand Down
4 changes: 2 additions & 2 deletions src/py/flwr/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import hashlib
import re
from pathlib import Path
from typing import Callable, List, Optional, cast
from typing import Callable, Optional, cast

import typer

Expand All @@ -40,7 +40,7 @@ def prompt_text(
return cast(str, result)


def prompt_options(text: str, options: List[str]) -> str:
def prompt_options(text: str, options: list[str]) -> str:
"""Ask user to select one of the given options and return the selected item."""
# Turn options into a list with index as in " [ 0] quickstart-pytorch"
options_formatted = [
Expand Down
21 changes: 11 additions & 10 deletions src/py/flwr/client/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
import subprocess
import sys
import time
from contextlib import AbstractContextManager
from dataclasses import dataclass
from logging import ERROR, INFO, WARN
from pathlib import Path
from typing import Callable, ContextManager, Dict, Optional, Tuple, Type, Union, cast
from typing import Callable, Optional, Union, cast

import grpc
from cryptography.hazmat.primitives.asymmetric import ec
Expand Down Expand Up @@ -94,7 +95,7 @@ def start_client(
insecure: Optional[bool] = None,
transport: Optional[str] = None,
authentication_keys: Optional[
Tuple[ec.EllipticCurvePrivateKey, ec.EllipticCurvePublicKey]
tuple[ec.EllipticCurvePrivateKey, ec.EllipticCurvePublicKey]
] = None,
max_retries: Optional[int] = None,
max_wait_time: Optional[float] = None,
Expand Down Expand Up @@ -204,7 +205,7 @@ def start_client_internal(
insecure: Optional[bool] = None,
transport: Optional[str] = None,
authentication_keys: Optional[
Tuple[ec.EllipticCurvePrivateKey, ec.EllipticCurvePublicKey]
tuple[ec.EllipticCurvePrivateKey, ec.EllipticCurvePublicKey]
] = None,
max_retries: Optional[int] = None,
max_wait_time: Optional[float] = None,
Expand Down Expand Up @@ -356,7 +357,7 @@ def _on_backoff(retry_state: RetryState) -> None:
# NodeState gets initialized when the first connection is established
node_state: Optional[NodeState] = None

runs: Dict[int, Run] = {}
runs: dict[int, Run] = {}

while not app_state_tracker.interrupt:
sleep_duration: int = 0
Expand Down Expand Up @@ -689,18 +690,18 @@ def start_numpy_client(
)


def _init_connection(transport: Optional[str], server_address: str) -> Tuple[
def _init_connection(transport: Optional[str], server_address: str) -> tuple[
Callable[
[
str,
bool,
RetryInvoker,
int,
Union[bytes, str, None],
Optional[Tuple[ec.EllipticCurvePrivateKey, ec.EllipticCurvePublicKey]],
Optional[tuple[ec.EllipticCurvePrivateKey, ec.EllipticCurvePublicKey]],
],
ContextManager[
Tuple[
AbstractContextManager[
tuple[
Callable[[], Optional[Message]],
Callable[[Message], None],
Optional[Callable[[], Optional[int]]],
Expand All @@ -711,7 +712,7 @@ def _init_connection(transport: Optional[str], server_address: str) -> Tuple[
],
],
str,
Type[Exception],
type[Exception],
]:
# Parse IP address
parsed_address = parse_address(server_address)
Expand Down Expand Up @@ -769,7 +770,7 @@ def signal_handler(sig, frame): # type: ignore
signal.signal(signal.SIGTERM, signal_handler)


def run_clientappio_api_grpc(address: str) -> Tuple[grpc.Server, ClientAppIoServicer]:
def run_clientappio_api_grpc(address: str) -> tuple[grpc.Server, ClientAppIoServicer]:
"""Run ClientAppIo API gRPC server."""
clientappio_servicer: grpc.Server = ClientAppIoServicer()
clientappio_add_servicer_to_server_fn = add_ClientAppIoServicer_to_server
Expand Down
8 changes: 3 additions & 5 deletions src/py/flwr/client/app_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
"""Flower Client app tests."""


from typing import Dict, Tuple

from flwr.common import (
Config,
EvaluateIns,
Expand Down Expand Up @@ -59,7 +57,7 @@ def evaluate(self, ins: EvaluateIns) -> EvaluateRes:
class NeedsWrappingClient(NumPyClient):
"""Client implementation extending the high-level NumPyClient."""

def get_properties(self, config: Config) -> Dict[str, Scalar]:
def get_properties(self, config: Config) -> dict[str, Scalar]:
"""Raise an Exception because this method is not expected to be called."""
raise NotImplementedError()

Expand All @@ -69,13 +67,13 @@ def get_parameters(self, config: Config) -> NDArrays:

def fit(
self, parameters: NDArrays, config: Config
) -> Tuple[NDArrays, int, Dict[str, Scalar]]:
) -> tuple[NDArrays, int, dict[str, Scalar]]:
"""Raise an Exception because this method is not expected to be called."""
raise NotImplementedError()

def evaluate(
self, parameters: NDArrays, config: Config
) -> Tuple[float, int, Dict[str, Scalar]]:
) -> tuple[float, int, dict[str, Scalar]]:
"""Raise an Exception because this method is not expected to be called."""
raise NotImplementedError()

Expand Down
6 changes: 3 additions & 3 deletions src/py/flwr/client/client_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


import inspect
from typing import Callable, List, Optional
from typing import Callable, Optional

from flwr.client.client import Client
from flwr.client.message_handler.message_handler import (
Expand Down Expand Up @@ -109,9 +109,9 @@ class ClientApp:
def __init__(
self,
client_fn: Optional[ClientFnExt] = None, # Only for backward compatibility
mods: Optional[List[Mod]] = None,
mods: Optional[list[Mod]] = None,
) -> None:
self._mods: List[Mod] = mods if mods is not None else []
self._mods: list[Mod] = mods if mods is not None else []

# Create wrapper function for `handle`
self._call: Optional[ClientAppCallable] = None
Expand Down
Loading

0 comments on commit 914b3da

Please sign in to comment.