Skip to content

Commit a64910d

Browse files
[MISC] Port MySQL v0 libs to Python 3.10+ (#660)
1 parent 1096b9a commit a64910d

File tree

5 files changed

+117
-126
lines changed

5 files changed

+117
-126
lines changed

lib/charms/mysql/v0/async_replication.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
)
3131
from ops.framework import Object
3232
from tenacity import RetryError, Retrying, stop_after_attempt, wait_fixed
33-
from typing_extensions import Optional
3433

3534
from constants import (
3635
BACKUPS_PASSWORD_KEY,
@@ -54,7 +53,7 @@
5453
# The unique Charmhub library identifier, never change it
5554
LIBID = "4de21f1a022c4e2c87ac8e672ec16f6a"
5655
LIBAPI = 0
57-
LIBPATCH = 8
56+
LIBPATCH = 9
5857

5958
RELATION_OFFER = "replication-offer"
6059
RELATION_CONSUMER = "replication"
@@ -126,21 +125,21 @@ def cluster_set_name(self) -> str:
126125
return self._charm.app_peer_data["cluster-set-domain-name"]
127126

128127
@property
129-
def relation(self) -> Optional[Relation]:
128+
def relation(self) -> Relation | None:
130129
"""Relation."""
131130
return self.model.get_relation(RELATION_OFFER) or self.model.get_relation(
132131
RELATION_CONSUMER
133132
)
134133

135134
@property
136-
def relation_data(self) -> Optional[RelationDataContent]:
135+
def relation_data(self) -> RelationDataContent | None:
137136
"""Relation data."""
138137
if not self.relation:
139138
return
140139
return self.relation.data[self.model.app]
141140

142141
@property
143-
def remote_relation_data(self) -> Optional[RelationDataContent]:
142+
def remote_relation_data(self) -> RelationDataContent | None:
144143
"""Remote relation data."""
145144
if not self.relation or not self.relation.app:
146145
return
@@ -362,7 +361,7 @@ def __init__(self, charm: "MySQLOperatorCharm"):
362361
self.framework.observe(self._charm.on.secret_changed, self._on_secret_change)
363362

364363
@property
365-
def state(self) -> Optional[States]:
364+
def state(self) -> States | None:
366365
"""State of the relation, on primary side."""
367366
if not self.relation:
368367
return States.UNINITIALIZED
@@ -413,7 +412,7 @@ def idle(self) -> bool:
413412
return True
414413

415414
@property
416-
def secret(self) -> Optional[Secret]:
415+
def secret(self) -> Secret | None:
417416
"""Return the async replication secret."""
418417
if not self.relation:
419418
return
@@ -642,7 +641,7 @@ def __init__(self, charm: "MySQLOperatorCharm"):
642641
self.framework.observe(self._charm.on.secret_changed, self._on_secret_change)
643642

644643
@property
645-
def state(self) -> Optional[States]:
644+
def state(self) -> States | None:
646645
"""State of the relation, on consumer side."""
647646
if not self.relation:
648647
return None
@@ -719,7 +718,7 @@ def _async_replication_credentials(self) -> dict[str, str]:
719718
secret = self._obtain_secret()
720719
return secret.peek_content()
721720

722-
def _get_endpoint(self) -> Optional[str]:
721+
def _get_endpoint(self) -> str | None:
723722
"""Get endpoint to be used by the primary cluster.
724723
725724
This is the address in which the unit must be reachable from the primary cluster.

lib/charms/mysql/v0/backups.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ def is_unit_blocked(self) -> bool:
5050
import pathlib
5151
import re
5252
import typing
53-
from typing import Dict, List, Optional, Tuple
5453

5554
from charms.data_platform_libs.v0.s3 import (
5655
CredentialsChangedEvent,
@@ -113,7 +112,7 @@ def is_unit_blocked(self) -> bool:
113112

114113
# Increment this PATCH version before using `charmcraft publish-lib` or reset
115114
# to 0 if you are raising the major API version
116-
LIBPATCH = 14
115+
LIBPATCH = 15
117116

118117
ANOTHER_S3_CLUSTER_REPOSITORY_ERROR_MESSAGE = "S3 repository claimed by another cluster"
119118
MOVE_RESTORED_CLUSTER_TO_ANOTHER_S3_REPOSITORY_ERROR = (
@@ -150,7 +149,7 @@ def _s3_integrator_relation_exists(self) -> bool:
150149
"""Returns whether a relation with the s3-integrator exists."""
151150
return bool(self.model.get_relation(S3_INTEGRATOR_RELATION_NAME))
152151

153-
def _retrieve_s3_parameters(self) -> Tuple[Dict[str, str], List[str]]:
152+
def _retrieve_s3_parameters(self) -> tuple[dict[str, str], list[str]]:
154153
"""Retrieve S3 parameters from the S3 integrator relation.
155154
156155
Returns: tuple of (s3_parameters, missing_required_parameters)
@@ -196,7 +195,7 @@ def _upload_logs_to_s3(
196195
stdout: str,
197196
stderr: str,
198197
log_filename: str,
199-
s3_parameters: Dict[str, str],
198+
s3_parameters: dict[str, str],
200199
) -> bool:
201200
"""Upload logs to S3 at the specified location.
202201
@@ -219,7 +218,7 @@ def _upload_logs_to_s3(
219218
# ------------------ List Backups ------------------
220219

221220
@staticmethod
222-
def _format_backups_list(backup_list: List[Tuple[str, str]]) -> str:
221+
def _format_backups_list(backup_list: list[tuple[str, str]]) -> str:
223222
"""Formats the provided list of backups as a table."""
224223
backups = [f"{'backup-id':<21} | {'backup-type':<12} | backup-status"]
225224

@@ -359,7 +358,7 @@ def _on_create_backup(self, event: ActionEvent) -> None:
359358
})
360359
self.charm._on_update_status(None)
361360

362-
def _can_unit_perform_backup(self) -> Tuple[bool, Optional[str]]:
361+
def _can_unit_perform_backup(self) -> tuple[bool, str | None]:
363362
"""Validates whether this unit can perform a backup.
364363
365364
Returns: tuple of (success, error_message)
@@ -390,7 +389,7 @@ def _can_unit_perform_backup(self) -> Tuple[bool, Optional[str]]:
390389

391390
return True, None
392391

393-
def _pre_backup(self) -> Tuple[bool, Optional[str]]:
392+
def _pre_backup(self) -> tuple[bool, str | None]:
394393
"""Runs operations required before performing a backup.
395394
396395
Returns: tuple of (success, error_message)
@@ -415,7 +414,7 @@ def _pre_backup(self) -> Tuple[bool, Optional[str]]:
415414

416415
return True, None
417416

418-
def _backup(self, backup_path: str, s3_parameters: Dict) -> Tuple[bool, Optional[str]]:
417+
def _backup(self, backup_path: str, s3_parameters: dict) -> tuple[bool, str | None]:
419418
"""Runs the backup operations.
420419
421420
Args:
@@ -450,7 +449,7 @@ def _backup(self, backup_path: str, s3_parameters: Dict) -> Tuple[bool, Optional
450449

451450
return True, None
452451

453-
def _post_backup(self) -> Tuple[bool, Optional[str]]:
452+
def _post_backup(self) -> tuple[bool, str | None]:
454453
"""Runs operations required after performing a backup.
455454
456455
Returns: tuple of (success, error_message)
@@ -613,7 +612,7 @@ def _on_restore(self, event: ActionEvent) -> None: # noqa: C901
613612
# update status as soon as possible
614613
self.charm._on_update_status(None)
615614

616-
def _pre_restore(self) -> Tuple[bool, str]:
615+
def _pre_restore(self) -> tuple[bool, str]:
617616
"""Perform operations that need to be done before performing a restore.
618617
619618
Returns: tuple of (success, error_message)
@@ -635,7 +634,7 @@ def _pre_restore(self) -> Tuple[bool, str]:
635634

636635
return True, ""
637636

638-
def _restore(self, backup_id: str, s3_parameters: Dict[str, str]) -> Tuple[bool, bool, str]:
637+
def _restore(self, backup_id: str, s3_parameters: dict[str, str]) -> tuple[bool, bool, str]:
639638
"""Run the restore operations.
640639
641640
Args:
@@ -687,7 +686,7 @@ def _restore(self, backup_id: str, s3_parameters: Dict[str, str]) -> Tuple[bool,
687686

688687
return True, True, ""
689688

690-
def _clean_data_dir_and_start_mysqld(self) -> Tuple[bool, str]:
689+
def _clean_data_dir_and_start_mysqld(self) -> tuple[bool, str]:
691690
"""Run idempotent operations run after restoring a backup.
692691
693692
Returns tuple of (success, error_message)
@@ -711,8 +710,8 @@ def _clean_data_dir_and_start_mysqld(self) -> Tuple[bool, str]:
711710
return True, ""
712711

713712
def _pitr_restore(
714-
self, restore_to_time: str, s3_parameters: Dict[str, str]
715-
) -> Tuple[bool, str]:
713+
self, restore_to_time: str, s3_parameters: dict[str, str]
714+
) -> tuple[bool, str]:
716715
try:
717716
logger.info("Restoring point-in-time-recovery")
718717
stdout, stderr = self.charm._mysql.restore_pitr(
@@ -728,7 +727,7 @@ def _pitr_restore(
728727
return False, f"Failed to restore point-in-time-recovery to the {restore_to_time}"
729728
return True, ""
730729

731-
def _post_restore(self) -> Tuple[bool, str]:
730+
def _post_restore(self) -> tuple[bool, str]:
732731
"""Run operations required after restoring a backup.
733732
734733
Returns: tuple of (success, error_message)
@@ -836,7 +835,7 @@ def _on_s3_credentials_gone(self, event: CredentialsGoneEvent) -> None:
836835
"Exception is occurred when trying to stop binlogs collecting after S3 relation depart. It may be a leader departure"
837836
)
838837

839-
def get_binlogs_collector_config(self) -> Dict[str, str]:
838+
def get_binlogs_collector_config(self) -> dict[str, str]:
840839
"""Return binlogs collector service config file.
841840
842841
Returns: dict of binlogs collector service config

0 commit comments

Comments
 (0)