Skip to content

Commit 2464e31

Browse files
author
Sylvain MARIE
committed
Removed the useless 'strict' length validators: removed min_len_strict and max_len_strict in validate entry point, and removed strict argument in validation_lib's minlen and maxlen. Indeed length is an integer by python framework definition, so it is always more compact to do +1 or -1 to the number. Fixes #29
1 parent 0ccb729 commit 2464e31

4 files changed

Lines changed: 32 additions & 88 deletions

File tree

valid8/entry_points_inline.py

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,7 @@ def validate(name, # type: str
137137
max_strict=False, # type: bool
138138
length=None, # type: int
139139
min_len=None, # type: int
140-
min_len_strict=False, # type: bool
141140
max_len=None, # type: int
142-
max_len_strict=False, # type: bool
143141
custom=None, # type: Callable[[Any], Any]
144142
error_type=None, # type: Type[ValidationError]
145143
help_msg=None, # type: str
@@ -153,8 +151,7 @@ def validate(name, # type: str
153151
* if `allowed_values` is provided, `value` should be in that set
154152
* if `min_value` (resp. `max_value`) is provided, `value` should be greater than it. Comparison is not strict by
155153
default and can be set to strict by setting `min_strict`, resp. `max_strict`, to `True`
156-
* if `min_len` (resp. `max_len`) is provided, `len(value)` should be greater than it. Comparison is not strict by
157-
default and can be set to strict by setting `min_len_strict`, resp. `max_len_strict`, to `True`
154+
* if `min_len` (resp. `max_len`) is provided, `len(value)` should be greater than it. Comparison is not strict.
158155
159156
:param name: the applicative name of the checked value, that will be used in error messages
160157
:param value: the value to check
@@ -174,9 +171,7 @@ def validate(name, # type: str
174171
:param max_strict: if True, only values strictly lesser than `max_value` will be accepted
175172
:param length: an optional strict length
176173
:param min_len: an optional minimum length
177-
:param min_len_strict: if True, only values with length strictly greater than `min_len` will be accepted
178174
:param max_len: an optional maximum length
179-
:param max_len_strict: if True, only values with length strictly lesser than `max_len` will be accepted
180175
:param custom: a custom base validation function or list of base validation functions to use. This is the same
181176
syntax than for valid8 decorators. A callable, a tuple(callable, help_msg_str), a tuple(callable, failure_type),
182177
or a list of several such elements. Nested lists are supported and indicate an implicit `and_`. Tuples indicate
@@ -272,22 +267,14 @@ def validate(name, # type: str
272267
raise WrongLength(wrong_value=value, ref_length=length)
273268

274269
if min_len is not None:
275-
# inlined version of minlen(min_length=min_len, strict=min_len_strict)(value) without 'return True'
276-
if min_len_strict:
277-
if not len(value) > min_len:
278-
raise TooShort(wrong_value=value, min_length=min_len, strict=True)
279-
else:
280-
if not len(value) >= min_len:
281-
raise TooShort(wrong_value=value, min_length=min_len, strict=False)
270+
# inlined version of minlen(min_length=min_len)(value) without 'return True'
271+
if len(value) < min_len:
272+
raise TooShort(wrong_value=value, min_length=min_len)
282273

283274
if max_len is not None:
284-
# inlined version of maxlen(max_length=max_len, strict=max_len_strict)(value) without 'return True'
285-
if max_len_strict:
286-
if not len(value) < max_len:
287-
raise TooLong(wrong_value=value, max_length=max_len, strict=True)
288-
else:
289-
if not len(value) <= max_len:
290-
raise TooLong(wrong_value=value, max_length=max_len, strict=False)
275+
# inlined version of maxlen(max_length=max_len)(value) without 'return True'
276+
if len(value) > max_len:
277+
raise TooLong(wrong_value=value, max_length=max_len)
291278

292279
except Exception as e:
293280
err = _QUICK_VALIDATOR._create_validation_error(name, value, validation_outcome=e, error_type=error_type,

valid8/tests/validation_lib/test_validators_collections.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from mini_lambda import make_lambda_friendly_method, _, x
44
from valid8 import on_each_, is_even, maxlen, on_all_, is_subset, is_superset, is_in, Failure, minlen, TooShort, \
5-
minlens, TooLong, length_between, maxlens, LengthNotInRange, lt, contains, has_length, WrongLength
5+
TooLong, length_between, LengthNotInRange, lt, contains, has_length, WrongLength
66

77

88
def test_is_in():
@@ -75,13 +75,6 @@ def test_minlen():
7575
minlen(1)([])
7676

7777

78-
def test_minlens():
79-
""" tests that the minlens() function works """
80-
with pytest.raises(TooShort):
81-
minlens(1)(['a'])
82-
assert minlens(1)(['a', 'a'])
83-
84-
8578
def test_maxlen():
8679
""" tests that the maxlen() function works """
8780
assert maxlen(1)(['a'])

valid8/validation_lib/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from .types import HasWrongType, IsWrongType, instance_of, subclass_of
2-
from .collections import TooLong, TooShort, minlen, minlens, maxlen, maxlens, WrongLength, has_length,\
2+
from .collections import TooLong, TooShort, minlen, maxlen, WrongLength, has_length,\
33
LengthNotInRange, length_between, NotInAllowedValues, is_in, NotSubset, is_subset, DoesNotContainValue, contains,\
44
NotSuperset, is_superset, InvalidItemInSequence, on_all_, on_each_
55
from .comparables import NotEqual, TooSmall, gt, gts, TooBig, lt, lts, NotInRange, between
@@ -10,7 +10,7 @@
1010
'types', 'collections', 'comparables', 'numbers',
1111
# symbols
1212
'HasWrongType', 'IsWrongType', 'instance_of', 'subclass_of',
13-
'TooLong', 'TooShort', 'minlen', 'minlens', 'maxlen', 'maxlens', 'WrongLength', 'has_length', 'LengthNotInRange',
13+
'TooLong', 'TooShort', 'minlen', 'maxlen', 'WrongLength', 'has_length', 'LengthNotInRange',
1414
'length_between', 'NotInAllowedValues', 'is_in', 'NotSubset', 'is_subset', 'DoesNotContainValue', 'contains',
1515
'NotSuperset', 'is_superset', 'InvalidItemInSequence', 'on_all_', 'on_each_',
1616
'NotEqual', 'TooSmall', 'gt', 'gts', 'TooBig', 'lt', 'lts', 'NotInRange', 'between',

valid8/validation_lib/collections.py

Lines changed: 22 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -9,93 +9,57 @@
99

1010
class TooShort(Failure, ValueError):
1111
""" Custom Failure raised by minlen """
12-
def __init__(self, wrong_value, min_length, strict):
13-
symbol = '>' if strict else '>='
14-
help_msg = 'len(x) {symbol} {min_length} does not hold for x={wrong_value}'
15-
super(TooShort, self).__init__(wrong_value=wrong_value, min_length=min_length, symbol=symbol, help_msg=help_msg)
12+
def __init__(self, wrong_value, min_length):
13+
help_msg = 'len(x) >= {min_length} does not hold for x={wrong_value}'
14+
super(TooShort, self).__init__(wrong_value=wrong_value, min_length=min_length, help_msg=help_msg)
1615

1716

18-
def minlen(min_length,
19-
strict=False # type: bool
17+
def minlen(min_length
2018
):
2119
"""
2220
'Minimum length' validation_function generator.
23-
Returns a validation_function to check that len(x) >= min_length (strict=False, default)
24-
or len(x) > min_length (strict=True)
21+
Returns a validation_function to check that len(x) >= min_length
2522
2623
:param min_length: minimum length for x
27-
:param strict: Boolean flag to switch between len(x) >= min_length (strict=False) and len(x) > min_length
28-
(strict=True)
2924
:return:
3025
"""
31-
if strict:
32-
def minlen_(x):
33-
if len(x) > min_length:
34-
return True
35-
else:
36-
# raise Failure('minlen: len(x) > ' + str(min_length) + ' does not hold for x=' + str(x))
37-
raise TooShort(wrong_value=x, min_length=min_length, strict=True)
38-
else:
39-
def minlen_(x):
40-
if len(x) >= min_length:
41-
return True
42-
else:
43-
# raise Failure('minlen: len(x) >= ' + str(min_length) + ' does not hold for x=' + str(x))
44-
raise TooShort(wrong_value=x, min_length=min_length, strict=False)
26+
def minlen_(x):
27+
if len(x) >= min_length:
28+
return True
29+
else:
30+
raise TooShort(wrong_value=x, min_length=min_length)
4531

46-
minlen_.__name__ = 'length_{}greater_than_{}'.format('strictly_' if strict else '', min_length)
32+
minlen_.__name__ = 'length_greater_than_%s' % min_length
4733
return minlen_
4834

4935

50-
def minlens(min_length_strict):
51-
""" Alias for 'Minimum length' validation_function generator in strict mode """
52-
return minlen(min_length_strict, True)
53-
54-
5536
class TooLong(Failure, ValueError):
5637
""" Custom Failure raised by maxlen """
57-
def __init__(self, wrong_value, max_length, strict):
58-
symbol = '<' if strict else '<='
59-
help_msg = 'len(x) {symbol} {max_length} does not hold for x={wrong_value}'
60-
super(TooLong, self).__init__(wrong_value=wrong_value, max_length=max_length, symbol=symbol, help_msg=help_msg)
38+
def __init__(self, wrong_value, max_length):
39+
help_msg = 'len(x) <= {max_length} does not hold for x={wrong_value}'
40+
super(TooLong, self).__init__(wrong_value=wrong_value, max_length=max_length, help_msg=help_msg)
6141

6242

6343
def maxlen(max_length,
64-
strict=False # type: bool
6544
):
6645
"""
6746
'Maximum length' validation_function generator.
68-
Returns a validation_function to check that len(x) <= max_length (strict=False, default) or len(x) < max_length (strict=True)
47+
Returns a validation_function to check that len(x) <= max_length
6948
7049
:param max_length: maximum length for x
71-
:param strict: Boolean flag to switch between len(x) <= max_length (strict=False) and len(x) < max_length
72-
(strict=True)
7350
:return:
7451
"""
75-
if strict:
76-
def maxlen_(x):
77-
if len(x) < max_length:
78-
return True
79-
else:
80-
# raise Failure('maxlen: len(x) < ' + str(max_length) + ' does not hold for x=' + str(x))
81-
raise TooLong(wrong_value=x, max_length=max_length, strict=True)
82-
else:
83-
def maxlen_(x):
84-
if len(x) <= max_length:
85-
return True
86-
else:
87-
# raise Failure('maxlen: len(x) <= ' + str(max_length) + ' does not hold for x=' + str(x))
88-
raise TooLong(wrong_value=x, max_length=max_length, strict=False)
52+
def maxlen_(x):
53+
if len(x) <= max_length:
54+
return True
55+
else:
56+
# raise Failure('maxlen: len(x) <= ' + str(max_length) + ' does not hold for x=' + str(x))
57+
raise TooLong(wrong_value=x, max_length=max_length)
8958

90-
maxlen_.__name__ = 'length_{}lesser_than_{}'.format('strictly_' if strict else '', max_length)
59+
maxlen_.__name__ = 'length_lesser_than_%s' % max_length
9160
return maxlen_
9261

9362

94-
def maxlens(max_length_strict):
95-
""" Alias for 'Maximum length' validation_function generator in strict mode """
96-
return maxlen(max_length_strict, True)
97-
98-
9963
class WrongLength(Failure, ValueError):
10064
""" Custom failure raised by has_length """
10165
def __init__(self, wrong_value, ref_length):

0 commit comments

Comments
 (0)