Compare commits

..
2 Commits
Author SHA1 Message Date
Fuxino 0829c33053 Fix mypy errors in simple_backup() and Backup
Accept None for the missing inputs, output, exclude and keep values and replace them with their defaults in __init__, and annotate the SSH client, which mypy typed as None and so skipped checking the remote code.
2026-10-14 08:37:06 +02:00
Fuxino cfe3b28436 Fix mypy errors in the tests
The config values are now a TypedDict, so check inputs for None before sorting, use literal keys and use ConfigArgs as the fake _read_config() return type.
2026-10-14 08:37:06 +02:00
4 changed files with 21 additions and 24 deletions

No files matched your search

+14 -18
View File
@@ -136,14 +136,16 @@ class Backup:
Perform the backup Perform the backup
""" """
def __init__(self, inputs: List[str], output: str, exclude: List[str], keep: int, options: str, def __init__(self, inputs: Optional[List[str]], output: Optional[str], exclude: Optional[List[str]],
keep: Optional[int], options: str,
ssh_host: Optional[str] = None, ssh_user: Optional[str] = None, ssh_keyfile: Optional[str] = None, ssh_host: Optional[str] = None, ssh_user: Optional[str] = None, ssh_keyfile: Optional[str] = None,
remote_sudo: bool = False, remove_before: bool = False, verbose: bool = False) -> None: remote_sudo: bool = False, remove_before: bool = False, verbose: bool = False) -> None:
self.inputs = inputs # Missing values are reported by check_params()
self.output = output self.inputs = inputs if inputs is not None else []
self.exclude = exclude self.output = output if output is not None else ''
self.exclude = exclude if exclude is not None else []
self.options = options self.options = options
self.keep = keep self.keep = keep if keep is not None else -1
self.ssh_host = ssh_host self.ssh_host = ssh_host
self.ssh_user = ssh_user self.ssh_user = ssh_user
self.ssh_keyfile = ssh_keyfile self.ssh_keyfile = ssh_keyfile
@@ -156,20 +158,20 @@ class Backup:
self._inputs_path = '' self._inputs_path = ''
self._exclude_path = '' self._exclude_path = ''
self._remote = False self._remote = False
self._ssh = None self._ssh: Optional[paramiko.client.SSHClient] = None
self._password_auth = False self._password_auth = False
self._removed_count = 0 self._removed_count = 0
def check_params(self, homedir: str = '') -> int: def check_params(self, homedir: str = '') -> int:
"""Check if parameters for the backup are valid""" """Check if parameters for the backup are valid"""
if self.inputs is None or len(self.inputs) == 0: if len(self.inputs) == 0:
logger.info( logger.info(
'No existing files or directories specified for backup. Nothing to do') 'No existing files or directories specified for backup. Nothing to do')
return 1 return 1
if self.output is None: if self.output == '':
logger.critical( logger.critical(
'No output path specified. Use -o argument or specify output path in configuration file') 'No output path specified. Use -o argument or specify output path in configuration file')
@@ -207,9 +209,6 @@ class Backup:
self.output = os.path.abspath(self.output) self.output = os.path.abspath(self.output)
if self.keep is None:
self.keep = -1
return 0 return 0
def _resolve_remote_path(self, path: str) -> Optional[str]: def _resolve_remote_path(self, path: str) -> Optional[str]:
@@ -605,10 +604,9 @@ class Backup:
fd, self._exclude_path = mkstemp(prefix='tmp_exclude', text=True) fd, self._exclude_path = mkstemp(prefix='tmp_exclude', text=True)
with os.fdopen(fd, 'w', encoding='utf-8') as fp: with os.fdopen(fd, 'w', encoding='utf-8') as fp:
if self.exclude is not None: for e in self.exclude:
for e in self.exclude: fp.write(e)
fp.write(e) fp.write('\n')
fp.write('\n')
if self.keep != -1 and self._remove_before: if self.keep != -1 and self._remove_before:
self.remove_old_backups() self.remove_old_backups()
@@ -1015,9 +1013,7 @@ def simple_backup() -> int:
if args.numeric_ids or config_args['numeric_ids']: if args.numeric_ids or config_args['numeric_ids']:
rsync_options.append('--numeric-ids') rsync_options.append('--numeric-ids')
rsync_options = ' '.join(rsync_options) backup = Backup(inputs, output, exclude, keep, ' '.join(rsync_options), ssh_host, ssh_user, ssh_keyfile,
backup = Backup(inputs, output, exclude, keep, rsync_options, ssh_host, ssh_user, ssh_keyfile,
remote_sudo, remove_before=args.remove_before_backup, verbose=args.verbose) remote_sudo, remove_before=args.remove_before_backup, verbose=args.verbose)
try: try:
+1 -1
View File
@@ -91,7 +91,7 @@ def config_calls(monkeypatch: pytest.MonkeyPatch) -> list[tuple[str, Optional[st
calls: list[tuple[str, Optional[str]]] = [] calls: list[tuple[str, Optional[str]]] = []
read_config = sb._read_config read_config = sb._read_config
def record(config_file: str, user: Optional[str] = None) -> dict[str, Any]: def record(config_file: str, user: Optional[str] = None) -> sb.ConfigArgs:
calls.append((config_file, user)) calls.append((config_file, user))
return read_config('/nonexistent/simple_backup.conf', user) return read_config('/nonexistent/simple_backup.conf', user)
+4 -3
View File
@@ -85,6 +85,7 @@ keep=3
config_args = sb._read_config(config_file, USER) config_args = sb._read_config(config_file, USER)
assert config_args['inputs'] is not None
assert sorted(config_args['inputs']) == [f'{tmp_path}/one', f'{tmp_path}/two'] assert sorted(config_args['inputs']) == [f'{tmp_path}/one', f'{tmp_path}/two']
# Expanded later, as it may be a path on the server # Expanded later, as it may be a path on the server
assert config_args['output'] == '~/backups' assert config_args['output'] == '~/backups'
@@ -176,9 +177,9 @@ numeric_ids=
config_args = sb._read_config(config_file, USER) config_args = sb._read_config(config_file, USER)
for key in ['ssh_host', 'ssh_user', 'ssh_keyfile']: assert config_args['ssh_host'] is None
assert config_args[key] is None assert config_args['ssh_user'] is None
assert config_args['ssh_keyfile'] is None
assert config_args['remote_sudo'] is False assert config_args['remote_sudo'] is False
assert config_args['numeric_ids'] is False assert config_args['numeric_ids'] is False
+2 -2
View File
@@ -79,14 +79,14 @@ class TestCheckParams:
assert sb.Backup([], str(out), [], -1, OPTIONS).check_params() == 1 assert sb.Backup([], str(out), [], -1, OPTIONS).check_params() == 1
def test_no_output(self) -> None: def test_no_output(self) -> None:
assert sb.Backup(['/x'], None, [], -1, OPTIONS).check_params() == 2 # type: ignore[arg-type] assert sb.Backup(['/x'], None, [], -1, OPTIONS).check_params() == 2
def test_missing_output(self, tmp_path: Path) -> None: def test_missing_output(self, tmp_path: Path) -> None:
assert sb.Backup(['/x'], str(tmp_path / 'missing'), [], -1, OPTIONS).check_params() == 2 assert sb.Backup(['/x'], str(tmp_path / 'missing'), [], -1, OPTIONS).check_params() == 2
def test_relative_output(self, out: Path, monkeypatch: pytest.MonkeyPatch) -> None: def test_relative_output(self, out: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.chdir(out.parent) monkeypatch.chdir(out.parent)
backup = sb.Backup(['/x'], out.name, [], None, OPTIONS) # type: ignore[arg-type] backup = sb.Backup(['/x'], out.name, [], None, OPTIONS)
assert backup.check_params() == 0 assert backup.check_params() == 0
assert backup.output == str(out) assert backup.output == str(out)