elastic / elasticsearch-dsl-py

High level Python client for Elasticsearch
http://elasticsearch-dsl.readthedocs.org
Apache License 2.0
3.84k stars 802 forks source link

Type errors while using `Response.hits` attrs #1897

Open realsuayip opened 2 months ago

realsuayip commented 2 months ago

Since the returned type is actually AttrList rather than plain list, accessing those attributes will result in type errors.

https://github.com/elastic/elasticsearch-dsl-py/blob/318ea9a9e4b792325556671e1537df9032ee4e65/elasticsearch_dsl/response/__init__.py#L93

e.g.,

error: "List[Hit]" has no attribute "total" [attr-defined]

miguelgrinberg commented 2 months ago

Yes. Typing isn't complete in the current release. We are working on typing all the Elasticsearch structures including queries and responses, but that will come in a later release.

giovannipcarvalho commented 2 months ago

The same thing happens for response.hits.hits.

Will accessing .hits.hits still be supported in the future, or should we use .hits instead?

miguelgrinberg commented 2 months ago

If you use .hits.hits you are accessing the raw response from Elasticsearch. When you use .hits you are accessing the results after being processed by this library, which in my opinion is preferable. The way this is implemented right now is confusing, but my hope is that .hits.hits will give you access to the unmodified response (possibly with missing or generic typing) and .hits will provide access with correct typing.