Empty Collection Handling with `next`
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 5.1k
- Forks
- 2.1k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 82
Description
There are some issues when using next when providing an empty collection as a default, let's take this example:
from collections.abc import Iterable
def foo(iter: Iterable[list[int]]) -> None:
next((item for item in iter if len(item) > 5), [])
This leads to errors in Pyright, as [] is treated as list[unknown]. Pyright Playground.
Here are the current next overloads:
@overload
def next(i: SupportsNext[_T], /) -> _T: ...
@overload
def next(i: SupportsNext[_T], default: _VT, /) -> _T | _VT: ...
I would propose changing the second one to:
def next(i: SupportsNext[_T], default: _VT | _T, /) -> _T | _VT: ...
This would fix the above issue, as in the case an empty list or othe rcollection Pyright could just infer the type as _T, while not disrupting other cases in which the types differ.
With this change in Pyright:
from typing import reveal_type
def foo(iter: Iterable[list[int]]) -> None:
result = next((item for item in iter if len(item) > 5), [])
reveal_type(result) # Revealed type is `list[int]`.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the two current next overloads shown in the issue and reproduce the example in the linked Pyright Playground. Compare the inferred type with the reveal_type expectation; done when the empty default is accepted and the result is inferred as list[int] without disrupting cases with differing default types.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 52/100