Skip to content

Commit

Permalink
Fix qstring.nest to accept Iterable as params
Browse files Browse the repository at this point in the history
Fixed `qstring.nest` to use more relaxed `Iterable` type for `params`
over `List`.
  • Loading branch information
jpvanhal committed Sep 29, 2023
1 parent 1f4ad5e commit 4f2d535
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/qstring/nest.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import re
from typing import Dict, Generator, List, Literal, NamedTuple, Tuple, Union
from typing import Dict, Generator, Iterable, List, Literal, NamedTuple, Tuple, Union

from . import exc

Nested = Dict[str, Union[str, List[str], "Nested"]]


def nest(params: List[Tuple[str, str]]) -> Nested:
def nest(params: Iterable[Tuple[str, str]]) -> Nested:
"""
Create a nested object from a list of query string parameters.
Expand Down Expand Up @@ -37,7 +37,7 @@ def nest(params: List[Tuple[str, str]]) -> Nested:


def _convert_params_list_to_dict(
params_list: List[Tuple[str, str]]
params_list: Iterable[Tuple[str, str]]
) -> Dict[str, Union[str, List[str]]]:
params_dict: Dict[str, Union[str, List[str]]] = {}
for key, value in params_list:
Expand Down
10 changes: 9 additions & 1 deletion tests/test_nest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List, Tuple
from typing import Iterable, List, Tuple

import pytest

Expand Down Expand Up @@ -30,6 +30,14 @@ def test_nest(obj: List[Tuple[str, str]], expected: qstring.Nested) -> None:
assert qstring.nest(obj) == expected


def test_nest_accepts_iterable() -> None:
def items() -> Iterable[Tuple[str, str]]:
yield ("foo", "1")
yield ("foo", "2")

assert qstring.nest(items()) == {"foo": ["1", "2"]}


def test_nest_maintains_order() -> None:
nested = qstring.nest(
[
Expand Down

0 comments on commit 4f2d535

Please sign in to comment.