You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
hasattr(v, "__iter__") does not narrow int | Iterable[int] #5582
Looks related to #684, I haven't checked what was done there but "In the negative case we do nothing, because the attribute could be present but set to None/empty string, etc." may be related, as with hasattr() we actually know that the attribute is not present.
Hmm, this is a little tricky. To handle this correctly, Pyrefly needs to be able to represent the narrowed type after hasattr(v, "__iter__"). In this specific example, we can use (int & Iterable[int]) | Iterable[int], but this doesn't easily generalize to a protocol with several abstract methods and several hasattr checks. Perhaps we can start with a simple partial fix that only handles protocols like Iterable with only a single abstract method.
Activity
rchen152 commentedon Nov 9, 2026
Hmm, this is a little tricky. To handle this correctly, Pyrefly needs to be able to represent the narrowed type after
hasattr(v, "__iter__"). In this specific example, we can use(int & Iterable[int]) | Iterable[int], but this doesn't easily generalize to a protocol with several abstract methods and severalhasattrchecks. Perhaps we can start with a simple partial fix that only handles protocols likeIterablewith only a single abstract method.