From dec2eb745b819d68b2d61cb47049dbf57ac19f3c Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Mon, 3 Nov 2025 23:19:21 +0100 Subject: [PATCH] refactor: update type hints to use Python 3.10+ syntax --- src/borg/chunkers/failing.py | 2 +- src/borg/chunkers/fixed.py | 6 +++--- src/borg/hashindex.pyi | 6 +++--- src/borg/helpers/__init__.py | 1 - src/borg/item.pyi | 20 ++++++++++---------- src/borg/legacyrepository.py | 3 +-- 6 files changed, 18 insertions(+), 20 deletions(-) diff --git a/src/borg/chunkers/failing.py b/src/borg/chunkers/failing.py index 49b9966af..3ff333bdf 100644 --- a/src/borg/chunkers/failing.py +++ b/src/borg/chunkers/failing.py @@ -26,7 +26,7 @@ class ChunkerFailing: self.count = 0 self.chunking_time = 0.0 # not updated, just provided so that caller does not crash - def chunkify(self, fd: BinaryIO = None, fh: int = -1) -> Iterator: + def chunkify(self, fd: BinaryIO | None = None, fh: int = -1) -> Iterator: """ Cut a file into chunks. diff --git a/src/borg/chunkers/fixed.py b/src/borg/chunkers/fixed.py index 03096e45c..3cdab7a9f 100644 --- a/src/borg/chunkers/fixed.py +++ b/src/borg/chunkers/fixed.py @@ -1,4 +1,4 @@ -from typing import List, Iterator, BinaryIO +from typing import Iterator, BinaryIO API_VERSION = "1.2_01" @@ -33,10 +33,10 @@ class ChunkerFixed: self.header_size = header_size self.chunking_time = 0.0 # likely will stay close to zero - not much to do here. self.reader_block_size = 1024 * 1024 - self.reader: FileReader = None + self.reader: FileReader | None = None self.sparse = sparse - def chunkify(self, fd: BinaryIO = None, fh: int = -1, fmap: List = None) -> Iterator: + def chunkify(self, fd: BinaryIO | None = None, fh: int = -1, fmap: list | None = None) -> Iterator: """ Cut a file into chunks. diff --git a/src/borg/hashindex.pyi b/src/borg/hashindex.pyi index fb05aba86..626973319 100644 --- a/src/borg/hashindex.pyi +++ b/src/borg/hashindex.pyi @@ -1,14 +1,14 @@ -from typing import NamedTuple, Tuple, Type, Union, IO, Iterator, Any +from typing import NamedTuple, Tuple, Type, IO, Iterator, Any API_VERSION: str -PATH_OR_FILE = Union[str, IO] +PATH_OR_FILE = str | IO class ChunkIndexEntry(NamedTuple): flags: int size: int -CIE = Union[Tuple[int, int], Type[ChunkIndexEntry]] +CIE = Tuple[int, int] | Type[ChunkIndexEntry] class ChunkIndex: F_NONE: int diff --git a/src/borg/helpers/__init__.py b/src/borg/helpers/__init__.py index 0f5626ad6..ca19f5c89 100644 --- a/src/borg/helpers/__init__.py +++ b/src/borg/helpers/__init__.py @@ -7,7 +7,6 @@ package, which are imported here for compatibility. import os import logging -from typing import List from collections import namedtuple from ..constants import * # NOQA diff --git a/src/borg/item.pyi b/src/borg/item.pyi index 97de37904..d85a03266 100644 --- a/src/borg/item.pyi +++ b/src/borg/item.pyi @@ -1,4 +1,4 @@ -from typing import Set, NamedTuple, Tuple, Mapping, Dict, List, Iterator, Callable, Any, Optional +from typing import Set, NamedTuple, Tuple, Mapping, Dict, List, Iterator, Callable, Any from .helpers import StableDict @@ -258,7 +258,7 @@ class ManifestItem(PropDict): class DiffChange: diff_type: str diff_data: Dict[str, Any] - def __init__(self, diff_type: str, diff_data: Optional[Dict[str, Any]] = ...) -> None: ... + def __init__(self, diff_type: str, diff_data: Dict[str, Any] | None = ...) -> None: ... def to_dict(self) -> Dict[str, Any]: ... class ItemDiff: @@ -275,14 +275,14 @@ class ItemDiff: ) -> None: ... def changes(self) -> Dict[str, DiffChange]: ... def equal(self, content_only: bool = ...) -> bool: ... - def content(self) -> Optional[DiffChange]: ... - def ctime(self) -> Optional[DiffChange]: ... - def mtime(self) -> Optional[DiffChange]: ... - def mode(self) -> Optional[DiffChange]: ... - def type(self) -> Optional[DiffChange]: ... - def owner(self) -> Optional[DiffChange]: ... - def user(self) -> Optional[DiffChange]: ... - def group(self) -> Optional[DiffChange]: ... + def content(self) -> DiffChange | None: ... + def ctime(self) -> DiffChange | None: ... + def mtime(self) -> DiffChange | None: ... + def mode(self) -> DiffChange | None: ... + def type(self) -> DiffChange | None: ... + def owner(self) -> DiffChange | None: ... + def user(self) -> DiffChange | None: ... + def group(self) -> DiffChange | None: ... def chunk_content_equal(chunks_a: Iterator, chunks_b: Iterator) -> bool: ... diff --git a/src/borg/legacyrepository.py b/src/borg/legacyrepository.py index 719f947f1..9cdd5f981 100644 --- a/src/borg/legacyrepository.py +++ b/src/borg/legacyrepository.py @@ -9,7 +9,6 @@ from collections import defaultdict from configparser import ConfigParser from functools import partial from itertools import islice -from typing import DefaultDict from collections.abc import Callable from .constants import * # NOQA @@ -48,7 +47,7 @@ TAG_PUT2 = 3 # may not be able to handle the new tags. MAX_TAG_ID = 15 -FreeSpace: Callable[[], DefaultDict] = partial(defaultdict, int) +FreeSpace: Callable[[], defaultdict] = partial(defaultdict, int) def header_size(tag):