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
Sum of an iterable of bools is considered to have a bool type #7579
@JelleZijlstra I'm not sure how that's relevant. The problem is that bool + bool is int not bool. I think the only way to fix this is to add an overload for bool to return int.
@ktbarrett it's relevant because currently we assume that sum(Iterable[T]) returns T. We tried an alternative where we made sum return the return type of __add__ instead, but backed it out because it led to false positives.
Type checkers already understand that bool + bool returns an int.
That int in the return type would have been allowing foo += 1 but now it's been changed to Literal[0] (since I think it's intended to represent the default start value).
pyright doesn't raise this error on the newer stubs so maybe mypy needs to be updated to account for this as well?
Alternatively, if the Literal[0] isn't providing any significant benefit, maybe it would just be simplest to revert Literal[0] to int?
pyright doesn't raise this error on the newer stubs so maybe mypy needs to be updated to account for this as well?
Yeah, there's some unfortunate disagreement among type checkers at the moment on how to treat literal types 😕 see #7258 for an extended discussion
But while the immediate cause of the new error might have been the change from int to Literal[0], I don't think that's really the underlying problem here.
The specific case of bool + bool -> int has been fixed in #7975. The general case can't be solved without introducing a significant number of false-positive errors, as we saw when experimenting in #7578. As such, I'm closing this issue as as-fixed-as-it-can-be-for-now.
Activity
JelleZijlstra commentedon Apr 2, 2022
See #7578 where we recently experimented with ways to address this sort of thing.
ktbarrett commentedon Apr 2, 2022
@JelleZijlstra I'm not sure how that's relevant. The problem is that
bool + boolisintnotbool. I think the only way to fix this is to add an overload forboolto returnint.JelleZijlstra commentedon Apr 2, 2022
@ktbarrett it's relevant because currently we assume that
sum(Iterable[T])returnsT. We tried an alternative where we madesumreturn the return type of__add__instead, but backed it out because it led to false positives.Type checkers already understand that
bool + boolreturns an int.ktbarrett commentedon Apr 2, 2022
Ah, I see the middle commit now. No one seemed to comment on the failure.
jpy-git commentedon Apr 2, 2022
I think the reason this was passing in mypy 0.931 was that the return used to be typed as
_T | intand has since been changed to_T | Literal[0](the typevar change in #7578 won't have made it into mypy yet)https://github.com/python/mypy/blob/4486d764c07a1cee9792d691e8e85cedfed2a451/mypy/typeshed/stdlib/builtins.pyi#L1389-L1393
That
intin the return type would have been allowingfoo += 1but now it's been changed toLiteral[0](since I think it's intended to represent the defaultstartvalue).You can see an example here: https://mypy-play.net/?mypy=0.941&python=3.10&flags=strict&gist=4b96aaf8e1261db4bf484c0f76138638
pyrightdoesn't raise this error on the newer stubs so maybe mypy needs to be updated to account for this as well?Alternatively, if the
Literal[0]isn't providing any significant benefit, maybe it would just be simplest to revertLiteral[0]toint?AlexWaygood commentedon Apr 2, 2022
Not quite.
Literal[0]is there to represent the special case of the empty iterable being passed tosum.jpy-git commentedon Apr 2, 2022
yes because if you have an empty iterable you never add anything to the start value which is by default 0 😄
AlexWaygood commentedon Apr 2, 2022
Sorry, looks like we were saying the same thing in different ways there 😄
AlexWaygood commentedon Apr 2, 2022
Yeah, there's some unfortunate disagreement among type checkers at the moment on how to treat literal types 😕 see #7258 for an extended discussion
But while the immediate cause of the new error might have been the change from
inttoLiteral[0], I don't think that's really the underlying problem here.AlexWaygood commentedon May 28, 2022
The specific case of
bool + bool -> inthas been fixed in #7975. The general case can't be solved without introducing a significant number of false-positive errors, as we saw when experimenting in #7578. As such, I'm closing this issue as as-fixed-as-it-can-be-for-now.