Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration to ruff #1176

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pip install -r linters-requirements.txt

## Code style

OpenFL uses [black](https://black.readthedocs.io/en/stable/), [isort](https://pycqa.github.io/isort/) and [precommit](https://pre-commit.com/) to format the code.
OpenFL uses [ruff](https://github.com/astral-sh/ruff) to lint/format code and [precommit](https://pre-commit.com/) checks.

Run the following command at the **root** directory of the repo to format your code.

Expand Down
4 changes: 1 addition & 3 deletions linters-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
black
flake8
isort
pre-commit
ruff
1 change: 1 addition & 0 deletions openfl/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@


"""openfl version information."""

__version__ = "1.6"
1 change: 1 addition & 0 deletions openfl/component/aggregator/aggregator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@


"""Aggregator module."""

import queue
import time
from logging import getLogger
Expand Down
1 change: 1 addition & 0 deletions openfl/component/collaborator/collaborator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@


"""Collaborator module."""

from enum import Enum
from logging import getLogger
from time import sleep
Expand Down
2 changes: 1 addition & 1 deletion openfl/component/director/director.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ def update_envoy_status(
if not shard_info:
raise ShardNotFoundError(f"Unknown shard {envoy_name}")

shard_info["is_online"]: True
shard_info["is_online"] = True
shard_info["is_experiment_running"] = is_experiment_running
shard_info["valid_duration"] = 2 * self.envoy_health_check_period
shard_info["last_updated"] = time.time()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@


"""Cutoff time based Straggler Handling function."""

import threading
import time
from logging import getLogger
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@


"""Percentage based Straggler Handling function."""

from logging import getLogger

from openfl.component.straggler_handling_functions.straggler_handling_function import (
Expand Down
1 change: 1 addition & 0 deletions openfl/cryptography/participant.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@


"""Cryptography participant utilities."""

from typing import Tuple

from cryptography import x509
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@


"""Experimental Aggregator module."""

import inspect
import pickle
import queue
Expand Down Expand Up @@ -47,8 +48,8 @@ def __init__(
rounds_to_train: int = 1,
checkpoint: bool = False,
private_attributes_callable: Callable = None,
private_attributes_kwargs: Dict = {},
private_attributes: Dict = {},
private_attributes_kwargs: Dict = None,
private_attributes: Dict = None,
single_col_cert_common_name: str = None,
log_metric_callback: Callable = None,
**kwargs,
Expand Down Expand Up @@ -232,7 +233,7 @@ def call_checkpoint(self, ctx: Any, f: Callable, stream_buffer: bytes = None) ->
f = pickle.loads(f)
if isinstance(stream_buffer, bytes):
# Set stream buffer as function parameter
setattr(f.__func__, "_stream_buffer", pickle.loads(stream_buffer))
f.__func__._stream_buffer = pickle.loads(stream_buffer)
Comment on lines -235 to +236
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an example of simplification automatically made by ruff.


checkpoint(ctx, f)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@


"""Experimental Collaborator module."""

import pickle
import time
from logging import getLogger
Expand Down Expand Up @@ -35,8 +36,8 @@ def __init__(
federation_uuid: str,
client: Any,
private_attributes_callable: Any = None,
private_attributes_kwargs: Dict = {},
private_attributes: Dict = {},
private_attributes_kwargs: Dict = None,
private_attributes: Dict = None,
**kwargs,
) -> None:
self.name = collaborator_name
Expand Down
1 change: 1 addition & 0 deletions openfl/experimental/workflow/federated/plan/plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@


"""Plan module."""

import inspect
import os
from hashlib import sha384
Expand Down
1 change: 1 addition & 0 deletions openfl/experimental/workflow/interface/cli/aggregator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@


"""Aggregator module."""

import os
import sys
import threading
Expand Down
99 changes: 2 additions & 97 deletions openfl/experimental/workflow/interface/cli/cli_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@


"""Module with auxiliary CLI helper functions."""

import os
import re
import shutil
from itertools import islice
from os import environ, stat
from os import environ
from pathlib import Path
from sys import argv

Expand All @@ -31,20 +31,6 @@ def pretty(o):
echo(style(f"{k:<{m}} : ", fg="blue") + style(f"{v}", fg="cyan"))


def tree(path):
"""Print current directory file tree."""
echo(f"+ {path}")

for path in sorted(path.rglob("*")):
depth = len(path.relative_to(path).parts)
space = " " * depth

if path.is_file():
echo(f"{space}f {path.name}")
else:
echo(f"{space}d {path.name}")


def print_tree(
dir_path: Path,
level: int = -1,
Expand Down Expand Up @@ -91,87 +77,6 @@ def inner(dir_path: Path, prefix: str = "", level=-1):
echo(f"\n{directories} directories" + (f", {files} files" if files else ""))


def copytree(
src,
dst,
symlinks=False,
ignore=None,
ignore_dangling_symlinks=False,
dirs_exist_ok=False,
):
"""From Python 3.8 'shutil' which include 'dirs_exist_ok' option."""

with os.scandir(src) as itr:
entries = list(itr)

copy_function = shutil.copy2

def _copytree():
if ignore is not None:
ignored_names = ignore(os.fspath(src), [x.name for x in entries])
else:
ignored_names = set()

os.makedirs(dst, exist_ok=dirs_exist_ok)
errors = []
use_srcentry = copy_function is shutil.copy2 or copy_function is shutil.copy

for srcentry in entries:
if srcentry.name in ignored_names:
continue
srcname = os.path.join(src, srcentry.name)
dstname = os.path.join(dst, srcentry.name)
srcobj = srcentry if use_srcentry else srcname
try:
is_symlink = srcentry.is_symlink()
if is_symlink and os.name == "nt":
lstat = srcentry.stat(follow_symlinks=False)
if lstat.st_reparse_tag == stat.IO_REPARSE_TAG_MOUNT_POINT:
is_symlink = False
if is_symlink:
linkto = os.readlink(srcname)
if symlinks:
os.symlink(linkto, dstname)
shutil.copystat(srcobj, dstname, follow_symlinks=not symlinks)
else:
if not os.path.exists(linkto) and ignore_dangling_symlinks:
continue
if srcentry.is_dir():
copytree(
srcobj,
dstname,
symlinks,
ignore,
dirs_exist_ok=dirs_exist_ok,
)
else:
copy_function(srcobj, dstname)
elif srcentry.is_dir():
copytree(
srcobj,
dstname,
symlinks,
ignore,
dirs_exist_ok=dirs_exist_ok,
)
else:
copy_function(srcobj, dstname)
except OSError as why:
errors.append((srcname, dstname, str(why)))
except Exception as err:
errors.extend(err.args[0])
try:
shutil.copystat(src, dst)
except OSError as why:
if getattr(why, "winerror", None) is None:
errors.append((src, dst, str(why)))
if errors:
raise Exception(errors)
return dst

return _copytree()


def get_workspace_parameter(name):
"""Get a parameter from the workspace config file (.workspace)."""
# Update the .workspace file to show the current workspace plan
Expand Down
3 changes: 2 additions & 1 deletion openfl/experimental/workflow/interface/cli/collaborator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@


"""Collaborator module."""

import os
import sys
from glob import glob
Expand Down Expand Up @@ -246,7 +247,7 @@ def certify_(collaborator_name, silent, request_pkg, import_):
certify(collaborator_name, silent, request_pkg, import_)


def certify(collaborator_name, silent, request_pkg=None, import_=False):
def certify(collaborator_name, silent, request_pkg=None, import_=False): # noqa C901
"""Sign/certify collaborator certificate key pair."""

common_name = f"{collaborator_name}"
Expand Down
1 change: 1 addition & 0 deletions openfl/experimental/workflow/interface/cli/plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@


"""Plan module."""

import sys
from logging import getLogger
from pathlib import Path
Expand Down
4 changes: 2 additions & 2 deletions openfl/experimental/workflow/interface/cli/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@


"""Workspace module."""

import os
import sys
from hashlib import sha256
Expand All @@ -16,9 +17,8 @@
from tempfile import mkdtemp
from typing import Tuple

from click import Choice
from click import Choice, confirm, echo, group, option, pass_context, style
from click import Path as ClickPath
from click import confirm, echo, group, option, pass_context, style
from cryptography.hazmat.primitives import serialization

from openfl.cryptography.ca import generate_root_cert, generate_signing_csr, sign_certificate
Expand Down
4 changes: 2 additions & 2 deletions openfl/experimental/workflow/interface/participants.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def __init__(
private_attributes_callable: Callable = None,
num_cpus: int = 0,
num_gpus: int = 0.0,
**kwargs
**kwargs,
):
"""Initializes the Collaborator object.

Expand Down Expand Up @@ -190,7 +190,7 @@ def __init__(
private_attributes_callable: Callable = None,
num_cpus: int = 0,
num_gpus: int = 0.0,
**kwargs
**kwargs,
):
"""Initializes the Aggregator object.

Expand Down
4 changes: 2 additions & 2 deletions openfl/experimental/workflow/placement/placement.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def wrapper(*args, **kwargs):
print(f"\nCalling {f.__name__}")
with RedirectStdStreamContext() as context_stream:
# context_stream capture stdout and stderr for the function f.__name__
setattr(wrapper, "_stream_buffer", context_stream)
wrapper._stream_buffer = context_stream
f(*args, **kwargs)

return wrapper
Expand Down Expand Up @@ -92,7 +92,7 @@ def wrapper(*args, **kwargs):
print(f"\nCalling {f.__name__}")
with RedirectStdStreamContext() as context_stream:
# context_stream capture stdout and stderr for the function f.__name__
setattr(wrapper, "_stream_buffer", context_stream)
wrapper._stream_buffer = context_stream
f(*args, **kwargs)

return wrapper
1 change: 1 addition & 0 deletions openfl/experimental/workflow/protocols/interceptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@


"""gRPC interceptors module."""

import collections

import grpc
Expand Down
2 changes: 1 addition & 1 deletion openfl/experimental/workflow/runtime/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# SPDX-License-Identifier: Apache-2.0


""" openfl.experimental.workflow.runtime package Runtime class."""
"""openfl.experimental.workflow.runtime package Runtime class."""

from openfl.experimental.workflow.runtime.federated_runtime import FederatedRuntime
from openfl.experimental.workflow.runtime.local_runtime import LocalRuntime
Expand Down
5 changes: 2 additions & 3 deletions openfl/experimental/workflow/runtime/federated_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# SPDX-License-Identifier: Apache-2.0


""" openfl.experimental.workflow.runtime package LocalRuntime class."""
"""openfl.experimental.workflow.runtime package LocalRuntime class."""

from __future__ import annotations

Expand All @@ -11,8 +11,7 @@
from openfl.experimental.workflow.runtime.runtime import Runtime

if TYPE_CHECKING:
from openfl.experimental.workflow.interface import Aggregator
from openfl.experimental.workflow.interface import Collaborator
from openfl.experimental.workflow.interface import Aggregator, Collaborator

from typing import List, Type

Expand Down
5 changes: 3 additions & 2 deletions openfl/experimental/workflow/runtime/local_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
# SPDX-License-Identifier: Apache-2.0


""" openfl.experimental.workflow.runtime package LocalRuntime class."""
"""openfl.experimental.workflow.runtime package LocalRuntime class."""

from __future__ import annotations

import gc
Expand Down Expand Up @@ -420,7 +421,7 @@ def __get_aggregator_object(self, aggregator: Type[Aggregator]) -> Any:
)

interface_module = importlib.import_module("openfl.experimental.workflow.interface")
aggregator_class = getattr(interface_module, "Aggregator")
aggregator_class = interface_module.Aggregator

aggregator_actor = ray.remote(aggregator_class).options(
num_cpus=agg_cpus, num_gpus=agg_gpus
Expand Down
2 changes: 1 addition & 1 deletion openfl/experimental/workflow/runtime/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# SPDX-License-Identifier: Apache-2.0


""" openfl.experimental.workflow.runtime module Runtime class."""
"""openfl.experimental.workflow.runtime module Runtime class."""

from typing import Callable, List

Expand Down
Loading
Loading