From c829003f079503096c4ded10b0c84f708cbe0575 Mon Sep 17 00:00:00 2001 From: uttam12331 Date: Tue, 4 Aug 2026 22:21:32 +0530 Subject: [PATCH] Raise LengthInvalid (not RangeInvalid) for non-sized values in Length Length.__call__ raises LengthInvalid for its min/max length checks, but its `except TypeError` handler (hit when the value has no len(), e.g. None or an int) raises RangeInvalid instead -- a copy-paste leftover from the Clamp validator above, which is genuinely a range operation. Passing a non-sized value to a Length validator therefore reports a range error rather than a length error, inconsistent with the validator's own two other error paths. Raise LengthInvalid, and tighten test_length_invalid to assert the error type. --- voluptuous/tests/tests.py | 5 ++++- voluptuous/validators.py | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/voluptuous/tests/tests.py b/voluptuous/tests/tests.py index 0608548..844dc80 100644 --- a/voluptuous/tests/tests.py +++ b/voluptuous/tests/tests.py @@ -33,6 +33,7 @@ IsDir, IsFile, Length, + LengthInvalid, Literal, LiteralInvalid, Marker, @@ -870,8 +871,10 @@ def test_length_too_long(): def test_length_invalid(): v = None s = Schema(Length(min=0, max=2)) - with pytest.raises(MultipleInvalid): + with pytest.raises(MultipleInvalid) as ctx: s(v) + # a value with no length must be reported as a LengthInvalid, not a RangeInvalid + assert isinstance(ctx.value.errors[0], LengthInvalid) def test_equal(): diff --git a/voluptuous/validators.py b/voluptuous/validators.py index a69bb8a..2ce5925 100644 --- a/voluptuous/validators.py +++ b/voluptuous/validators.py @@ -773,7 +773,7 @@ def __call__(self, v): # Objects that have no length e.g. None or strings will raise TypeError except TypeError: - raise RangeInvalid(self.msg or 'invalid value or type') + raise LengthInvalid(self.msg or 'invalid value or type') def __repr__(self): return 'Length(min=%s, max=%s)' % (self.min, self.max)