-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
input to sorted
is treated differently based on where the output is passed.
#5394
Comments
This isn't a bug. Pyright is doing the correct thing here given the definition of There are two overloads provided for def sorted(__iter: Iterable[T], *, key: Callable[[T], SupportsRichComparison]) -> list[T]:
... It includes both an The reason you're seeing different behaviors (an error in one case and not in the other) is because of bidirectional type inference. The type of the parameter sorted_list: Iterable[Iterable[Any]] = sorted(track_table, key=operator.itemgetter(0)) If you omit the annotation for |
Describe the bug
The same input to
sorted
can produce (or not produce) an error depending on how the output is used.To Reproduce
A minimal reproducer is included below. This behaviour appears to have changed in version 1.1.281, as version 1.1.280 does not behave this way.
Expected behavior
I believe the correct behaviour is to not bring up an error in either of the cases below. However, it's possible that both cases should result in the error. I think that pyright is using the SupportsRichComparisonT overload of
sorted
in the case that raises the error and the Iterable[_T] overload in that case that does not.Code or Screenshots
Minimal reproducer.
VS Code extension or command-line
CLI tool. Occurs with the reproducer above starting in version 1.1.281, occurs with
tabulate.tabulate
starting at version 1.1.315. We're using tabulate v0.9.0.Additional context
We discovered this after updating our CI to v1.1.315, which causes existing code (circa April 2022) to bring this error. This code uses
tabulate.tabulate
: https://github.com/snapcore/snapcraft/blob/main/snapcraft/commands/names.py#L140The text was updated successfully, but these errors were encountered: