Description
Elasticsearch version (bin/elasticsearch --version
):
8.9.0
elasticsearch-py
version (elasticsearch.__versionstr__
):
8.9.0
Description of the problem including expected versus actual behavior:
The auto generated type annotations for the searches
parameter (body
) in msearch
methods (and possibly msearch_template
, but I have never used that API) under the clients are incorrect and a static type checker will report type errors when a List
is passed to it.
Steps to reproduce:
Since List
s are invariant (unlike Tuple
s) the following will trigger a type error;
(apologies for the slightly "complicated" code but this way the type of the searches
variable will be correctly resolved thanks to itertools
)
import itertools
from elasticsearch import Elasticsearch
es = Elasticsearch("http://localhost:9200")
ref_ids_to_search = (
"9a225de6-53fe-4825-a7bb-089fd57b0238",
"621b146c-1bdc-4ce3-8a77-d3d637504a80",
)
searches_list = list(
itertools.chain.from_iterable(
(
{},
{
"query": {
"term": {
"ref_id.keyword": ref_id,
}
}
},
)
for ref_id in ref_ids_to_search
)
)
r = es.msearch(index="my-index", searches=searches_list) # Argument of type "list[dict[str, dict[str, dict[str, str]]]]" cannot be assigned to parameter "searches" of type "List[Mapping[str, Any]] | Tuple[Mapping[str, Any], ...]" in function "msearch"
Changing the signature from this:
elasticsearch-py/elasticsearch/_async/client/__init__.py
Lines 2568 to 2570 in b045ce1
to this:
searches: t.Sequence[t.Mapping[str, t.Any]]
would (I believe) resolve the issue.
Unsure if this issue would be fixable with changes to elasticsearch-specification and as the code generator project is private I decided to open an issue here since this feels Python specific.