Skip to content
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

builtins: Specify return type of int.__pow__ #2271

Closed
wants to merge 1 commit into from

Conversation

cschramm
Copy link
Contributor

No description provided.

@cschramm cschramm changed the title [builtins]: Specify return type of int.__pow__ builtins: Specify return type of int.__pow__ Jun 21, 2018
@JelleZijlstra
Copy link
Member

The Any return type is intentional here because Union return types are generally annoying to deal with. This is mentioned in https://github.com/python/typeshed/blob/master/CONTRIBUTING.md#conventions.

@cschramm
Copy link
Contributor Author

I don't really agree with that as the user always has the option to cast or just ignore Union return types and having specific types is the purpose of stub files but sorry for the fuss if it's common convention.

Then again, that convention is not followed consistently. There are e.g. signatures for next and sum in the same file that do have Union return types...

@cschramm cschramm closed this Jun 21, 2018
Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please no. The Any was intentional. Otherwise mypy will prompt users for isinstance () checks even with n**2.

@gvanrossum
Copy link
Member

The union return types for next() and sum() are only overloads for special cases, and in most cases the union will disappear because the iterator item type and the default/start value type are the same.

@cschramm
Copy link
Contributor Author

There's absolutely no requirement for isinstance. Other options the user has include:

  • cast(int, n**2) when we know that the result will be int
  • cast(Any, n**e) or n**e # type: ignore when we don't want to deal with the type (why use types at all then?)
  • float(n**e) when both types are possible and we do not want to distinguish

@gvanrossum
Copy link
Member

All of those have the same problem as isinstance -- if you have working correct code, mypy gives you a false positive forcing you to make it uglier. While sometimes that's the right thing to do (e.g. a = []), usually we prefer false negatives over false positives, since false positives very quickly annoy users. Remember that mypy users are Python users first and foremost, not type system zealots. :-)

@JukkaL
Copy link
Contributor

JukkaL commented Jun 25, 2018

By the way, mypy already special cases type checking of n**m for integers when m is a literal, and it can infer int / float as the type instead of Any.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants