Skip to content

Commit ed2ed83

Browse files
dragomirpclaudemarceloneppel
authored
[MISC] Add ty typechecking (#1744)
* Enable ty * fix(types): resolve ty type checking errors Fix all 194 type errors reported by ty across 11 source files. Main patterns fixed: annotate self.charm as PostgresqlOperatorCharm via TYPE_CHECKING guards, add str | None narrowing for optional secrets and endpoints, fix implicit None returns, convert dict_keys to list before subscripting, and suppress dynamic Literal constructs with ty: ignore. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * Tweaks * Apply suggestions from code review Co-authored-by: Marcelo Henrique Neppel <marcelo.neppel@gmail.com> Signed-off-by: Dragomir Penev <6687393+dragomirp@users.noreply.github.com> * Empty string setting --------- Signed-off-by: Dragomir Penev <6687393+dragomirp@users.noreply.github.com> Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com> Co-authored-by: Marcelo Henrique Neppel <marcelo.neppel@gmail.com>
1 parent 6b4e74f commit ed2ed83

21 files changed

Lines changed: 341 additions & 198 deletions

poetry.lock

Lines changed: 30 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ optional = true
4747

4848
[tool.poetry.group.lint.dependencies]
4949
codespell = "^2.4.2"
50+
ty = "^0.0.50"
5051

5152
[tool.poetry.group.unit]
5253
optional = true
@@ -139,3 +140,11 @@ max-complexity = 10
139140

140141
[tool.ruff.lint.pydocstyle]
141142
convention = "google"
143+
144+
[tool.ty.environment]
145+
python = ".tox/lint/"
146+
extra-paths = ["./lib"]
147+
148+
[tool.ty.src]
149+
include = ["src", "scripts"]
150+
exclude = ["tests"]

src/backups.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def _get_s3_session_resource(self, s3_parameters: dict):
133133
),
134134
)
135135

136-
def _are_backup_settings_ok(self) -> tuple[bool, str | None]:
136+
def _are_backup_settings_ok(self) -> tuple[bool, str]:
137137
"""Validates whether backup settings are OK."""
138138
if self.model.get_relation(self.relation_name) is None:
139139
return (
@@ -145,7 +145,7 @@ def _are_backup_settings_ok(self) -> tuple[bool, str | None]:
145145
if missing_parameters:
146146
return False, f"Missing S3 parameters: {missing_parameters}"
147147

148-
return True, None
148+
return True, ""
149149

150150
@property
151151
def _can_initialise_stanza(self) -> bool:
@@ -209,7 +209,7 @@ def _is_standby_cluster(self) -> bool:
209209

210210
return not self.charm.async_replication.is_primary_cluster()
211211

212-
def can_use_s3_repository(self) -> tuple[bool, str | None]:
212+
def can_use_s3_repository(self) -> tuple[bool, str]:
213213
"""Returns whether the charm was configured to use another cluster repository."""
214214
# Check model uuid
215215
s3_parameters, _ = self._retrieve_s3_parameters()
@@ -272,7 +272,7 @@ def can_use_s3_repository(self) -> tuple[bool, str | None]:
272272
)
273273
return False, ANOTHER_CLUSTER_REPOSITORY_ERROR_MESSAGE
274274

275-
return True, None
275+
return True, ""
276276

277277
def _change_connectivity_to_database(self, connectivity: bool) -> None:
278278
"""Enable or disable the connectivity to the database."""
@@ -1059,8 +1059,8 @@ def _run_backup(
10591059
try:
10601060
backup_id = list(self._list_backups(show_failed=True).keys())[-1]
10611061
except ListBackupsError:
1062-
logger.exception(error_message)
10631062
error_message = "Failed to retrieve backup id"
1063+
logger.exception(error_message)
10641064
logger.error(f"Backup failed: {error_message}")
10651065
event.fail(error_message)
10661066
return
@@ -1238,7 +1238,7 @@ def _generate_fake_backup_id(self, backup_type: str) -> str:
12381238
if backup_type == "full":
12391239
return datetime.strftime(datetime.now(), "%Y%m%d-%H%M%SF")
12401240
if backup_type == "differential":
1241-
backups = self._list_backups(show_failed=False, parse=False).keys()
1241+
backups = list(self._list_backups(show_failed=False, parse=False).keys())
12421242
last_full_backup = None
12431243
for label in backups[::-1]:
12441244
if label.endswith("F"):
@@ -1249,12 +1249,13 @@ def _generate_fake_backup_id(self, backup_type: str) -> str:
12491249
raise TypeError("Differential backup requested but no previous full backup")
12501250
return f"{last_full_backup}_{datetime.strftime(datetime.now(), '%Y%m%d-%H%M%SD')}"
12511251
if backup_type == "incremental":
1252-
backups = self._list_backups(show_failed=False, parse=False).keys()
1252+
backups = list(self._list_backups(show_failed=False, parse=False).keys())
12531253
if not backups:
12541254
raise TypeError("Incremental backup requested but no previous successful backup")
12551255
return f"{backups[-1]}_{datetime.strftime(datetime.now(), '%Y%m%d-%H%M%SI')}"
1256+
raise TypeError(f"Invalid backup type: {backup_type}")
12561257

1257-
def _fetch_backup_from_id(self, backup_id: str) -> str:
1258+
def _fetch_backup_from_id(self, backup_id: str) -> str | None:
12581259
"""Fetches backup's pgbackrest label from backup id."""
12591260
timestamp = f"{datetime.strftime(datetime.strptime(backup_id, '%Y-%m-%dT%H:%M:%SZ'), '%Y%m%d-%H%M%S')}"
12601261
backups = self._list_backups(show_failed=False, parse=False).keys()
@@ -1361,7 +1362,7 @@ def _render_pgbackrest_conf_file(self) -> bool:
13611362
storage_path=self.charm._storage_path,
13621363
user=BACKUP_USER,
13631364
retention_full=s3_parameters["delete-older-than-days"],
1364-
process_max=max(os.cpu_count() - 2, 1),
1365+
process_max=max(self.charm.cpu_count - 2, 1),
13651366
)
13661367
# Render pgBackRest config file.
13671368
self.charm._patroni.render_file(f"{PGBACKREST_CONF_PATH}/pgbackrest.conf", rendered, 0o640)

0 commit comments

Comments
 (0)