Skip to content

Commit

Permalink
improvement(ruff): enable F821 and F823 rules
Browse files Browse the repository at this point in the history
Enable following rules:
- F821, "undefined-name" -> Undefined name {name}. {tip}
- F823, "undefined-local" -> Local variable {name} referenced before assignment
  • Loading branch information
vponomaryov authored and fruch committed Sep 29, 2024
1 parent dd6d58a commit 30aee48
Show file tree
Hide file tree
Showing 22 changed files with 60 additions and 49 deletions.
1 change: 1 addition & 0 deletions multiple_dc_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

# TODO: this test seem to be broken, hence disabling all pylint checks
# pylint: disable=all
# ruff: noqa: F821

from sdcm.tester import ClusterTester

Expand Down
7 changes: 6 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
[tool.ruff]
lint.select = ["PL", "PLR0916", "PLR0913","PLR0914", "YTT", "BLE", "F841", "F401"]
lint.select = [
"BLE",
"F401", "F821", "F823", "F841",
"PL", "PLR0913","PLR0914", "PLR0916",
"YTT",
]

lint.ignore = ["E501", "PLR2004"]

Expand Down
2 changes: 1 addition & 1 deletion sdcm/audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def read(self, from_datetime: Optional[datetime] = None, category: Optional[Audi
class Audit:
"""Manage audit state and query audit log on Scylla cluster."""

def __init__(self, cluster: "BaseCluster"):
def __init__(self, cluster: "BaseCluster"): # noqa: F821
self._cluster = cluster
self._configuration = self._get_audit_configuration()

Expand Down
4 changes: 2 additions & 2 deletions sdcm/cluster_k8s/mini_k8s.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ def _target_user(self) -> Optional[str]:

@property
@abc.abstractmethod
def host_node(self) -> 'BaseNode':
def host_node(self) -> 'BaseNode': # noqa: F821
"""
Host where kind/k3d/minikube is running
"""
Expand Down Expand Up @@ -457,7 +457,7 @@ def upgrade_kubernetes_platform(self, pod_objects: list[cluster.BaseNode],
class LocalKindCluster(LocalMinimalClusterBase):
docker_pull: Callable
docker_tag: Callable
host_node: 'BaseNode'
host_node: 'BaseNode' # noqa: F821
scylla_image: Optional[str]
software_version: str
_target_user: str
Expand Down
8 changes: 4 additions & 4 deletions sdcm/coredump.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
@dataclass
class CoreDumpInfo:
pid: str
node: 'BaseNode' = None
node: 'BaseNode' = None # noqa: F821
corefile: str = ''
source_timestamp: Optional[float] = None
coredump_info: str = ''
Expand All @@ -62,7 +62,7 @@ def __str__(self):

# pylint: disable=too-many-arguments
def update(self,
node: 'BaseNode' = None,
node: 'BaseNode' = None, # noqa: F821
corefile: str = None,
source_timestamp: Optional[float] = None,
coredump_info: str = None,
Expand Down Expand Up @@ -91,7 +91,7 @@ class CoredumpThreadBase(Thread): # pylint: disable=too-many-instance-attribute
upload_retry_limit = 3
max_coredump_thread_exceptions = 10

def __init__(self, node: 'BaseNode', max_core_upload_limit: int):
def __init__(self, node: 'BaseNode', max_core_upload_limit: int): # noqa: F821
self.node = node
self.log = SDCMAdapter(node.log, extra={"prefix": self.__class__.__name__})
self.max_core_upload_limit = max_core_upload_limit
Expand Down Expand Up @@ -500,7 +500,7 @@ class CoredumpExportFileThread(CoredumpThreadBase):
"""
checkup_time_core_to_complete = 1

def __init__(self, node: 'BaseNode', max_core_upload_limit: int, coredump_directories: List[str]):
def __init__(self, node: 'BaseNode', max_core_upload_limit: int, coredump_directories: List[str]): # noqa: F821
self.coredumps_directories = coredump_directories
super().__init__(node=node, max_core_upload_limit=max_core_upload_limit)

Expand Down
2 changes: 1 addition & 1 deletion sdcm/node_exporter_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class NodeExporterSetup: # pylint: disable=too-few-public-methods
@staticmethod
def install(node: "BaseNode | None" = None, remoter: "Remoter | None" = None):
def install(node: "BaseNode | None" = None, remoter: "Remoter | None" = None): # noqa: F821
assert node or remoter, "node or remoter much be pass to this function"
if node:
remoter = node.remoter
Expand Down
6 changes: 3 additions & 3 deletions sdcm/provision/aws/instance_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ class AWSInstanceParams(InstanceParamsBase):
def dict(
self,
*,
include: Union['AbstractSetIntStr', 'MappingIntStrAny'] = None,
exclude: Union['AbstractSetIntStr', 'MappingIntStrAny'] = None,
include: Union['AbstractSetIntStr', 'MappingIntStrAny'] = None, # noqa: F821
exclude: Union['AbstractSetIntStr', 'MappingIntStrAny'] = None, # noqa: F821
by_alias: bool = False,
skip_defaults: bool = None,
exclude_unset: bool = False,
exclude_defaults: bool = False,
exclude_none: bool = False,
encode_user_data: bool = False
) -> 'DictStrAny':
) -> 'DictStrAny': # noqa: F821
dict_data = super().dict(
include=include,
exclude=exclude,
Expand Down
6 changes: 3 additions & 3 deletions sdcm/provision/common/builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ def _exclude_by_default(self):
def dict(
self,
*,
include: Union['MappingIntStrAny', 'AbstractSetIntStr'] = None,
exclude: Union['MappingIntStrAny', 'AbstractSetIntStr'] = None,
include: Union['MappingIntStrAny', 'AbstractSetIntStr'] = None, # noqa: F821
exclude: Union['MappingIntStrAny', 'AbstractSetIntStr'] = None, # noqa: F821
by_alias: bool = False,
skip_defaults: bool = None,
exclude_unset: bool = False,
exclude_defaults: bool = False,
exclude_none: bool = False,
) -> 'DictStrAny':
) -> 'DictStrAny': # noqa: F821
"""
Pydantic does not treat properties as fields, so you can't get their values when call dict
This function is to enable property extraction
Expand Down
8 changes: 4 additions & 4 deletions sdcm/provision/scylla_yaml/scylla_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,15 +359,15 @@ def set_authorizer(cls, authorizer: str):
def dict( # pylint: disable=arguments-differ
self,
*,
include: Union['MappingIntStrAny', 'AbstractSetIntStr'] = None,
exclude: Union['MappingIntStrAny', 'AbstractSetIntStr'] = None,
include: Union['MappingIntStrAny', 'AbstractSetIntStr'] = None, # noqa: F821
exclude: Union['MappingIntStrAny', 'AbstractSetIntStr'] = None, # noqa: F821
by_alias: bool = False,
skip_defaults: bool = None,
exclude_defaults: bool = False,
exclude_none: bool = False,
exclude_unset: bool = False,
explicit: Union['AbstractSetIntStr', 'MappingIntStrAny'] = None,
) -> 'DictStrAny':
explicit: Union['AbstractSetIntStr', 'MappingIntStrAny'] = None, # noqa: F821
) -> 'DictStrAny': # noqa: F821
to_dict = super().dict(
include=include, exclude=exclude, by_alias=by_alias, skip_defaults=skip_defaults,
exclude_unset=exclude_unset, exclude_defaults=exclude_defaults, exclude_none=exclude_none)
Expand Down
2 changes: 1 addition & 1 deletion sdcm/sct_events/handlers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ class EventHandler(abc.ABC):
def __init__(self):
"""Initializes event handler"""

def handle(self, event: SctEvent, tester_obj: "sdcm.tester.ClusterTester"):
def handle(self, event: SctEvent, tester_obj: "sdcm.tester.ClusterTester"): # noqa: F821
"""Handler main function"""
raise NotImplementedError()
2 changes: 1 addition & 1 deletion sdcm/sct_events/handlers/schema_disagreement.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def _should_upload_sstables(self):
# upload sstables once per hour as usually these events come multiple times for some period of time
return time.time() - 3600 > self._sstables_uploaded_time

def handle(self, event: CassandraStressLogEvent, tester_obj: "sdcm.tester.ClusterTester"):
def handle(self, event: CassandraStressLogEvent, tester_obj: "sdcm.tester.ClusterTester"): # noqa: F821
if self._should_upload_sstables():
self._sstables_uploaded_time = time.time()
# create new event with details to make them visible in error events log/argus
Expand Down
2 changes: 1 addition & 1 deletion sdcm/snitch_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
class SnitchConfig: # pylint: disable=too-few-public-methods
"""Keeps all cassandra-rackdc.properties settings and function to apply them"""

def __init__(self, node: "sdcm.cluster.BaseNode", datacenters: List[str]):
def __init__(self, node: "sdcm.cluster.BaseNode", datacenters: List[str]): # noqa: F821
self._node = node
self._is_multi_dc = len(datacenters) > 1
self._rack = f"RACK{node.rack}"
Expand Down
5 changes: 3 additions & 2 deletions sdcm/utils/adaptive_timeouts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,15 @@ class Operations(Enum):

class TestInfoServices: # pylint: disable=too-few-public-methods
@staticmethod
def get(node: "BaseNode") -> dict:
def get(node: "BaseNode") -> dict: # noqa: F821
return dict(
n_db_nodes=len(node.parent_cluster.nodes),
)


@contextmanager
def adaptive_timeout(operation: Operations, node: "BaseNode", stats_storage: AdaptiveTimeoutStore = ESAdaptiveTimeoutStore(), **kwargs):
def adaptive_timeout(operation: Operations, node: "BaseNode", # noqa: F821
stats_storage: AdaptiveTimeoutStore = ESAdaptiveTimeoutStore(), **kwargs):
"""
Calculate timeout in seconds for given operation based on node load info and return its value.
Upon exit, verify if timeout occurred and publish SoftTimeoutEvent if happened.
Expand Down
2 changes: 1 addition & 1 deletion sdcm/utils/adaptive_timeouts/load_info_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ class NodeLoadInfoServices(metaclass=Singleton): # pylint: disable=too-few-publ
def __init__(self):
self._services: dict[str, NodeLoadInfoService] = {}

def get(self, node: "BaseNode") -> NodeLoadInfoService:
def get(self, node: "BaseNode") -> NodeLoadInfoService: # noqa: F821
if node not in self._services:
self._services[node.name] = NodeLoadInfoService(node.remoter, node.name, node.scylla_version_detailed)
if self._services[node.name].remoter != node.remoter:
Expand Down
8 changes: 4 additions & 4 deletions sdcm/utils/benchmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class ScyllaClusterBenchmarkManager(metaclass=Singleton):
"""

def __init__(self, global_compare: bool = False):
self._nodes: list["BaseNode"] = []
self._nodes: list["BaseNode"] = [] # noqa: F821
self._benchmark_runners: list[ScyllaNodeBenchmarkRunner] = []
self._es = ES()
self._comparison = {}
Expand All @@ -101,13 +101,13 @@ def __init__(self, global_compare: bool = False):
def comparison(self):
return self._comparison

def add_node(self, new_node: "BaseNode"):
def add_node(self, new_node: "BaseNode"): # noqa: F821
if new_node.distro.is_debian_like:
self._benchmark_runners.append(ScyllaNodeBenchmarkRunner(new_node))
else:
LOGGER.debug("Skipped installing benchmarking tools on a non-debian-like distro.")

def add_nodes(self, nodes: list["BaseNode"]):
def add_nodes(self, nodes: list["BaseNode"]): # noqa: F821
for node in nodes:
self.add_node(node)

Expand Down Expand Up @@ -256,7 +256,7 @@ class ScyllaNodeBenchmarkRunner:
tools on given cluster nodes and collects the output.
"""

def __init__(self, node: "BaseNode"):
def __init__(self, node: "BaseNode"): # noqa: F821
self._node = node
self._remoter: RemoteCmdRunnerBase = node.remoter
self.node_instance_type = self._get_db_node_instance_type()
Expand Down
6 changes: 3 additions & 3 deletions sdcm/utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1118,9 +1118,9 @@ def list_clusters(self) -> list: # pylint: disable=no-self-use
return clusters


def filter_k8s_clusters_by_tags(tags_dict: dict,
clusters: list[Union["EksClusterForCleaner", "GkeClusterForCleaner"]]) -> list[
Union["EksClusterForCleaner", "GkeClusterForCleaner"]]:
def filter_k8s_clusters_by_tags(tags_dict: dict, clusters: list[
Union["EksClusterForCleaner", "GkeClusterForCleaner"]]) -> list[ # noqa: F821
Union["EksClusterForCleaner", "GkeClusterForCleaner"]]: # noqa: F821
if "NodeType" in tags_dict and "k8s" not in tags_dict.get("NodeType"):
return []

Expand Down
5 changes: 3 additions & 2 deletions sdcm/utils/k8s/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,15 +300,16 @@ def kubectl_cmd(kluster, *command, namespace=None, ignore_k8s_server_url=False):

@classmethod
def kubectl(cls, kluster, *command, namespace: Optional[str] = None, timeout: int = KUBECTL_TIMEOUT,
remoter: Optional['KubernetesCmdRunner'] = None, ignore_status: bool = False, verbose: bool = True):
remoter: Optional['KubernetesCmdRunner'] = None, # noqa: F821
ignore_status: bool = False, verbose: bool = True):
cmd = cls.kubectl_cmd(kluster, *command, namespace=namespace, ignore_k8s_server_url=bool(remoter))
if remoter is None:
remoter = LOCALRUNNER
return remoter.run(cmd, timeout=timeout, ignore_status=ignore_status, verbose=verbose)

@classmethod
def kubectl_multi_cmd(cls, kluster, *command, namespace: Optional[str] = None, timeout: int = KUBECTL_TIMEOUT,
remoter: Optional['KubernetesCmdRunner'] = None, ignore_status: bool = False,
remoter: Optional['KubernetesCmdRunner'] = None, ignore_status: bool = False, # noqa: F821
verbose: bool = True):
total_command = ' '.join(command)
final_command = []
Expand Down
21 changes: 11 additions & 10 deletions sdcm/utils/k8s/chaos_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class ChaosMesh: # pylint: disable=too-few-public-methods
}
ADDITIONAL_CONFIGS = sct_abs_path("sdcm/k8s_configs/chaos-mesh/gke-auth-workaround.yaml")

def __init__(self, k8s_cluster: "sdcm.cluster_k8s.KubernetesCluster"):
def __init__(self, k8s_cluster: "sdcm.cluster_k8s.KubernetesCluster"): # noqa: F821
self._k8s_cluster = k8s_cluster
self.log = SDCMAdapter(LOGGER, extra={'prefix': k8s_cluster.region_name})
self.initialized = False
Expand Down Expand Up @@ -125,7 +125,7 @@ class ChaosMeshExperiment:
API_VERSION = "chaos-mesh.org/v1alpha1"
CHAOS_KIND = "" # need to override it in child classes

def __init__(self, pod: "sdcm.cluster_k8s.BasePodContainer", name: str, timeout: int = 0):
def __init__(self, pod: "sdcm.cluster_k8s.BasePodContainer", name: str, timeout: int = 0): # noqa: F821
self._k8s_cluster = pod.k8s_cluster
self._name = name
self._namespace = pod.parent_cluster.namespace
Expand Down Expand Up @@ -217,7 +217,7 @@ class PodFailureExperiment(ChaosMeshExperiment):
"""
CHAOS_KIND = "PodChaos"

def __init__(self, pod: "sdcm.cluster_k8s.BasePodContainer", duration: str):
def __init__(self, pod: "sdcm.cluster_k8s.BasePodContainer", duration: str): # noqa: F821
"""Injects fault into a specified Pod to make the Pod unavailable for a period of time.
:param sdcm.cluster_k8s.BasePodContainer pod: affected scylla pod
Expand All @@ -242,7 +242,8 @@ class MemoryStressExperiment(ChaosMeshExperiment):
CHAOS_KIND = "StressChaos"

# pylint: disable=too-many-arguments
def __init__(self, pod: "sdcm.cluster_k8s.BasePodContainer", duration: str, workers: int, size: str, time_to_reach: str | None = None):
def __init__(self, pod: "sdcm.cluster_k8s.BasePodContainer", duration: str, # noqa: F821
workers: int, size: str, time_to_reach: str | None = None):
"""Stresses memory on scylla pod using https://github.com/chaos-mesh/memStress
:param sdcm.cluster_k8s.BasePodContainer pod: affected scylla pod
Expand Down Expand Up @@ -306,8 +307,8 @@ class IOFaultChaosExperiment(ChaosMeshExperiment):
CHAOS_KIND = "IOChaos"

# pylint: disable=too-many-arguments
def __init__(self, pod: "sdcm.cluster_k8s.BasePodContainer", duration: str, error: DiskError, error_probability: int,
methods: list[DiskMethod], volume_path: str, path: str | None = None):
def __init__(self, pod: "sdcm.cluster_k8s.BasePodContainer", duration: str, error: DiskError, # noqa: F821
error_probability: int, methods: list[DiskMethod], volume_path: str, path: str | None = None):
"""Induces disk fault (programatically) using IOChaos: https://chaos-mesh.org/docs/simulate-io-chaos-on-kubernetes/
:param sdcm.cluster_k8s.BasePodContainer pod: affected scylla pod
Expand Down Expand Up @@ -345,7 +346,7 @@ class NetworkPacketLossExperiment(ChaosMeshExperiment):
"""
CHAOS_KIND = "NetworkChaos"

def __init__(self, pod: "sdcm.cluster_k8s.BasePodContainer", duration: str,
def __init__(self, pod: "sdcm.cluster_k8s.BasePodContainer", duration: str, # noqa: F821
probability: int = 0, correlation: int = 0):
"""Simulate packet loss fault into a specified Pod for a period of time.
Expand Down Expand Up @@ -377,7 +378,7 @@ class NetworkCorruptExperiment(ChaosMeshExperiment):
"""
CHAOS_KIND = "NetworkChaos"

def __init__(self, pod: "sdcm.cluster_k8s.BasePodContainer", duration: str,
def __init__(self, pod: "sdcm.cluster_k8s.BasePodContainer", duration: str, # noqa: F821
probability: int = 0, correlation: int = 0):
"""Simulate network corrupt fault into a specified Pod for a period of time.
Expand Down Expand Up @@ -410,7 +411,7 @@ class NetworkDelayExperiment(ChaosMeshExperiment):
CHAOS_KIND = "NetworkChaos"

# pylint: disable=too-many-arguments
def __init__(self, pod: "sdcm.cluster_k8s.BasePodContainer", duration: str, latency: str,
def __init__(self, pod: "sdcm.cluster_k8s.BasePodContainer", duration: str, latency: str, # noqa: F821
correlation: int = 0, jitter: str = "0"):
"""Simulate network delay fault into a specified Pod for a period of time.
Expand Down Expand Up @@ -444,7 +445,7 @@ class NetworkBandwidthLimitExperiment(ChaosMeshExperiment):
CHAOS_KIND = "NetworkChaos"

# pylint: disable=too-many-arguments
def __init__(self, pod: "sdcm.cluster_k8s.BasePodContainer", duration: str,
def __init__(self, pod: "sdcm.cluster_k8s.BasePodContainer", duration: str, # noqa: F821
rate: str, limit: int, buffer: int):
"""Simulate network bandwidth limit fault into a specified Pod for a period of time.
Expand Down
6 changes: 3 additions & 3 deletions sdcm/utils/raft/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class MessageTimeout(NamedTuple):


class RaftFeatureOperations(Protocol):
_node: "BaseNode"
_node: "BaseNode" # noqa: F821
TOPOLOGY_OPERATION_LOG_PATTERNS: dict[TopologyOperations, Iterable[MessagePosition]]

@property
Expand Down Expand Up @@ -119,7 +119,7 @@ class RaftFeature(RaftFeatureOperations):
TopologyOperations.BOOTSTRAP: ABORT_BOOTSTRAP_LOG_PATTERNS,
}

def __init__(self, node: "BaseNode") -> None:
def __init__(self, node: "BaseNode") -> None: # noqa: F821
super().__init__()
self._node = node

Expand Down Expand Up @@ -284,7 +284,7 @@ class NoRaft(RaftFeatureOperations):
]
}

def __init__(self, node: "BaseNode") -> None:
def __init__(self, node: "BaseNode") -> None: # noqa: F821
super().__init__()
self._node = node

Expand Down
2 changes: 1 addition & 1 deletion sdcm/utils/remote_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ class K8sClientLogger(LoggerBase): # pylint: disable=too-many-instance-attribut
RECONNECT_DELAY = 30
CHUNK_SIZE = 64

def __init__(self, pod: "sdcm.cluster_k8s.BasePodContainer", target_log_file: str):
def __init__(self, pod: "sdcm.cluster_k8s.BasePodContainer", target_log_file: str): # noqa: F821
"""
Reads logs from a k8s pod using k8s client API and forwards it to a file.
Expand Down
3 changes: 2 additions & 1 deletion sdcm/utils/sstable/sstable_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class SstableUtils:
"""

# pylint: disable=too-many-instance-attributes
def __init__(self, propagation_delay_in_seconds: int = 0, ks_cf: str = None, db_node: 'BaseNode' = None,
def __init__(self, propagation_delay_in_seconds: int = 0, ks_cf: str = None,
db_node: 'BaseNode' = None, # noqa: F821
**kwargs):
self.db_node = db_node
self.db_cluster = self.db_node.parent_cluster if self.db_node else None
Expand Down
Loading

0 comments on commit 30aee48

Please sign in to comment.