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

Error in mypy when using any() or all() to check for None in a iterable #17218

Open
kingco6ra opened this issue May 6, 2024 · 0 comments
Open
Labels
bug mypy got something wrong topic-type-narrowing Conditional type narrowing / binder

Comments

@kingco6ra
Copy link

kingco6ra commented May 6, 2024

Bug Report

Mypy raises errors indicating the absence of attributes for objects of type "None" when using any() or all().

To Reproduce

from dataclasses import dataclass


def get_hobby_description(hobby: str) -> ...: ...


def get_best_friend_info(best_friend: str) -> ...: ...


@dataclass
class Person:
    name: str
    age: int
    hobby: str | None = None
    best_friend: str | None = None


person = Person(name="name", age=0, hobby=None, best_friend=None)
if not all((person.hobby, person.best_friend)):
    raise ValueError

if not any((person.hobby, person.best_friend)):
    raise ValueError

hobby_description = get_hobby_description(person.hobby)
best_friend_info = get_best_friend_info(person.best_friend)

Expected Behavior

The code should be recognized as correct since it checks for the absence of None among the variables person.hobby and person.best_friend.

Actual Behavior

Mypy raises errors indicating the absence of attributes for objects of type "None" when using any() or all().

error: Argument 1 to "get_hobby_description" has incompatible type "str | None"; expected "str"  [arg-type]
error: Argument 1 to "get_best_friend_info" has incompatible type "str | None"; expected "str"  [arg-type]

Your Environment

  • Mypy version used: mypy 1.10.0 (compiled: yes)
  • Mypy command-line flags: No
  • Mypy configuration options from mypy.ini (and other config files): No
  • Python version used: 3.12.2
@kingco6ra kingco6ra added the bug mypy got something wrong label May 6, 2024
@JelleZijlstra JelleZijlstra added the topic-type-narrowing Conditional type narrowing / binder label May 6, 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