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

Pyright fails to find type error if argument uses a high exponent #7877

Closed
ottumm opened this issue May 8, 2024 · 9 comments
Closed

Pyright fails to find type error if argument uses a high exponent #7877

ottumm opened this issue May 8, 2024 · 9 comments
Labels
as designed Not a bug, working as intended bug Something isn't working

Comments

@ottumm
Copy link

ottumm commented May 8, 2024

Describe the bug
When an expression involving an integer and a high exponent (e.g. 2**31) is passed into a function expecting a non-integer type, Pyright doesn't catch the error. I've tried this with lower exponents (e.g. 2**2) and Pyright does correctly catch the error.

Code or Screenshots

def bar(f: str) -> int:
    ...


bar(2**31)

(pyright playground link)

VS Code extension or command-line
Command-line, version 1.1.361, strict mode on.

@ottumm ottumm added the bug Something isn't working label May 8, 2024
@ottumm
Copy link
Author

ottumm commented May 8, 2024

It appears this is because 2**31's type is being inferenced as Any.

@erictraut
Copy link
Collaborator

Yes, this behavior is correct based on the typeshed stub for int.__pow__. It has several overloads, but the fallback overload used in this case is:

    def __pow__(self, value: int, mod: None = None, /) -> Any: ...

@erictraut
Copy link
Collaborator

Closing because pyright is doing the right thing here given the type information it's provided.

@erictraut erictraut closed this as not planned Won't fix, can't repro, duplicate, stale May 8, 2024
@erictraut erictraut added the as designed Not a bug, working as intended label May 8, 2024
@ottumm
Copy link
Author

ottumm commented May 8, 2024

I've found that the cut-off here is 2**26. 2**25's type is inferred as int and 2**26 is inferred as Any. Is it really expected behavior that that is the cut-off?

Screenshot 2024-05-08 at 12 15 33

@ottumm
Copy link
Author

ottumm commented May 8, 2024

If there was an option to prevent Pyright from accepting an Any type when a more-specific type is required, that would also have helped me avoid the bug in this case.

@ottumm
Copy link
Author

ottumm commented May 8, 2024

Also for what it's worth, mypy reports the same example as a type error: https://mypy-play.net/?mypy=latest&python=3.12&gist=4474b98a1574fa206eaab2ee0d05e692

@erictraut
Copy link
Collaborator

Pyright is not using type inference in this case. It's applying standard type rules (as specified in the Python typing spec) to type information provided by typeshed stubs.

Mypy provides special-case logic that overrides the type information provided in the typeshed stubs in this instance. That's something I generally don't do in pyright. I've done it a few times in the past, and I've regretted it every time because it leads to compatibility and maintenance issues.

@ottumm
Copy link
Author

ottumm commented May 8, 2024

That makes sense; thank you for the explanation. It seems like the correct place for me to take this up is with typeshed's policy of avoiding union types for return values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
as designed Not a bug, working as intended bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants