GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — builtins: Specify return type of int.__pow__ by cschramm · Pull Request #2271 · python/typeshed

Conversation

@cschramm

@cschramm cschramm commented Jun 21, 2018

Copy link
Copy Markdown
Contributor

No description provided.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@cschramm
[builtins]: Specify return type of int.__pow__
a5a49d9
@cschramm cschramm changed the title [builtins]: Specify return type of int.__pow__ builtins: Specify return type of int.__pow__ Jun 21, 2018
@JelleZijlstra

JelleZijlstra commented 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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@cschramm

cschramm commented Jun 21, 2018

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...

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@cschramm cschramm closed this Jun 21, 2018
gvanrossum
gvanrossum reviewed Jun 21, 2018
View reviewed changes

@gvanrossum gvanrossum left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this 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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@gvanrossum

gvanrossum commented Jun 21, 2018

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@srittau srittau mentioned this pull request Jun 21, 2018
AnyOf - Union for return types python/typing#566
Open
@cschramm

cschramm commented Jun 22, 2018

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
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gvanrossum

gvanrossum commented Jun 23, 2018

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. :-)

cschramm and fvilante reacted with thumbs up emoji fvilante reacted with heart emoji
All reactions
  • 👍 2 reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@JukkaL

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@GatienBouyer GatienBouyer mentioned this pull request Dec 10, 2023
Pow doesn't infer types when variables are used python/mypy#16635
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@gvanrossum gvanrossum gvanrossum left review comments

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@cschramm @JelleZijlstra @gvanrossum @JukkaL
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.