-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Open
Labels
bugmypy got something wrongmypy got something wrongtopic-type-narrowingConditional type narrowing / binderConditional type narrowing / binder
Description
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
Metadata
Metadata
Assignees
Labels
bugmypy got something wrongmypy got something wrongtopic-type-narrowingConditional type narrowing / binderConditional type narrowing / binder