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

Support type inference after using any() #17992

Open
KazMiddelhoek opened this issue Oct 18, 2024 · 0 comments
Open

Support type inference after using any() #17992

KazMiddelhoek opened this issue Oct 18, 2024 · 0 comments
Labels
feature topic-type-narrowing Conditional type narrowing / binder

Comments

@KazMiddelhoek
Copy link

KazMiddelhoek commented Oct 18, 2024

This function:

def test(a: int | None, b: int | None) -> int | None:

    if any(x is None for x in [a, b]):
        return None

    return a+b

and this function:

def test_2(a: int | None, b: int | None) -> int | None:

    if a is None or b is None:
        return None

    return a+b

do the same thing. However, for the first one, mpy produces an error:

test.py:8: error: Unsupported operand types for + ("int" and "None")  [operator]
test.py:8: error: Unsupported operand types for + ("None" and "int")  [operator]
test.py:8: error: Unsupported left operand type for + ("None")  [operator]

while for the second one, the fact that a and b are not None is inferred correctly.

It would be great to improve the inference for this case!

@JelleZijlstra JelleZijlstra added the topic-type-narrowing Conditional type narrowing / binder label Oct 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature topic-type-narrowing Conditional type narrowing / binder
Projects
None yet
Development

No branches or pull requests

2 participants