Skip to content

Allow tuples and other iterables in source() method #1895

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 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,10 @@ 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)):
return [str(f) for f in fields]
Copy link
Member

@pquentin pquentin Sep 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you considered a more explicit check, like explicitly allowing lists and tuples or looking for for collections.abc.Iterable? If not, maybe this warrants a comment?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add a comment for now. Basically the way I'm thinking about this is that in this case the typing change broke how the library works at runtime. My goal was to address the runtime problem. As I mentioned back when we did the typing changes, I think we could replace specific types such as List and Dict with more generic ones in a lot of places, but when I look at that I'll do it for the whole library.

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
Loading