-
-
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
pow function return type overly broad #285
Comments
That does sound better! Could you send a pull request? |
I'd rather not do this. Basically this would be equivalent to having the
|
That argument works for any Also, the inferred type here would be |
@JukkaL I don't understand. Won't leaving it as Consider:
|
Also see python/mypy#1693 |
@johnthagen This is a tradeoff: by occasionally falling back to @matthiaskramm Yes, any If a type system allows the above call to |
I wonder if it might be worth adding an Obviously, this wouldn't catch int/float errors (which I think we consider overly onerous to do safely in this instance anyway), but would prevent all other misuses. |
We primarily want "safe" Unions for Maybe Going with @ddfisher's idea, maybe every |
I don't think that's the only reason we want safe Unions, actually. It's not uncommon to accept multiple argument types, and it makes sense for the type checker to ensure you're using them safely. A somewhat contrived example: def my_print(x: Union[str, List[str]]) -> None:
if isinstance(x, list):
x = ", ".join(x)
print(x) This is basically Python's version of sum typing, and I think it's reasonably important that it be safe by default. |
If mypy had a way of special casing type checking of certain functions (discussed at python/mypy#1240) it would likely resolve this issue and would still enable precise type checking for at least many call sites. Then having a union return type for
Now we could modify it to this (with an arbitrary
Unions can be used when we have a few similar but not identical types. Mypy can verify that they are used in a way that is compatible with all items. Consider this somewhat realistic example:
|
Shouldn't this case be fixable with overloading?
The only thing this would not catch is that Other than that, |
|
Good point, I did not test that^^ |
can we conclude that this is a won't fix issue? |
Yeah, we're leaving this one alone. |
This change because mypy isn't willing to believe the exponent is always positive. See python/typeshed#285.
As a historical note, in 2022 pow() is now typed with fifteen alternative type signatures (via typing.overload), rather than Any: https://github.com/python/typeshed/blob/c347c29/stdlib/builtins.pyi#L1545 |
I really like the type |
While typeshed has made a bunch of pragmatic improvements, especially with the literal overloads, I don't want to overpromise. The underlying issue remains that e.g. type checkers can't prove that a number is positive, users rarely actually pass negative base (which would return complex), and a union return type would create a lot of false positives. That is, you will still get
|
I happened to be reading through the
typeshed
code because of a completely unrelated issue I was tracking down, and noticed that thepow
function return type seems to be overly broad:https://github.com/python/typeshed/blob/master/stdlib/3/builtins.pyi#L703
Should that be?
The text was updated successfully, but these errors were encountered: