FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

builtins: Specify return type of int.__pow__ by cschramm · Pull Request #2271 · python/typeshed · GitHub

Repository navigation

builtins: Specify return type of int.__pow__ - #2271

Closed
cschramm wants to merge 1 commit into
python:masterfrom
cschramm:patch-1
Closed

cschramm wants to merge 1 commit into
python:masterfrom
cschramm:patch-1

Conversation

Copy link
Copy Markdown
Contributor

No description provided.

cschramm changed the title [builtins]: Specify return type of int.__pow__ builtins: Specify return type of int.__pow__ Jun 21, 2018

Copy link
Copy Markdown
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.

Copy link
Copy Markdown
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 closed this Jun 21, 2018

gvanrossum left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Copy link
Copy Markdown
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.

Copy link
Copy Markdown
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

Copy link
Copy Markdown
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 commented Jun 25, 2018

Copy link
Copy Markdown
Contributor

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.

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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


Back | FazBrowse Home | New Git URL