From cf6fd0769db652c45ad80ca19934090d551847fb Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Fri, 27 Feb 2026 20:11:05 +0100 Subject: [PATCH] use jsonargparse's PositiveInt --- src/borg/archiver/_common.py | 8 ++++---- src/borg/helpers/__init__.py | 2 +- src/borg/helpers/argparsing.py | 1 + src/borg/helpers/parseformat.py | 8 -------- 4 files changed, 6 insertions(+), 13 deletions(-) diff --git a/src/borg/archiver/_common.py b/src/borg/archiver/_common.py index 0d058e9b0..19f08c319 100644 --- a/src/borg/archiver/_common.py +++ b/src/borg/archiver/_common.py @@ -7,9 +7,9 @@ from ..archive import Archive from ..constants import * # NOQA from ..cache import Cache, assert_secure from ..helpers import Error -from ..helpers import SortBySpec, positive_int_validator, location_validator, Location, relative_time_marker_validator +from ..helpers import SortBySpec, location_validator, Location, relative_time_marker_validator from ..helpers import Highlander, octal_int -from ..helpers.argparsing import SUPPRESS +from ..helpers.argparsing import SUPPRESS, PositiveInt from ..helpers.nanorst import rst_to_terminal from ..manifest import Manifest, AI_HUMAN_SORT_KEYS from ..patterns import PatternMatcher @@ -375,7 +375,7 @@ def define_archive_filters_group( "--first", metavar="N", dest="first", - type=positive_int_validator, + type=PositiveInt, action=Highlander, help="consider the first N archives after other filters are applied", ) @@ -383,7 +383,7 @@ def define_archive_filters_group( "--last", metavar="N", dest="last", - type=positive_int_validator, + type=PositiveInt, action=Highlander, help="consider the last N archives after other filters are applied", ) diff --git a/src/borg/helpers/__init__.py b/src/borg/helpers/__init__.py index 2e2c30709..12db71b2a 100644 --- a/src/borg/helpers/__init__.py +++ b/src/borg/helpers/__init__.py @@ -27,7 +27,7 @@ from .misc import sysinfo, log_multi, consume from .misc import ChunkIteratorFileWrapper, open_item, chunkit, iter_separated, ErrorIgnoringTextIOWrapper from .parseformat import octal_int, bin_to_hex, hex_to_bin, safe_encode, safe_decode from .parseformat import text_to_json, binary_to_json, remove_surrogates, join_cmd -from .parseformat import eval_escapes, decode_dict, positive_int_validator, interval +from .parseformat import eval_escapes, decode_dict, interval from .parseformat import ( PathSpec, FilesystemPathSpec, diff --git a/src/borg/helpers/argparsing.py b/src/borg/helpers/argparsing.py index 69214ca58..f0a205749 100644 --- a/src/borg/helpers/argparsing.py +++ b/src/borg/helpers/argparsing.py @@ -102,6 +102,7 @@ from argparse import Action, ArgumentError, ArgumentTypeError, RawDescriptionHel from jsonargparse import ArgumentParser as _ArgumentParser # we subclass that to add custom behavior from jsonargparse import Namespace, SUPPRESS, REMAINDER # noqa: F401 from jsonargparse.typing import register_type # noqa: F401 +from jsonargparse.typing import PositiveInt # noqa: F401 # borg completion uses these private symbols, so we need to import them: from jsonargparse._actions import _ActionSubCommands # noqa: F401 diff --git a/src/borg/helpers/parseformat.py b/src/borg/helpers/parseformat.py index fbc580c3f..b6c0c7a21 100644 --- a/src/borg/helpers/parseformat.py +++ b/src/borg/helpers/parseformat.py @@ -128,14 +128,6 @@ def decode_dict(d, keys, encoding="utf-8", errors="surrogateescape"): return d -def positive_int_validator(value): - """argparse type for positive integers, N > 0.""" - int_value = int(value) - if int_value <= 0: - raise ArgumentTypeError("A positive integer is required: %s" % value) - return int_value - - def interval(s): """Convert a string representing a valid interval to a number of seconds.""" if isinstance(s, int):