Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

validators: Make to_positive_or_allowed_int an optional argument.

Vishnu KS772500d1c610
diff --git a/zerver/lib/validator.py b/zerver/lib/validator.py
index 969eb0c0f9d..9c84d41b585 100644
--- a/zerver/lib/validator.py
+++ b/zerver/lib/validator.py
@@ -465,10 +465,10 @@ def to_non_negative_int(s: str, max_int_size: int = 2 ** 32 - 1) -> int:
return x
-def to_positive_or_allowed_int(allowed_integer: int) -> Callable[[str], int]:
+def to_positive_or_allowed_int(allowed_integer: Optional[int] = None) -> Callable[[str], int]:
def converter(s: str) -> int:
x = int(s)
- if x == allowed_integer:
+ if allowed_integer is not None and x == allowed_integer:
return x
if x == 0:
raise ValueError("argument is 0")
diff --git a/zerver/tests/test_decorators.py b/zerver/tests/test_decorators.py
index 45b6708e2e2..0f6be0d8c51 100644
--- a/zerver/tests/test_decorators.py
+++ b/zerver/tests/test_decorators.py
@@ -787,12 +787,20 @@ class ValidatorTestCase(ZulipTestCase):
self.assertEqual(to_non_negative_int(str(2 ** 32)))
def test_to_positive_or_allowed_int(self) -> None:
+ self.assertEqual(to_positive_or_allowed_int()("5"), 5)
self.assertEqual(to_positive_or_allowed_int(-1)("5"), 5)
+
self.assertEqual(to_positive_or_allowed_int(-1)("-1"), -1)
with self.assertRaisesRegex(ValueError, "argument is negative"):
to_positive_or_allowed_int(-1)("-5")
+ with self.assertRaisesRegex(ValueError, "argument is negative"):
+ to_positive_or_allowed_int()("-5")
+
with self.assertRaises(ValueError):
to_positive_or_allowed_int(-1)("0")
+ with self.assertRaises(ValueError):
+ to_positive_or_allowed_int()("0")
+ self.assertEqual(to_positive_or_allowed_int(0)("0"), 0)
def test_check_float(self) -> None:
x: Any = 5.5