From 65f736cf6fde9bae22aa643495cbda384cb3df59 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Tue, 25 Jun 2024 22:55:29 +0200 Subject: [PATCH] Improve validator typing to allow non-number formats for min and max (#516) --- voluptuous/validators.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/voluptuous/validators.py b/voluptuous/validators.py index 9951738..89eed0f 100644 --- a/voluptuous/validators.py +++ b/voluptuous/validators.py @@ -1,4 +1,6 @@ # fmt: off +from __future__ import annotations + import datetime import os import re @@ -18,6 +20,9 @@ # F401: flake8 complains about 'raises' not being used, but it is used in doctests from voluptuous.schema_builder import Schema, Schemable, message, raises # noqa: F401 +if typing.TYPE_CHECKING: + from _typeshed import SupportsAllComparisons + # fmt: on @@ -610,9 +615,6 @@ def Maybe(validator: typing.Callable, msg: typing.Optional[str] = None): return Any(None, validator, msg=msg) -NullableNumber = typing.Union[int, float, None] - - class Range(object): """Limit a value to a range. @@ -636,8 +638,8 @@ class Range(object): def __init__( self, - min: NullableNumber = None, - max: NullableNumber = None, + min: SupportsAllComparisons | None = None, + max: SupportsAllComparisons | None = None, min_included: bool = True, max_included: bool = True, msg: typing.Optional[str] = None, @@ -705,8 +707,8 @@ class Clamp(object): def __init__( self, - min: NullableNumber = None, - max: NullableNumber = None, + min: SupportsAllComparisons | None = None, + max: SupportsAllComparisons | None = None, msg: typing.Optional[str] = None, ) -> None: self.min = min @@ -736,8 +738,8 @@ class Length(object): def __init__( self, - min: NullableNumber = None, - max: NullableNumber = None, + min: SupportsAllComparisons | None = None, + max: SupportsAllComparisons | None = None, msg: typing.Optional[str] = None, ) -> None: self.min = min