-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
return better return type for __pow__ #4352
Comments
This comes up every now and then, everyone forgets about negative numbers :-) |
Per the comment, we can't use a more specific return type. Using |
There's also room for type checkers to special case this in specific cases. The original example in the pyright issue, for example, was |
@srittau can I ask why overloads won't work? thank you. |
What overloads would you propose? |
I'm finding it hard to see why But in lieu of that return type or range handling, would it make sense to add an overload or two for the "common" right sides, like 2? I did a quick search of a bunch of projects I had checked out (django, numpy, xarray, fastai, sklearn) and the vast bulk is just
So maybe this is "good enough":
|
Thanks for doing the counting, I'd accept a PR that adds an overload for literal 2!
typeshed doesn't have a way to distinguish constants, so I'm not sure we can see past that. But leaving that aside, typeshed tries pretty hard to avoid false positives for any usage that's reasonably common, because our experience has typically been that users don't want to cast and assert for common usage. Some of our general principles / previous discussion around this are outlined at https://github.com/python/typeshed/blob/master/CONTRIBUTING.md#conventions. The mypy issue linked at the relevant bit happens to use |
I see, I guess I have a different philosophy in that I don't mind creating temp variables to assert my expectations, even if less convenient. I'd rather be told of a potential problem and have to manually dismiss it than to never know that the bug might be there. Returning
I'll submit a PR (when I have time) to add the overload and see how it fares. |
Two more mypy specific points: a) I believe mypy already special cases literal exponents using a plugin, b) If you prefer explicit assertions, you can use |
pow always returns "Any" which makes more specific return type information of ** lost.
created from this issue - microsoft/pyright#871
The text was updated successfully, but these errors were encountered: