Skip to content
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

Wrong mypy annotation for _TYPE_HOSTS #2321

Closed
pklejch opened this issue Oct 2, 2023 · 2 comments · Fixed by #2325
Closed

Wrong mypy annotation for _TYPE_HOSTS #2321

pklejch opened this issue Oct 2, 2023 · 2 comments · Fixed by #2325

Comments

@pklejch
Copy link

pklejch commented Oct 2, 2023

Elasticsearch version (bin/elasticsearch --version): 8.9.0 (but it's not relevant to this bug)

elasticsearch-py version (elasticsearch.__versionstr__): 8.10.0

Description of the problem including expected versus actual behavior:
Currently _TYPE_HOSTS has type annotation defined as

_TYPE_HOSTS = Union[str, List[Union[str, Mapping[str, Union[str, int]], NodeConfig]]]

The problem is that List is invariant and thus List[str] is not valid subtype for List[Union[str, Mapping[str, Union[str, int]], NodeConfig]]]

See common mypy issue python/mypy#3351

The solution is to replace List with Sequence.

Steps to reproduce:
Minimal example

import elasticsearch

HOSTS = ['http://localhost:9200', 'http://localhost:10200']  # this will be inferred as list[str]

client = elasticsearch.Elasticsearch(hosts=HOSTS)

Mypy output

test.py:5: error: Argument "hosts" to "Elasticsearch" has incompatible type "list[str]"; expected "str | list[str | Mapping[str, str | int] | NodeConfig] | None"  [arg-type]
test.py:5: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
test.py:5: note: Consider using "Sequence" instead, which is covariant
@pquentin
Copy link
Member

pquentin commented Oct 5, 2023

Thanks for the report. We're aware of this bug and plan to fix it soon. Closing in favor of #2273

@pquentin pquentin closed this as completed Oct 5, 2023
@pquentin
Copy link
Member

pquentin commented Oct 5, 2023

Sorry, reopening as I realized the fix for this issue is going to be different as _TYPE_HOSTS is not generated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants