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
False negative when using the power (**) operator #7765
This is an issue with typeshed due to a limitation in the type system. foo can return either an int or a float, depending on whether x is a positive or negative number. While we could change typeshed in a way that 2 ** x will always return float (which is correct, since for typing purposes int is a subtype if float), this would be rather annoying for users that know that it will return an int, for example because they know that x is always non-negative. Therefore we return Any, because there is no way to properly express this.
We would need a range type to be able to express this.
Wouldn't Union[int, float] be a better option instead of Any? The users can then assert that the result is an int if they are sure that x is non-negative.
Wouldn't Union[int, float] be a better option instead of Any? The users can then assert that the result is an int if they are sure that x is non-negative.
We try to avoid that sort of union return in typeshed stubs because they produce a pretty bad user experience.
Activity
srittau commentedon Oct 21, 2019
This is an issue with typeshed due to a limitation in the type system.
foocan return either anintor afloat, depending on whetherxis a positive or negative number. While we could change typeshed in a way that2 ** xwill always returnfloat(which is correct, since for typing purposesintis a subtype iffloat), this would be rather annoying for users that know that it will return anint, for example because they know thatxis always non-negative. Therefore we returnAny, because there is no way to properly express this.We would need a range type to be able to express this.
nikola-benes commentedon Oct 21, 2019
Wouldn't
Union[int, float]be a better option instead ofAny? The users can then assert that the result is anintif they are sure thatxis non-negative.nikola-benes commentedon Oct 21, 2019
Also, a related problem is that
fails with
mypy --strict. What is the recomended way to deal with this? Explicit type conversion (float(2 ** x))?msullivan commentedon Oct 21, 2019
We try to avoid that sort of union return in typeshed stubs because they produce a pretty bad user experience.
msullivan commentedon Oct 21, 2019
Yeah, probably.
--disallow-return-anymaybe shouldn't be in --strict? See #7767 which I just openedFix mypy Any return
Fix mypy Any return