Thanks to visit codestin.com
Credit goes to github.com

Skip to content

False positive when an Optional is unpacked with None not in. #11339

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

Closed
yanqd0 opened this issue Oct 15, 2021 · 3 comments
Closed

False positive when an Optional is unpacked with None not in. #11339

yanqd0 opened this issue Oct 15, 2021 · 3 comments
Labels
bug mypy got something wrong topic-type-narrowing Conditional type narrowing / binder

Comments

@yanqd0
Copy link

yanqd0 commented Oct 15, 2021

Bug Report

False positive when an Optional is unpacked with None not in.

To Reproduce

demo.py:

from typing import Optional

a: Optional[int] = 1
b: Optional[int] = 1

if None not in (a, b):
    print(a + b)

Actual Behavior

$ mypy demo.py 
demo.py:7: error: Unsupported operand types for + ("int" and "None")
demo.py:7: error: Unsupported operand types for + ("None" and "int")
demo.py:7: error: Unsupported left operand type for + ("None")
demo.py:7: note: Both left and right operands are unions
Found 3 errors in 1 file (checked 1 source file)

Expected Behavior

$ mypy demo.py 
Success: no issues found in 1 source file

In fact, the 2 expression should be the same:

# mypy failed
if None not in (a, b):
    print(a + b)

# mypy passed
if a is not None and b is not None:
    print(a + b)

Your Environment

$ mypy --version
mypy 0.910
$ python --version
Python 3.8.10
$ uname -a
Linux yan-p520c 5.4.0-88-generic #99-Ubuntu SMP Thu Sep 23 17:29:00 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.3 LTS
Release:        20.04
Codename:       focal
@yanqd0 yanqd0 added the bug mypy got something wrong label Oct 15, 2021
@yanqd0
Copy link
Author

yanqd0 commented Oct 15, 2021

Besides, some other expressions are also failed.

if all((a, b)):
    print(a + b)

if not any(i is None for i in (a, b)):
    print(a + b)

if all(isinstance(i, int) for i in (a, b)):
    print(a + b)

@ppenchev-storpool
Copy link

ppenchev-storpool commented Oct 24, 2021

A question for the developers: should I file a separate issue for the following or is it the same one in essence?

def handle(parts: Optional[List[str]]) -> None:
    """Handle a list... or not."""
    if parts not in (None,):
        pset = set(parts)
        print(f"Handling {pset!r}")
    else:
        print("Nothing to handle")

If the check is if parts is not None:, mypy handles it just fine, but the above fails.

Thanks in advance, and keep up the great work!

@JelleZijlstra JelleZijlstra added the topic-type-narrowing Conditional type narrowing / binder label Mar 19, 2022
@AlexWaygood
Copy link
Member

Duplicate of #2980

@AlexWaygood AlexWaygood marked this as a duplicate of #2980 Oct 21, 2022
@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale Oct 21, 2022
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-narrowing Conditional type narrowing / binder
Projects
None yet
Development

No branches or pull requests

4 participants