Skip to content
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

Find lambdas are not checked correctly #810

Open
eirnym opened this issue Dec 7, 2023 · 1 comment
Open

Find lambdas are not checked correctly #810

eirnym opened this issue Dec 7, 2023 · 1 comment

Comments

@eirnym
Copy link

eirnym commented Dec 7, 2023

Pyre Bug

Bug description
Please enter a clear and concise description of what the bug is.

I use filter function to filter out None values in a second value of sequence of pairs (tuple is used for the pair class).

With pyre-check 0.9.19 and Python 3.12.0 I have following error which is not correct as I filter out None values (value[1] is not None as shown below)

test.py:5:4 Incompatible return type [7]: Expected `Iterable[Tuple[str, str]]` but got `filter[Tuple[str, Optional[str]]]`.

Reproduction steps
Enter steps to reproduce the behavior.
I created an empty virtualenv, created a file test.py in empty folder with contents as shown below and run pyre

from collections import abc
def filter_none(
    values: abc.Sequence[tuple[str, str | None]]
) -> abc.Iterable[tuple[str, str]]:
    return filter(lambda value: value[1] is not None, values)

Expected behavior
Give a clear and concise description of what you expected to happen.

Logs
Please include any relevant logs here:

Output goes here

Please run your reproduction steps followed by pyre rage > pyre_rage.log, and upload the file here:

pyre_rage.log

Additional context
Add any other context about the problem here. (like dependencies in your venv, third party stub files being used, overall goals, etc.)

@ebrahimsofi123
Copy link

Hi! It looks like you're facing a type-checking issue with Pyre when filtering tuples that might contain None in their second element. Pyre seems to struggle with inferring that after filtering, the second element of the tuples is indeed always a string and not None. Normally, you'd expect Pyre to recognize that the lambda function ensures no None values remain in the second element, so the output should be Iterable[Tuple[str, str]]. However, it incorrectly interprets it as Iterable[Tuple[str, Optional[str]]]. A practical workaround is to use a list comprehension instead of the filter function. This alternative approach usually provides clearer type hints that help static analysis tools like Pyre understand the intended types more accurately.

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

No branches or pull requests

2 participants