Skip to content

Commit

Permalink
fix(python): add missing default value for parse_numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
Goldziher committed Jul 19, 2023
1 parent 9a208f0 commit 558100b
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 31 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description = "Ultra-fast query string and url-encoded form-data parsers"
readme = "README.md"
license-file = "LICENSE"
repository = "https://github.com/starlite-api/fast-query-parsers"
version = "1.0.0"
version = "1.0.1"
edition = "2021"

[lib]
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ encoded = urlencode(
]
).encode()

result = parse_url_encoded_dict(encoded, True)
result = parse_url_encoded_dict(encoded, parse_numbers=True)

# result == {
# "value": [10, 12],
Expand All @@ -92,6 +92,7 @@ does not nest all values inside lists.

Note: the second argument passed to `parse_url_encoded_dict` dictates whether numbers should be parsed. If `True`,
the value will be parsed into an int or float as appropriate, otherwise it will be kept as a string.
By default the value of this arg is `True`.

#### Benchmarks

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "fast-query-parsers"
version = "1.0.0"
version = "1.0.1"
description = "Ultra-fast query string and url-encoded form-data parsers"
authors = [
"Na'aman Hirschfeld <[email protected]>",
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn parse_query_string(qs: &[u8], separator: char) -> PyResult<Vec<(String, Strin

// parse query string into a python object.
#[pyfunction]
#[pyo3(text_signature = "(qs, parse_numbers, /)")]
#[pyo3(signature = (qs, parse_numbers=true, /),text_signature = "(qs, parse_numbers=true, /)")]
fn parse_url_encoded_dict(py: Python, qs: &[u8], parse_numbers: bool) -> PyResult<PyObject> {
Ok(pythonize(py, &parse_query_string_to_json(qs, parse_numbers)).unwrap())
}
Expand Down
50 changes: 26 additions & 24 deletions tests/test_parse_qs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@

from fast_query_parsers import parse_url_encoded_dict

encoded = urlencode(
[
("value", "10"),
("value", "12"),
("veggies", '["tomato", "potato", "aubergine"]'),
("nested", '{"some_key": "some_value"}'),
("calories", "122.53"),
("healthy", "true"),
("polluting", "false"),
("json", "null"),
],
).encode()


def test_parse_urlencoded_with_parse_numbers() -> None:
encoded = urlencode(
[
("value", "10"),
("value", "12"),
("veggies", '["tomato", "potato", "aubergine"]'),
("nested", '{"some_key": "some_value"}'),
("calories", "122.53"),
("healthy", "true"),
("polluting", "false"),
("json", "null"),
],
).encode()
result = parse_url_encoded_dict(encoded, True)
assert result == {
"value": [10, 12],
Expand All @@ -29,18 +30,6 @@ def test_parse_urlencoded_with_parse_numbers() -> None:


def test_parse_urlencoded_without_parse_numbers() -> None:
encoded = urlencode(
[
("value", "10"),
("value", "12"),
("veggies", '["tomato", "potato", "aubergine"]'),
("nested", '{"some_key": "some_value"}'),
("calories", "122.53"),
("healthy", "true"),
("polluting", "false"),
("json", "null"),
],
).encode()
result = parse_url_encoded_dict(encoded, False)
assert result == {
"value": ["10", "12"],
Expand All @@ -51,3 +40,16 @@ def test_parse_urlencoded_without_parse_numbers() -> None:
"polluting": False,
"json": None,
}


def test_parse_urlencoded_defaults_parse_numbers_true() -> None:
result = parse_url_encoded_dict(encoded)
assert result == {
"value": [10, 12],
"veggies": ["tomato", "potato", "aubergine"],
"nested": {"some_key": "some_value"},
"calories": 122.53,
"healthy": True,
"polluting": False,
"json": None,
}

0 comments on commit 558100b

Please sign in to comment.