Skip to content

Commit

Permalink
Allow tuples and other iterables in source() method (#1895)
Browse files Browse the repository at this point in the history
* Allow tuples and other iterables in source() method

Fixes #1893

* review feedback

(cherry picked from commit 6853db7)
  • Loading branch information
miguelgrinberg authored and github-actions[bot] committed Sep 3, 2024
1 parent 7dbf074 commit af5a4bc
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
12 changes: 8 additions & 4 deletions elasticsearch_dsl/search_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@
from typing_extensions import Self, TypeVar

from .aggs import A, Agg, AggBase
from .document_base import InstrumentedField
from .exceptions import IllegalOperation
from .query import Bool, Q, Query
from .response import Hit, Response
from .utils import _R, AnyUsingType, AttrDict, DslBase, recursive_to_dict

if TYPE_CHECKING:
from .document_base import InstrumentedField
from .field import Field, Object


Expand Down Expand Up @@ -714,10 +714,14 @@ def ensure_strings(
Dict[str, List[Union[str, "InstrumentedField"]]],
]
) -> Union[str, List[str], Dict[str, List[str]]]:
if isinstance(fields, list):
return [str(f) for f in fields]
elif isinstance(fields, dict):
if isinstance(fields, dict):
return {k: ensure_strings(v) for k, v in fields.items()}
elif not isinstance(fields, (str, InstrumentedField)):
# we assume that if `fields` is not a any of [dict, str,
# InstrumentedField] then it is an iterable of strings or
# InstrumentedFields, so we convert them to a plain list of
# strings
return [str(f) for f in fields]
else:
return str(fields)

Expand Down
2 changes: 1 addition & 1 deletion tests/_async/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ def test_source() -> None:

assert {
"_source": {"includes": ["foo.bar.*"], "excludes": ["foo.one"]}
} == AsyncSearch().source(includes=["foo.bar.*"], excludes=["foo.one"]).to_dict()
} == AsyncSearch().source(includes=["foo.bar.*"], excludes=("foo.one",)).to_dict()

assert {"_source": False} == AsyncSearch().source(False).to_dict()

Expand Down
2 changes: 1 addition & 1 deletion tests/_sync/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ def test_source() -> None:

assert {
"_source": {"includes": ["foo.bar.*"], "excludes": ["foo.one"]}
} == Search().source(includes=["foo.bar.*"], excludes=["foo.one"]).to_dict()
} == Search().source(includes=["foo.bar.*"], excludes=("foo.one",)).to_dict()

assert {"_source": False} == Search().source(False).to_dict()

Expand Down

0 comments on commit af5a4bc

Please sign in to comment.