python / python/typeshed

Empty Collection Handling with `next`

Open
#12,064 0 comments 0 reactions 0 assignees View on GitHub

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.