Skip to content

Commit

Permalink
Fix issues reported by pyright
Browse files Browse the repository at this point in the history
`pyright` was reporting the following issues:

```
  pg_backup_api/server_operation.py:512:16 - error: Expression of type "tuple[str | bytearray | memoryview, int | Any]" cannot be assigned to return type "Tuple[str | None, int]" (reportGeneralTypeIssues)
pg_backup_api/utils.py
  pg_backup_api/utils.py:101:37 - error: Cannot access member "server_names" for type "None"
    Member "server_names" is unknown (reportGeneralTypeIssues)
  pg_backup_api/utils.py:102:34 - error: Cannot access member "get_server" for type "None"
    Member "get_server" is unknown (reportGeneralTypeIssues)
  pg_backup_api/utils.py:107:36 - error: "server" is not a known member of module "barman" (reportGeneralTypeIssues)
pg_backup_api/logic/utility_controller.py
  pg_backup_api/logic/utility_controller.py:57:33 - error: Cannot access member "server_names" for type "None"
    Member "server_names" is unknown (reportGeneralTypeIssues)
  pg_backup_api/logic/utility_controller.py:61:34 - error: Cannot access member "get_server" for type "None"
    Member "get_server" is unknown (reportGeneralTypeIssues)
  pg_backup_api/logic/utility_controller.py:69:37 - error: Cannot access member "servers_msg_list" for type "None"
    Member "servers_msg_list" is unknown (reportGeneralTypeIssues)
  pg_backup_api/logic/utility_controller.py:217:39 - error: "id" is not a known member of "None" (reportOptionalMemberAccess)
```

This commit fixes those issues by asserting that the variables
are instances of the expected types.

Note: we had to add `pyright: ignore` to a couple lines because
`barman` doesn't have type hints, otherwise `pyright` would keep
complaining about `Unknown` here in `pg-backup-api`.

References: BAR-133.

Signed-off-by: Israel Barth Rubio <[email protected]>
  • Loading branch information
barthisrael committed Nov 22, 2023
1 parent e660f0d commit fb056d5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
11 changes: 10 additions & 1 deletion pg_backup_api/pg_backup_api/logic/utility_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@

if TYPE_CHECKING: # pragma: no cover
from flask import Request, Response
from barman.config import Config as BarmanConfig
from pg_backup_api.server_operation import Operation


@app.route("/diagnose", methods=["GET"])
Expand All @@ -53,11 +55,15 @@ def diagnose() -> 'Response':
"""
# Reload the barman config so that any changes are picked up
load_barman_config()

if TYPE_CHECKING: # pragma: no cover
assert isinstance(barman.__config__, BarmanConfig)

# Get every server (both inactive and temporarily disabled)
servers = barman.__config__.server_names()

server_dict = {}
for server in servers:
for server in servers: # pyright: ignore
conf = barman.__config__.get_server(server)
if conf is None:
# Unknown server
Expand Down Expand Up @@ -214,6 +220,9 @@ def servers_operations_post(server_name: str,
)
subprocess.Popen(cmd.split())

if TYPE_CHECKING: # pragma: no cover
assert isinstance(operation, Operation)

return {"operation_id": operation.id}


Expand Down
13 changes: 8 additions & 5 deletions pg_backup_api/pg_backup_api/server_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import os
import subprocess
import sys
from typing import (Any, Callable, Dict, List, Optional, Set, Tuple,
from typing import (Any, Callable, Dict, List, Optional, Set, Tuple, Union,
TYPE_CHECKING)

from datetime import datetime
Expand Down Expand Up @@ -494,7 +494,8 @@ def get_status(self) -> str:
return self.server.get_operation_status(self.id)

@staticmethod
def _run_subprocess(cmd: List[str]) -> Tuple[Optional[str], int]:
def _run_subprocess(cmd: List[str]) -> \
Tuple[Union[str, bytearray, memoryview], Union[int, Any]]:
"""
Run *cmd* as a subprocess.
Expand All @@ -512,7 +513,8 @@ def _run_subprocess(cmd: List[str]) -> Tuple[Optional[str], int]:
return stdout, process.returncode

@abstractmethod
def _run_logic(self) -> Tuple[Optional[str], int]:
def _run_logic(self) -> \
Tuple[Union[str, bytearray, memoryview], Union[int, Any]]:
"""
Logic to be ran when executing the operation.
Expand All @@ -526,7 +528,7 @@ def _run_logic(self) -> Tuple[Optional[str], int]:
"""
pass

def run(self) -> Tuple[Optional[str], int]:
def run(self) -> Tuple[Union[str, bytearray, memoryview], Union[int, Any]]:
"""
Run the operation.
Expand Down Expand Up @@ -621,7 +623,8 @@ def _get_args(self) -> List[str]:
remote_ssh_command,
]

def _run_logic(self) -> Tuple[Optional[str], int]:
def _run_logic(self) -> \
Tuple[Union[str, bytearray, memoryview], Union[int, Any]]:
"""
Logic to be ran when executing the recovery operation.
Expand Down
18 changes: 12 additions & 6 deletions pg_backup_api/pg_backup_api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@

if TYPE_CHECKING: # pragma: no cover
import flask.app
from barman.config import ServerConfig
from barman.config import Config as BarmanConfig, ServerConfig
import barman.server


CONFIG_FILENAME = "/etc/barman.conf"
Expand Down Expand Up @@ -98,7 +99,10 @@ def get_server_by_name(server_name: str) -> Optional['ServerConfig']:
:return: configuration of Barman server *server_name* if that server
exists, ``None`` otherwise.
"""
for server in barman.__config__.server_names():
if TYPE_CHECKING: # pragma: no cover
assert isinstance(barman.__config__, BarmanConfig)

for server in barman.__config__.server_names(): # pyright: ignore
conf = barman.__config__.get_server(server)
if server == server_name:
return conf
Expand All @@ -120,11 +124,13 @@ def parse_backup_id(server: barman.server.Server,
:return: information about the backup, if *backup_id* can be satisfied,
``None`` otherwise.
"""
parsed_backup_id = backup_id

if backup_id in ("latest", "last"):
backup_id = server.get_last_backup_id()
parsed_backup_id = server.get_last_backup_id()
elif backup_id in ("oldest", "first"):
backup_id = server.get_first_backup_id()
parsed_backup_id = server.get_first_backup_id()
elif backup_id in ("last-failed"):
backup_id = server.get_last_backup_id([BackupInfo.FAILED])
parsed_backup_id = server.get_last_backup_id([BackupInfo.FAILED])

return server.get_backup(backup_id)
return server.get_backup(parsed_backup_id)

0 comments on commit fb056d5

Please sign in to comment.