Skip to content

Commit

Permalink
Make positional only args kwargs or positional (#18)
Browse files Browse the repository at this point in the history
* Make args not positional only

* Fix missing default value in stubs
  • Loading branch information
provinzkraut committed Jul 20, 2023
1 parent 558100b commit f6bd946
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion fast_query_parsers.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Returns:
A list of string/string tuples.
"""

def parse_url_encoded_dict(qs: bytes, parse_numbers: bool) -> dict[str, Any]: ...
def parse_url_encoded_dict(qs: bytes, parse_numbers: bool = False) -> dict[str, Any]: ...

"""Parse a query string into a dictionary of values.
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ pub use query_string::{parse_query_string as parse_qs, parse_query_string_to_jso

// parse query string into a list of tuples.
#[pyfunction]
#[pyo3(text_signature = "(qs, separator, /)")]
#[pyo3(text_signature = "(qs, separator)")]
fn parse_query_string(qs: &[u8], separator: char) -> PyResult<Vec<(String, String)>> {
Ok(parse_qs(qs, separator))
}

// parse query string into a python object.
#[pyfunction]
#[pyo3(signature = (qs, parse_numbers=true, /),text_signature = "(qs, parse_numbers=true, /)")]
#[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
13 changes: 13 additions & 0 deletions tests/test_parse_qs.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,16 @@ def test_parse_urlencoded_defaults_parse_numbers_true() -> None:
"polluting": False,
"json": None,
}


def test_parse_urlencoded_kwargs() -> None:
result = parse_url_encoded_dict(qs=encoded, parse_numbers=True)
assert result == {
"value": [10, 12],
"veggies": ["tomato", "potato", "aubergine"],
"nested": {"some_key": "some_value"},
"calories": 122.53,
"healthy": True,
"polluting": False,
"json": None,
}
4 changes: 4 additions & 0 deletions tests/test_parse_qsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,7 @@ def test_parses_non_ascii_text() -> None:
assert parse_query_string("arabic_text=اختبار اللغة العربية".encode(), "&") == [
("arabic_text", "اختبار اللغة العربية")
]


def test_kwargs() -> None:
assert parse_query_string(qs=b"foo=bar", separator="&") == [("foo", "bar")]

0 comments on commit f6bd946

Please sign in to comment.