Skip to content

Fix _TYPE_HOSTS to require covariant Sequence #2325

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 1 commit into from
Oct 5, 2023

Conversation

pquentin
Copy link
Member

@pquentin pquentin commented Oct 5, 2023

Closes #2321

Comment on lines -142 to +145
if not isinstance(hosts, (tuple, list)):
if isinstance(hosts, str):
Copy link
Member Author

Choose a reason for hiding this comment

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

This change is required because hosts is now a Sequence which allows more than just tuple and list. I considered changing the check to isinstance(hosts, collections.abc.Sequence), but it felt wrong as Python embraces duck typing, which means being a subclass of collections.abc.Sequence is not needed to be an actual sequence. (It was much more convincing when it was still in my head! The irony being that we're doing this to fix type hints where all types are very much explicit.)

Anyway, due to the above, I then decided to switch from a denylist to an allowlist, since only two types currently work: str and NodeConfig. Why disallow NodeConfig then? Because the type does not allow it and I'm not expecting anyone to actually be passing hosts=NodeConfig(...) as it currently only works by accident. (Note that hosts=[NodeConfig(...)] still works.)

@pquentin pquentin merged commit c5c4df4 into elastic:main Oct 5, 2023
github-actions bot pushed a commit that referenced this pull request Oct 5, 2023
github-actions bot pushed a commit that referenced this pull request Oct 5, 2023
pquentin pushed a commit that referenced this pull request Oct 6, 2023
* Fix _TYPE_HOSTS to require covariant Sequence (#2325)

(cherry picked from commit c5c4df4)
pquentin pushed a commit that referenced this pull request Oct 6, 2023
* Fix _TYPE_HOSTS to require covariant Sequence (#2325)

(cherry picked from commit c5c4df4)
@pquentin pquentin deleted the hosts-covariant-sequence branch November 6, 2024 04:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Wrong mypy annotation for _TYPE_HOSTS
2 participants