Skip to content

iter("") --> Iterator[Never] #13902

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

Closed
wants to merge 3 commits into from

Conversation

Avasam
Copy link
Collaborator

@Avasam Avasam commented Apr 29, 2025

To match iter(()) --> Iterator[Never], and any other Never generically typed Iterators (like iter(list[Never]()))

@JelleZijlstra
Copy link
Member

Never often won't be handled by type checkers in a way that you might naively expect. Could you try some examples and see how type checkers handle them?

Also, every overload makes type checking a bit more complicated for type checkers. What sort of use cases does this help?

@Avasam
Copy link
Collaborator Author

Avasam commented Apr 29, 2025

Yeah I thought I might add some tests just in case. I first validated with Pylance locally, but you never know.

Also, every overload makes type checking a bit more complicated for type checkers. What sort of use cases does this help?

Just for completeness sake honestly. It was a curiosity I had in astral-sh/ruff#16569 (comment) to try iter("") and noticed that it resulted in Iterator[str] when we can statically know there's nothing to iterate over.

At that point I figured may as well submit the change I tried for fun.

Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

ibis (https://github.com/ibis-project/ibis)
+ ibis/expr/types/generic.py:1705: error: "Never" has no attribute "to_expr"  [attr-defined]

altair (https://github.com/vega/altair)
+ tools/vega_expr.py:811: error: "Never" has no attribute "get"  [attr-defined]

@Avasam
Copy link
Collaborator Author

Avasam commented Apr 29, 2025

Turns out it's Any that holds surprises here 😉😅

Seems Any/Unknown will just match the first overload, so it matches Literal[""]

@Avasam
Copy link
Collaborator Author

Avasam commented Apr 29, 2025

If passing Any to an overload resulted in a union of of all possible return types *cough* using AnyOf *cough* this would be fine. But as-is matching the first param just leads to false-positives in untyped code and no known real-world benefit. So I'll close this for now.

@Avasam Avasam closed this Apr 29, 2025
@Avasam Avasam deleted the iter(empty_str)----Iterator-Never- branch April 29, 2025 02:33
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 this pull request may close these issues.

2 participants