-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Unexpected error with bool overload (Xarray) #6069
Comments
I believe this is due to the fact that the only overload for which |
This behavior is by design. When performing overload matching, pyright will expand unions of argument types, but it does not expand a Unfortunately, overload matching behaviors are not specified in any PEP, but I've tried very hard to match the behavior of mypy so developers (and library authors) see consistent behaviors. Mypy likewise does not expand For reference, here's a self-contained minimal repro of the issue. As you can see, both pyright and mypy generate an error for this code. from typing import Literal, overload
@overload
def func1(x: Literal[True]) -> int:
...
@overload
def func1(x: Literal[False]) -> str:
...
def func1(x: bool) -> int | str:
return ""
def func2(x: bool):
func1(x) If you want your overloaded function to accept |
@erictraut Thanks however part of the problem is that the error message does not really convey this.
but the error message does not convey that none of the overloads match. Only that one specific overload does not.
|
Yes, reporting cogent messages for overload mismatches is very difficult. Older versions of pyright simply said "no overloads match", but users (rightfully) said that this error message wasn't very helpful. We've gone through many rounds of refinements since then. The latest version attempts to find the overload that is "closest" to what the user probably intended (using a heuristic) and then emits an error based on this signature. Very few users understand how complex overload matching actually is — and would definitely be confused by the notion of union expansion if the error message attempted to explain that. So the current error message is the best we've been able to come up with for this situation. If you have other specific suggestions, let us know. |
I see, I will leave it up to you to judge what it the least confusing but personally I read that error message as if pyright (due to a bug) only considered the Literal[False] overload and did not at all evaluate the others. Personally, I would find the error message clearer if it was prepended with something like.
If this cannot be done realistically be done better, it would be nice if the docs were updated to reflect the current state. |
…o find any applicable overloads. This addresses #6069.
OK, I like that idea. I've implemented a variant of what you suggested. Pyright now emits two diagnostics. The first indicates that "no overloads match the provided arguments" and points to the closest overload match that it could identify. It then evaluates the call expression using the closest match and emits any errors associated with that call evaluation. This will be included in the next release. I'll be curious to see what the feedback is. |
Great Thanks. I have in the mean time added a pr to xarray to add the bool overload as you suggested. |
…o find any applicable overloads. This addresses #6069. (#6076) Co-authored-by: Eric Traut <[email protected]>
Describe the bug
The code below unexpectedly raises an Error
However as can be seen below the method accepts both True and False.
Code or Screenshots
Where the definition of to_netcdf contains variants both for Literal[True] and Literal[False]
https://github.com/pydata/xarray/blob/main/xarray/core/dataset.py#L2130
Xarray: 2023.9.0
VS Code extension or command-line
Both. Pyright 1.1.329 on the cmd line
The text was updated successfully, but these errors were encountered: