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

Skip to content

Support type narrowing on all() #16799

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
lstolcman opened this issue Jan 19, 2024 · 1 comment
Closed

Support type narrowing on all() #16799

lstolcman opened this issue Jan 19, 2024 · 1 comment
Labels
bug mypy got something wrong topic-type-narrowing Conditional type narrowing / binder

Comments

@lstolcman
Copy link

Bug Report

Consider a code checking whether all elements of a list are true. If so, it prints every element to the console with appended exclamation mark. If not, it should skip execution and print appropriate message

To Reproduce

example 1 - mypy returns failure

li = [None, "a"]

if not all(li):
    print("Not all elems of li are true")
else:
    for elem in li:
        print(elem + "!")

example 2 - mypy ok

li = ["a", "b"]

if not all(li):
    print("Not all elems of li are true")
else:
    for elem in li:
        print(elem + "!")

Expected Behavior
mypy should not report an error in line 7: print(elem + "!") - python cannot reach this code due to the condition: if not all(li):

Actual Behavior
example 1 fails:

main.py:7: error: Unsupported left operand type for + ("None")  [operator]
main.py:7: note: Left operand is of type "str | None"
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: mypy 1.8.0, mypy master (from https://mypy-play.net)
  • Mypy command-line flags: none, mypy-play was used
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.11, 3.12
@lstolcman lstolcman added the bug mypy got something wrong label Jan 19, 2024
@JelleZijlstra JelleZijlstra changed the title Variable is None-checked but mypy prompts Unsupported left operand type for + ("None") Support type narrowing on all() Jan 19, 2024
@JelleZijlstra JelleZijlstra added the topic-type-narrowing Conditional type narrowing / binder label Jan 19, 2024
@JelleZijlstra
Copy link
Member

Duplicate of #7853

@JelleZijlstra JelleZijlstra marked this as a duplicate of #7853 Jan 19, 2024
@JelleZijlstra JelleZijlstra closed this as not planned Won't fix, can't repro, duplicate, stale Jan 19, 2024
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

2 participants