microsoft / microsoft/pyright

`list(filter(None, map(func, ...)))` is typed as `list[Unknown]` if `func`'s return type doesn't include `None`

Open
#10,560 0 comments 1 reaction 0 assignees View on GitHub
bug
Dominant language
Python
Stars
15.6k
Forks
1.8k
Avg merge
12h 13m
Merged PRs (30d)
52

Description

**Describe the bug**

`list(filter(None, map(func, ...)))` is typed as `list[Unknown]` if `func`'s return type doesn't include `None`.

**Code or Screenshots**

Code sample in [pyright playground](https://pyright-play.net/?code=CYUwZgBGD20BQH0BcECWA7ALgSggWgD40skAoCCiAJxEwFcr0IBGU00SGaBaKhdaOhCIUGHPiJiIAHwgA5QSDKVqtBk1akwqADaYQfLhAC8UXfqpwFQgDQQAtgEMADnC52A2swC62bFvMDBC4ePgEhEzM9AytFOydXEN5%2BOIgvX39SGgA3EEcdBEwAT2dhbWjDWH8cvILi0rdAyu5k8JBMmvzCkuEdVABnTEaK4KrqkFyu%2Bt6BofKLUZawxT82TrqeuD7B4YtY2wcXN1hPHz8-CgBiCG3MDwBVdABrAQB3dG8sidruhtvdmLWEDxI5JZYHdLnXAQa63DxibxAA)

```python
def foo(_: int) -> int:
return 1

def foo_or_none(_: int) -> int | None:
return 1

filter_foo = filter(None, map(foo, [1]))
filter_foo_or_none = filter(None, map(foo_or_none, [1]))

reveal_type(filter_foo)
reveal_type(filter_foo_or_none)

reveal_type(list(filter_foo))
reveal_type(list(filter_foo_or_none))

reveal_type(list(filter(None, map(foo, [1])))) # list[Unknown]
reveal_type(list(filter(None, map(foo_or_none, [1])))) # list[int]
```

Contributor guide

Open the contributing guide

Research direction

Start with the linked pyright playground and compare the four reveal_type results for filter/map with and without None in the callback return type. Trace the relevant type inference behavior, then confirm that the non-Optional callback case produces the intended list element type without changing the existing Optional case.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.