Skip to content

filter does not narrow TypeVar used in TypeGuard signature #12996

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

Open
Jasha10 opened this issue Jun 18, 2022 · 1 comment
Open

filter does not narrow TypeVar used in TypeGuard signature #12996

Jasha10 opened this issue Jun 18, 2022 · 1 comment
Labels
bug mypy got something wrong topic-type-variables topic-typeguard TypeGuard / PEP 647

Comments

@Jasha10
Copy link

Jasha10 commented Jun 18, 2022

Bug Report

Using filter with a TypeGuard, the TypeVar used in the TypeGuard signature is not properly narrowed.

To Reproduce

# tmp.py
from typing import Iterator, Optional, TypeVar

from typing_extensions import TypeGuard

T = TypeVar("T")


def is_not_none(x: Optional[T]) -> TypeGuard[T]:
    return x is not None


maybe_ints: list[Optional[int]] = [1, None, 2]
ints: Iterator[int] = filter(is_not_none, maybe_ints)
$ mypy tmp.py
tmp.py:14: error: Argument 1 to "filter" has incompatible type "Callable[[Optional[T]], TypeGuard[T]]"; expected "Callable[[Optional[int]], TypeGuard[int]]"
Found 1 error in 1 file (checked 1 source file)

Expected Behavior

I wouldn't expect any error. Since the second argument to filter is typed as list[Optional[int]], mypy should narrow the TypeVar T appropriately.

Your Environment

  • Mypy version used: mypy 0.950
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.10

This may be related to #12682. I decided to open a new ticket instead of commenting on that issue because the repro above feels different from the one reported on that issue.

@PabloRuizCuevas
Copy link

PabloRuizCuevas commented Feb 15, 2025

I think this changed a bit already:

c:list[int|None] = [2,None, 4]
d = filter(lambda x: x is None, c)  # not working which is kind of annoying
e:list[int|None] = [2,None, 4]
f = filter(lambda x: isinstance(x,None), c)  # not workign

But this works:

k:list[int|None] = [2,None, 4]
g = filter(None, k)
def isNone[T](x: T | None) -> TypeGuard[T]:
    return x is None

a: list[int|None] = [2,None, 4]
b = filter(lambda x: isNone(x), a)

It would be nice if first ones are supported though, but still this issue can be closed already.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-type-variables topic-typeguard TypeGuard / PEP 647
Projects
None yet
Development

No branches or pull requests

3 participants