Drop superfluous type annotation of 'self'

See https://mypy.readthedocs.io/en/stable/cheat_sheet_py3.html#classes
> For instance methods, omit type for "self"
This commit is contained in:
Denis Laxalde 2023-10-02 13:44:49 +02:00 committed by Denis Laxalde
parent de8b3daa7a
commit 95f21a133d
3 changed files with 43 additions and 65 deletions

View file

@ -14,7 +14,7 @@ def replace_chars(text: str) -> str:
class ClusterNodeCount(PatroniResource): class ClusterNodeCount(PatroniResource):
def probe(self: "ClusterNodeCount") -> Iterable[nagiosplugin.Metric]: def probe(self) -> Iterable[nagiosplugin.Metric]:
item_dict = self.rest_api("cluster") item_dict = self.rest_api("cluster")
role_counters: Counter[str] = Counter() role_counters: Counter[str] = Counter()
roles = [] roles = []
@ -48,7 +48,7 @@ class ClusterNodeCount(PatroniResource):
class ClusterHasLeader(PatroniResource): class ClusterHasLeader(PatroniResource):
def probe(self: "ClusterHasLeader") -> Iterable[nagiosplugin.Metric]: def probe(self) -> Iterable[nagiosplugin.Metric]:
item_dict = self.rest_api("cluster") item_dict = self.rest_api("cluster")
is_leader_found = False is_leader_found = False
@ -69,24 +69,20 @@ class ClusterHasLeader(PatroniResource):
class ClusterHasLeaderSummary(nagiosplugin.Summary): class ClusterHasLeaderSummary(nagiosplugin.Summary):
def ok(self: "ClusterHasLeaderSummary", results: nagiosplugin.Result) -> str: def ok(self, results: nagiosplugin.Result) -> str:
return "The cluster has a running leader." return "The cluster has a running leader."
@handle_unknown @handle_unknown
def problem(self: "ClusterHasLeaderSummary", results: nagiosplugin.Result) -> str: def problem(self, results: nagiosplugin.Result) -> str:
return "The cluster has no running leader." return "The cluster has no running leader."
class ClusterHasReplica(PatroniResource): class ClusterHasReplica(PatroniResource):
def __init__( def __init__(self, connection_info: ConnectionInfo, max_lag: Union[int, None]):
self: "ClusterHasReplica",
connection_info: ConnectionInfo,
max_lag: Union[int, None],
):
super().__init__(connection_info) super().__init__(connection_info)
self.max_lag = max_lag self.max_lag = max_lag
def probe(self: "ClusterHasReplica") -> Iterable[nagiosplugin.Metric]: def probe(self) -> Iterable[nagiosplugin.Metric]:
item_dict = self.rest_api("cluster") item_dict = self.rest_api("cluster")
replicas = [] replicas = []
@ -140,7 +136,7 @@ class ClusterHasReplica(PatroniResource):
class ClusterConfigHasChanged(PatroniResource): class ClusterConfigHasChanged(PatroniResource):
def __init__( def __init__(
self: "ClusterConfigHasChanged", self,
connection_info: ConnectionInfo, connection_info: ConnectionInfo,
config_hash: str, # Always contains the old hash config_hash: str, # Always contains the old hash
state_file: str, # Only used to update the hash in the state_file (when needed) state_file: str, # Only used to update the hash in the state_file (when needed)
@ -151,7 +147,7 @@ class ClusterConfigHasChanged(PatroniResource):
self.config_hash = config_hash self.config_hash = config_hash
self.save = save self.save = save
def probe(self: "ClusterConfigHasChanged") -> Iterable[nagiosplugin.Metric]: def probe(self) -> Iterable[nagiosplugin.Metric]:
item_dict = self.rest_api("config") item_dict = self.rest_api("config")
new_hash = hashlib.md5(json.dumps(item_dict).encode()).hexdigest() new_hash = hashlib.md5(json.dumps(item_dict).encode()).hexdigest()
@ -183,23 +179,21 @@ class ClusterConfigHasChanged(PatroniResource):
class ClusterConfigHasChangedSummary(nagiosplugin.Summary): class ClusterConfigHasChangedSummary(nagiosplugin.Summary):
def __init__(self: "ClusterConfigHasChangedSummary", config_hash: str) -> None: def __init__(self, config_hash: str) -> None:
self.old_config_hash = config_hash self.old_config_hash = config_hash
# Note: It would be helpful to display the old / new hash here. Unfortunately, it's not a metric. # Note: It would be helpful to display the old / new hash here. Unfortunately, it's not a metric.
# So we only have the old / expected one. # So we only have the old / expected one.
def ok(self: "ClusterConfigHasChangedSummary", results: nagiosplugin.Result) -> str: def ok(self, results: nagiosplugin.Result) -> str:
return f"The hash of patroni's dynamic configuration has not changed ({self.old_config_hash})." return f"The hash of patroni's dynamic configuration has not changed ({self.old_config_hash})."
@handle_unknown @handle_unknown
def problem( def problem(self, results: nagiosplugin.Result) -> str:
self: "ClusterConfigHasChangedSummary", results: nagiosplugin.Result
) -> str:
return f"The hash of patroni's dynamic configuration has changed. The old hash was {self.old_config_hash}." return f"The hash of patroni's dynamic configuration has changed. The old hash was {self.old_config_hash}."
class ClusterIsInMaintenance(PatroniResource): class ClusterIsInMaintenance(PatroniResource):
def probe(self: "ClusterIsInMaintenance") -> Iterable[nagiosplugin.Metric]: def probe(self) -> Iterable[nagiosplugin.Metric]:
item_dict = self.rest_api("cluster") item_dict = self.rest_api("cluster")
# The actual check # The actual check
@ -212,7 +206,7 @@ class ClusterIsInMaintenance(PatroniResource):
class ClusterHasScheduledAction(PatroniResource): class ClusterHasScheduledAction(PatroniResource):
def probe(self: "ClusterIsInMaintenance") -> Iterable[nagiosplugin.Metric]: def probe(self) -> Iterable[nagiosplugin.Metric]:
item_dict = self.rest_api("cluster") item_dict = self.rest_api("cluster")
scheduled_switchover = 0 scheduled_switchover = 0

View file

@ -7,7 +7,7 @@ from .types import APIError, ConnectionInfo, PatroniResource, handle_unknown
class NodeIsPrimary(PatroniResource): class NodeIsPrimary(PatroniResource):
def probe(self: "NodeIsPrimary") -> Iterable[nagiosplugin.Metric]: def probe(self) -> Iterable[nagiosplugin.Metric]:
try: try:
self.rest_api("primary") self.rest_api("primary")
except APIError: except APIError:
@ -16,24 +16,22 @@ class NodeIsPrimary(PatroniResource):
class NodeIsPrimarySummary(nagiosplugin.Summary): class NodeIsPrimarySummary(nagiosplugin.Summary):
def ok(self: "NodeIsPrimarySummary", results: nagiosplugin.Result) -> str: def ok(self, results: nagiosplugin.Result) -> str:
return "This node is the primary with the leader lock." return "This node is the primary with the leader lock."
@handle_unknown @handle_unknown
def problem(self: "NodeIsPrimarySummary", results: nagiosplugin.Result) -> str: def problem(self, results: nagiosplugin.Result) -> str:
return "This node is not the primary with the leader lock." return "This node is not the primary with the leader lock."
class NodeIsLeader(PatroniResource): class NodeIsLeader(PatroniResource):
def __init__( def __init__(
self: "NodeIsLeader", self, connection_info: ConnectionInfo, check_is_standby_leader: bool
connection_info: ConnectionInfo,
check_is_standby_leader: bool,
) -> None: ) -> None:
super().__init__(connection_info) super().__init__(connection_info)
self.check_is_standby_leader = check_is_standby_leader self.check_is_standby_leader = check_is_standby_leader
def probe(self: "NodeIsLeader") -> Iterable[nagiosplugin.Metric]: def probe(self) -> Iterable[nagiosplugin.Metric]:
apiname = "leader" apiname = "leader"
if self.check_is_standby_leader: if self.check_is_standby_leader:
apiname = "standby-leader" apiname = "standby-leader"
@ -46,26 +44,23 @@ class NodeIsLeader(PatroniResource):
class NodeIsLeaderSummary(nagiosplugin.Summary): class NodeIsLeaderSummary(nagiosplugin.Summary):
def __init__( def __init__(self, check_is_standby_leader: bool) -> None:
self: "NodeIsLeaderSummary",
check_is_standby_leader: bool,
) -> None:
if check_is_standby_leader: if check_is_standby_leader:
self.leader_kind = "standby leader" self.leader_kind = "standby leader"
else: else:
self.leader_kind = "leader" self.leader_kind = "leader"
def ok(self: "NodeIsLeaderSummary", results: nagiosplugin.Result) -> str: def ok(self, results: nagiosplugin.Result) -> str:
return f"This node is a {self.leader_kind} node." return f"This node is a {self.leader_kind} node."
@handle_unknown @handle_unknown
def problem(self: "NodeIsLeaderSummary", results: nagiosplugin.Result) -> str: def problem(self, results: nagiosplugin.Result) -> str:
return f"This node is not a {self.leader_kind} node." return f"This node is not a {self.leader_kind} node."
class NodeIsReplica(PatroniResource): class NodeIsReplica(PatroniResource):
def __init__( def __init__(
self: "NodeIsReplica", self,
connection_info: ConnectionInfo, connection_info: ConnectionInfo,
max_lag: str, max_lag: str,
check_is_sync: bool, check_is_sync: bool,
@ -76,7 +71,7 @@ class NodeIsReplica(PatroniResource):
self.check_is_sync = check_is_sync self.check_is_sync = check_is_sync
self.check_is_async = check_is_async self.check_is_async = check_is_async
def probe(self: "NodeIsReplica") -> Iterable[nagiosplugin.Metric]: def probe(self) -> Iterable[nagiosplugin.Metric]:
try: try:
if self.check_is_sync: if self.check_is_sync:
api_name = "synchronous" api_name = "synchronous"
@ -95,12 +90,7 @@ class NodeIsReplica(PatroniResource):
class NodeIsReplicaSummary(nagiosplugin.Summary): class NodeIsReplicaSummary(nagiosplugin.Summary):
def __init__( def __init__(self, lag: str, check_is_sync: bool, check_is_async: bool) -> None:
self: "NodeIsReplicaSummary",
lag: str,
check_is_sync: bool,
check_is_async: bool,
) -> None:
self.lag = lag self.lag = lag
if check_is_sync: if check_is_sync:
self.replica_kind = "synchronous replica" self.replica_kind = "synchronous replica"
@ -109,7 +99,7 @@ class NodeIsReplicaSummary(nagiosplugin.Summary):
else: else:
self.replica_kind = "replica" self.replica_kind = "replica"
def ok(self: "NodeIsReplicaSummary", results: nagiosplugin.Result) -> str: def ok(self, results: nagiosplugin.Result) -> str:
if self.lag is None: if self.lag is None:
return ( return (
f"This node is a running {self.replica_kind} with no noloadbalance tag." f"This node is a running {self.replica_kind} with no noloadbalance tag."
@ -117,14 +107,14 @@ class NodeIsReplicaSummary(nagiosplugin.Summary):
return f"This node is a running {self.replica_kind} with no noloadbalance tag and the lag is under {self.lag}." return f"This node is a running {self.replica_kind} with no noloadbalance tag and the lag is under {self.lag}."
@handle_unknown @handle_unknown
def problem(self: "NodeIsReplicaSummary", results: nagiosplugin.Result) -> str: def problem(self, results: nagiosplugin.Result) -> str:
if self.lag is None: if self.lag is None:
return f"This node is not a running {self.replica_kind} with no noloadbalance tag." return f"This node is not a running {self.replica_kind} with no noloadbalance tag."
return f"This node is not a running {self.replica_kind} with no noloadbalance tag and a lag under {self.lag}." return f"This node is not a running {self.replica_kind} with no noloadbalance tag and a lag under {self.lag}."
class NodeIsPendingRestart(PatroniResource): class NodeIsPendingRestart(PatroniResource):
def probe(self: "NodeIsPendingRestart") -> Iterable[nagiosplugin.Metric]: def probe(self) -> Iterable[nagiosplugin.Metric]:
item_dict = self.rest_api("patroni") item_dict = self.rest_api("patroni")
is_pending_restart = item_dict.get("pending_restart", False) is_pending_restart = item_dict.get("pending_restart", False)
@ -137,19 +127,17 @@ class NodeIsPendingRestart(PatroniResource):
class NodeIsPendingRestartSummary(nagiosplugin.Summary): class NodeIsPendingRestartSummary(nagiosplugin.Summary):
def ok(self: "NodeIsPendingRestartSummary", results: nagiosplugin.Result) -> str: def ok(self, results: nagiosplugin.Result) -> str:
return "This node doesn't have the pending restart flag." return "This node doesn't have the pending restart flag."
@handle_unknown @handle_unknown
def problem( def problem(self, results: nagiosplugin.Result) -> str:
self: "NodeIsPendingRestartSummary", results: nagiosplugin.Result
) -> str:
return "This node has the pending restart flag." return "This node has the pending restart flag."
class NodeTLHasChanged(PatroniResource): class NodeTLHasChanged(PatroniResource):
def __init__( def __init__(
self: "NodeTLHasChanged", self,
connection_info: ConnectionInfo, connection_info: ConnectionInfo,
timeline: str, # Always contains the old timeline timeline: str, # Always contains the old timeline
state_file: str, # Only used to update the timeline in the state_file (when needed) state_file: str, # Only used to update the timeline in the state_file (when needed)
@ -160,7 +148,7 @@ class NodeTLHasChanged(PatroniResource):
self.timeline = timeline self.timeline = timeline
self.save = save self.save = save
def probe(self: "NodeTLHasChanged") -> Iterable[nagiosplugin.Metric]: def probe(self) -> Iterable[nagiosplugin.Metric]:
item_dict = self.rest_api("patroni") item_dict = self.rest_api("patroni")
new_tl = item_dict["timeline"] new_tl = item_dict["timeline"]
@ -193,27 +181,23 @@ class NodeTLHasChanged(PatroniResource):
class NodeTLHasChangedSummary(nagiosplugin.Summary): class NodeTLHasChangedSummary(nagiosplugin.Summary):
def __init__(self: "NodeTLHasChangedSummary", timeline: str) -> None: def __init__(self, timeline: str) -> None:
self.timeline = timeline self.timeline = timeline
def ok(self: "NodeTLHasChangedSummary", results: nagiosplugin.Result) -> str: def ok(self, results: nagiosplugin.Result) -> str:
return f"The timeline is still {self.timeline}." return f"The timeline is still {self.timeline}."
@handle_unknown @handle_unknown
def problem(self: "NodeTLHasChangedSummary", results: nagiosplugin.Result) -> str: def problem(self, results: nagiosplugin.Result) -> str:
return f"The expected timeline was {self.timeline} got {results['timeline'].metric}." return f"The expected timeline was {self.timeline} got {results['timeline'].metric}."
class NodePatroniVersion(PatroniResource): class NodePatroniVersion(PatroniResource):
def __init__( def __init__(self, connection_info: ConnectionInfo, patroni_version: str) -> None:
self: "NodePatroniVersion",
connection_info: ConnectionInfo,
patroni_version: str,
) -> None:
super().__init__(connection_info) super().__init__(connection_info)
self.patroni_version = patroni_version self.patroni_version = patroni_version
def probe(self: "NodePatroniVersion") -> Iterable[nagiosplugin.Metric]: def probe(self) -> Iterable[nagiosplugin.Metric]:
item_dict = self.rest_api("patroni") item_dict = self.rest_api("patroni")
version = item_dict["patroni"]["version"] version = item_dict["patroni"]["version"]
@ -232,21 +216,21 @@ class NodePatroniVersion(PatroniResource):
class NodePatroniVersionSummary(nagiosplugin.Summary): class NodePatroniVersionSummary(nagiosplugin.Summary):
def __init__(self: "NodePatroniVersionSummary", patroni_version: str) -> None: def __init__(self, patroni_version: str) -> None:
self.patroni_version = patroni_version self.patroni_version = patroni_version
def ok(self: "NodePatroniVersionSummary", results: nagiosplugin.Result) -> str: def ok(self, results: nagiosplugin.Result) -> str:
return f"Patroni's version is {self.patroni_version}." return f"Patroni's version is {self.patroni_version}."
@handle_unknown @handle_unknown
def problem(self: "NodePatroniVersionSummary", results: nagiosplugin.Result) -> str: def problem(self, results: nagiosplugin.Result) -> str:
# FIXME find a way to make the following work, check is perf data can be strings # FIXME find a way to make the following work, check is perf data can be strings
# return f"The expected patroni version was {self.patroni_version} got {results['patroni_version'].metric}." # return f"The expected patroni version was {self.patroni_version} got {results['patroni_version'].metric}."
return f"Patroni's version is not {self.patroni_version}." return f"Patroni's version is not {self.patroni_version}."
class NodeIsAlive(PatroniResource): class NodeIsAlive(PatroniResource):
def probe(self: "NodeIsAlive") -> Iterable[nagiosplugin.Metric]: def probe(self) -> Iterable[nagiosplugin.Metric]:
try: try:
self.rest_api("liveness") self.rest_api("liveness")
except APIError: except APIError:
@ -255,9 +239,9 @@ class NodeIsAlive(PatroniResource):
class NodeIsAliveSummary(nagiosplugin.Summary): class NodeIsAliveSummary(nagiosplugin.Summary):
def ok(self: "NodeIsAliveSummary", results: nagiosplugin.Result) -> str: def ok(self, results: nagiosplugin.Result) -> str:
return "This node is alive (patroni is running)." return "This node is alive (patroni is running)."
@handle_unknown @handle_unknown
def problem(self: "NodeIsAliveSummary", results: nagiosplugin.Result) -> str: def problem(self, results: nagiosplugin.Result) -> str:
return "This node is not alive (patroni is not running)." return "This node is not alive (patroni is not running)."

View file

@ -32,7 +32,7 @@ class Parameters:
class PatroniResource(nagiosplugin.Resource): class PatroniResource(nagiosplugin.Resource):
conn_info: ConnectionInfo conn_info: ConnectionInfo
def rest_api(self: "PatroniResource", service: str) -> Any: def rest_api(self, service: str) -> Any:
"""Try to connect to all the provided endpoints for the requested service""" """Try to connect to all the provided endpoints for the requested service"""
for endpoint in self.conn_info.endpoints: for endpoint in self.conn_info.endpoints:
cert: Optional[Union[Tuple[str, str], str]] = None cert: Optional[Union[Tuple[str, str], str]] = None