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.

request: Add a var_name parameter to converter.

Anders Kaseorg5f92078d077b
diff --git a/zerver/decorator.py b/zerver/decorator.py
index db554fbfe99..eb13f08153f 100644
--- a/zerver/decorator.py
+++ b/zerver/decorator.py
@@ -857,7 +857,7 @@ def internal_notify_view(
return _wrapped_view_func
-def to_utc_datetime(timestamp: str) -> datetime.datetime:
+def to_utc_datetime(var_name: str, timestamp: str) -> datetime.datetime:
return timestamp_to_datetime(float(timestamp))
diff --git a/zerver/lib/request.py b/zerver/lib/request.py
index b91fc47a1aa..a7f5d7b90e3 100644
--- a/zerver/lib/request.py
+++ b/zerver/lib/request.py
@@ -130,7 +130,7 @@ class _REQ(Generic[ResultT]):
self,
whence: Optional[str] = None,
*,
- converter: Optional[Callable[[str], ResultT]] = None,
+ converter: Optional[Callable[[str, str], ResultT]] = None,
default: Union[_NotSpecified, ResultT, None] = NotSpecified,
json_validator: Optional[Validator[ResultT]] = None,
str_validator: Optional[Validator[ResultT]] = None,
@@ -204,7 +204,7 @@ class _REQ(Generic[ResultT]):
def REQ(
whence: Optional[str] = ...,
*,
- converter: Callable[[str], ResultT],
+ converter: Callable[[str, str], ResultT],
default: ResultT = ...,
intentionally_undocumented: bool = ...,
documentation_pending: bool = ...,
@@ -278,7 +278,7 @@ def REQ(
def REQ(
whence: Optional[str] = None,
*,
- converter: Optional[Callable[[str], ResultT]] = None,
+ converter: Optional[Callable[[str, str], ResultT]] = None,
default: Union[_REQ._NotSpecified, ResultT] = _REQ.NotSpecified,
json_validator: Optional[Validator[ResultT]] = None,
str_validator: Optional[Validator[ResultT]] = None,
@@ -413,7 +413,7 @@ def has_request_variables(view_func: ViewFuncT) -> ViewFuncT:
if param.converter is not None and not default_assigned:
try:
- val = param.converter(val)
+ val = param.converter(post_var_name, val)
except JsonableError:
raise
except Exception:
diff --git a/zerver/lib/topic.py b/zerver/lib/topic.py
index 544c17cf124..2dd145b157c 100644
--- a/zerver/lib/topic.py
+++ b/zerver/lib/topic.py
@@ -51,7 +51,7 @@ def REQ_topic() -> Optional[str]:
return REQ(
whence="topic",
aliases=["subject"],
- converter=lambda x: x.strip(),
+ converter=lambda var_name, x: x.strip(),
default=None,
)
diff --git a/zerver/lib/validator.py b/zerver/lib/validator.py
index 78721f899d3..c9138f71e42 100644
--- a/zerver/lib/validator.py
+++ b/zerver/lib/validator.py
@@ -530,7 +530,7 @@ def validate_todo_data(todo_data: object) -> None:
# Converter functions for use with has_request_variables
-def to_non_negative_int(s: str, max_int_size: int = 2**32 - 1) -> int:
+def to_non_negative_int(var_name: str, s: str, max_int_size: int = 2**32 - 1) -> int:
x = int(s)
if x < 0:
raise ValueError("argument is negative")
@@ -539,15 +539,15 @@ def to_non_negative_int(s: str, max_int_size: int = 2**32 - 1) -> int:
return x
-def to_float(s: str) -> float:
+def to_float(var_name: str, s: str) -> float:
return float(s)
-def to_decimal(s: str) -> Decimal:
+def to_decimal(var_name: str, s: str) -> Decimal:
return Decimal(s)
-def to_timezone_or_empty(s: str) -> str:
+def to_timezone_or_empty(var_name: str, s: str) -> str:
if s in pytz.all_timezones_set:
return canonicalize_timezone(s)
else:
@@ -555,11 +555,11 @@ def to_timezone_or_empty(s: str) -> str:
def to_converted_or_fallback(
- sub_converter: Callable[[str], ResultT], default: ResultT
-) -> Callable[[str], ResultT]:
- def converter(s: str) -> ResultT:
+ sub_converter: Callable[[str, str], ResultT], default: ResultT
+) -> Callable[[str, str], ResultT]:
+ def converter(var_name: str, s: str) -> ResultT:
try:
- return sub_converter(s)
+ return sub_converter(var_name, s)
except ValueError:
return default
diff --git a/zerver/tests/test_decorators.py b/zerver/tests/test_decorators.py
index 17c03ed4c53..61b4500cab5 100644
--- a/zerver/tests/test_decorators.py
+++ b/zerver/tests/test_decorators.py
@@ -154,7 +154,7 @@ class DecoratorTestCase(ZulipTestCase):
self.assertEqual(str(cm.exception), "Can't decide between 'number' and 'x' arguments")
def test_REQ_converter(self) -> None:
- def my_converter(data: str) -> List[int]:
+ def my_converter(var_name: str, data: str) -> List[int]:
lst = orjson.loads(data)
if not isinstance(lst, list):
raise ValueError("not a list")
@@ -766,13 +766,13 @@ class ValidatorTestCase(ZulipTestCase):
check_int("x", x)
def test_to_non_negative_int(self) -> None:
- self.assertEqual(to_non_negative_int("5"), 5)
+ self.assertEqual(to_non_negative_int("x", "5"), 5)
with self.assertRaisesRegex(ValueError, "argument is negative"):
- to_non_negative_int("-1")
+ to_non_negative_int("x", "-1")
with self.assertRaisesRegex(ValueError, re.escape("5 is too large (max 4)")):
- to_non_negative_int("5", max_int_size=4)
+ to_non_negative_int("x", "5", max_int_size=4)
with self.assertRaisesRegex(ValueError, re.escape(f"{2**32} is too large (max {2**32-1})")):
- to_non_negative_int(str(2**32))
+ to_non_negative_int("x", str(2**32))
def test_check_float(self) -> None:
x: Any = 5.5
diff --git a/zerver/tornado/views.py b/zerver/tornado/views.py
index a17793fbf41..cb5c07e515d 100644
--- a/zerver/tornado/views.py
+++ b/zerver/tornado/views.py
@@ -65,7 +65,7 @@ def get_events_backend(
# user_client is intended only for internal Django=>Tornado requests
# and thus shouldn't be documented for external use.
user_client: Optional[Client] = REQ(
- converter=get_client, default=None, intentionally_undocumented=True
+ converter=lambda var_name, s: get_client(s), default=None, intentionally_undocumented=True
),
last_event_id: Optional[int] = REQ(json_validator=check_int, default=None),
queue_id: Optional[str] = REQ(default=None),
diff --git a/zerver/views/custom_profile_fields.py b/zerver/views/custom_profile_fields.py
index 366bdc3e53e..e378f054b04 100644
--- a/zerver/views/custom_profile_fields.py
+++ b/zerver/views/custom_profile_fields.py
@@ -105,7 +105,7 @@ check_profile_field_data: Validator[ProfileFieldData] = check_dict(
def create_realm_custom_profile_field(
request: HttpRequest,
user_profile: UserProfile,
- name: str = REQ(default="", converter=lambda x: x.strip()),
+ name: str = REQ(default="", converter=lambda var_name, x: x.strip()),
hint: str = REQ(default=""),
field_data: ProfileFieldData = REQ(default={}, json_validator=check_profile_field_data),
field_type: int = REQ(json_validator=check_int),
@@ -152,7 +152,7 @@ def update_realm_custom_profile_field(
request: HttpRequest,
user_profile: UserProfile,
field_id: int,
- name: str = REQ(default="", converter=lambda x: x.strip()),
+ name: str = REQ(default="", converter=lambda var_name, x: x.strip()),
hint: str = REQ(default=""),
field_data: ProfileFieldData = REQ(default={}, json_validator=check_profile_field_data),
) -> HttpResponse:
diff --git a/zerver/views/message_fetch.py b/zerver/views/message_fetch.py
index e9ee6809ac0..4d339c672b3 100644
--- a/zerver/views/message_fetch.py
+++ b/zerver/views/message_fetch.py
@@ -600,7 +600,7 @@ def get_search_fields(
}
-def narrow_parameter(json: str) -> OptionalNarrowListT:
+def narrow_parameter(var_name: str, json: str) -> OptionalNarrowListT:
data = orjson.loads(json)
if not isinstance(data, list):